Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t似乎改变了KStream<;A、 B>;至KTable<;十、 Y>;_Java_Apache Kafka_Apache Kafka Streams - Fatal编程技术网

Java Can';t似乎改变了KStream<;A、 B>;至KTable<;十、 Y>;

Java Can';t似乎改变了KStream<;A、 B>;至KTable<;十、 Y>;,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,这是我第一次尝试使用KTable。我有一个Kafka流,其中包含类型为a、B的Avro序列化对象。这个很好用。我可以编写一个消费量很好的消费者,也可以编写一个简单的KStream来简单计算记录 B对象有一个包含国家代码的字段。我希望将该代码提供给KTable,以便它可以计算包含特定国家代码的记录数。为此,我尝试将流转换为X,Y(或者实际上是:国家代码,计数)流。最后,我查看表中的内容并提取一个KV对数组 我(包括在内)的代码总是出现以下错误(请参见带有“原因”的行): 这是我正在使用的代码。为了

这是我第一次尝试使用
KTable
。我有一个Kafka流,其中包含类型为
a、B
的Avro序列化对象。这个很好用。我可以编写一个消费量很好的消费者,也可以编写一个简单的
KStream
来简单计算记录

B
对象有一个包含国家代码的字段。我希望将该代码提供给KTable,以便它可以计算包含特定国家代码的记录数。为此,我尝试将流转换为
X,Y
(或者实际上是:国家代码,计数)流。最后,我查看表中的内容并提取一个KV对数组

我(包括在内)的代码总是出现以下错误(请参见带有“原因”的行):

这是我正在使用的代码。为了简洁起见,我省略了某些课程注意我没有使用合流的卡夫卡夫罗类

    private synchronized void createStreamProcessor2() {
    if (streams == null) {
        try {
            Properties props = new Properties();
            props.put(StreamsConfig.APPLICATION_ID_CONFIG, getClass().getName());
            props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);

            StreamsConfig config = new StreamsConfig(props);
            StreamsBuilder builder = new StreamsBuilder();

            Map<String, Object> serdeProps = new HashMap<>();
            serdeProps.put("schema.registry.url", schemaRegistryURL);
            AvroSerde<CpaTrackingCallback> cpaTrackingCallbackAvroSerde = new AvroSerde<>(schemaRegistryURL);
            cpaTrackingCallbackAvroSerde.configure(serdeProps, false);

            // This is the key to telling kafka the specific Serde instance to use
            // to deserialize the Avro encoded value
            KStream<Long, CpaTrackingCallback> stream = builder.stream(CpaTrackingCallback.class.getName(),
                            Consumed.with(Serdes.Long(), cpaTrackingCallbackAvroSerde));

            // provide a way to convert CpsTrackicking... info into just country codes
            // (Long, CpaTrackingCallback) -> (countryCode:Integer, placeHolder:Long)
            TransformerSupplier<Long, CpaTrackingCallback, KeyValue<Integer, Long>> transformer = new TransformerSupplier<Long, CpaTrackingCallback, KeyValue<Integer, Long>>() {
                @Override
                public Transformer<Long, CpaTrackingCallback, KeyValue<Integer, Long>> get() {
                    return new Transformer<Long, CpaTrackingCallback, KeyValue<Integer, Long>>() {

                        @Override
                        public void init(ProcessorContext context) {
                            // Not doing Punctuate so no need to store context
                        }

                        @Override
                        public KeyValue<Integer, Long> transform(Long key, CpaTrackingCallback value) {
                            return new KeyValue(value.getCountryCode(), 1);
                        }

                        @Override
                        public KeyValue<Integer, Long> punctuate(long timestamp) {
                            return null;
                        }

                        @Override
                        public void close() {
                        }
                    };
                }
            };

            KTable<Integer, Long> countryCounts = stream.transform(transformer).groupByKey() //
                            .count(Materialized.as("country-counts"));

            streams = new KafkaStreams(builder.build(), config);
            Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
            streams.cleanUp();
            streams.start();

            try {
                countryCountsView = waitUntilStoreIsQueryable("country-counts", QueryableStoreTypes.keyValueStore(),
                                streams);
            }
            catch (InterruptedException e) {
                log.warn("Interrupted while waiting for query store to become available", e);
            }
        }
        catch (Exception e) {
            log.error(e);
        }
    }
}
private synchronized void createStreamProcessor2(){
if(streams==null){
试一试{
Properties props=新属性();
put(StreamsConfig.APPLICATION_ID_CONFIG,getClass().getName());
put(StreamsConfig.BOOTSTRAP\u server\u CONFIG,bootstrapserver);
StreamsConfig配置=新的StreamsConfig(道具);
StreamsBuilder builder=新的StreamsBuilder();
Map serdeProps=newhashmap();
serdeProps.put(“schema.registry.url”,schemaRegistryURL);
AvroSerde cpaTrackingCallbackAvroSerde=新的AvroSerde(schemaRegistryURL);
cpaTrackingCallbackAvroSerde.configure(serdeProps,false);
//这是告诉卡夫卡要使用的特定Serde实例的关键
//要反序列化Avro编码的值
KStream stream=builder.stream(CpaTrackingCallback.class.getName(),
使用(Serdes.Long(),cpaTrackingCallbackAvroSerde));
//提供一种将CPS Tracking…信息转换为国家代码的方法
//(长,CpaTrackingCallback)->(国家代码:整数,占位符:长)
变压器供应商变压器=新变压器供应商(){
@凌驾
公共变压器get(){
返回新变压器(){
@凌驾
公共void init(ProcessorContext上下文){
//不使用标点符号,因此无需存储上下文
}
@凌驾
公钥值转换(长键、CpaTrackingCallback值){
返回新的KeyValue(value.getCountryCode(),1);
}
@凌驾
公钥值标点(长时间戳){
返回null;
}
@凌驾
公众假期结束(){
}
};
}
};
KTable countryCounts=stream.transform(transformer).groupByKey()//
.count(具体化为“国家计数”);
streams=新的KafkaStreams(builder.build(),config);
Runtime.getRuntime().addShutdownHook(新线程(streams::close));
streams.cleanUp();
streams.start();
试一试{
countryCountsView=waitUntilStoreIsQueryable(“国家计数”,QueryableStoreTypes.keyValueStore(),
溪流);
}
捕捉(中断异常e){
log.warn(“在等待查询存储可用时中断”,e);
}
}
捕获(例外e){
日志错误(e);
}
}
}
KStream
上的裸
groupByKey()
方法使用默认的序列化程序/反序列化程序(您尚未设置)。使用方法
groupByKey(Serialized Serialized)
,如中所示:

.groupByKey(Serialized.with(Serdes.Integer(), Serdes.Long()))

另外请注意,您在自定义TransformerSupplier中所做的工作,只需调用
KStream.map
即可。

谢谢。这解决了眼前的问题。另外,我将尝试改用映射。
.groupByKey(Serialized.with(Serdes.Integer(), Serdes.Long()))