Java 卡夫卡流并行处理不工作

Java 卡夫卡流并行处理不工作,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,我正在学习Kafka Stream,我想构建一个简单的应用程序,从一个主题中读取文本行,并将字母出现的数量放入XDB。我的第二个目标是并行运行。我希望有两个Kafka流的运行实例,它们都同时处理输入,但来自不同的分区。不幸的是,在我启动这两个实例之后,只有一个在工作,另一个在等待 我的配置如下: *输入主题有4个分区 *流应用程序的属性num.Stream.threads设置为2 以下是源代码: 公共类LineStatisticsStream{ 静态模式letterPattern=Pattern

我正在学习Kafka Stream,我想构建一个简单的应用程序,从一个主题中读取文本行,并将字母出现的数量放入XDB。我的第二个目标是并行运行。我希望有两个Kafka流的运行实例,它们都同时处理输入,但来自不同的分区。不幸的是,在我启动这两个实例之后,只有一个在工作,另一个在等待

我的配置如下: *输入主题有4个分区 *流应用程序的属性
num.Stream.threads
设置为2

以下是源代码:

公共类LineStatisticsStream{
静态模式letterPattern=Pattern.compile(“[a-z]”);
InfluxDB InfluxDB;
公共静态void main(字符串[]args){
新建LineStatisticsStream().start();
}
属性getStreamProperties(){
属性=新属性();
properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,“localhost:9092”);
properties.put(StreamsConfig.APPLICATION_ID_CONFIG,“lineStatistics4”);
put(StreamsConfig.CACHE\u MAX\u BYTES\u BUFFERING\u CONFIG,0);
put(StreamsConfig.NUM\u STREAM\u THREADS\u CONFIG,2);
归还财产;
}
拓扑getStreamTopology(ForeachAction操作){
StreamsBuilder builder=新的StreamsBuilder();
KStream-linesStream=builder.stream(“行”,已使用.with(Serdes.String(),Serdes.String());
linesStream.flatMapValues(this::FindAlletters)
.map((k,v)->KeyValue.pair(v,v))
.groupByKey(分组为.with(Serdes.String(),Serdes.String()))
.count()
.toStream()
.to(“letterCount”,产生于.with(Serdes.String(),Serdes.Long());
KStream countStream=builder.stream(“letterCount”,consumered.with(Serdes.String(),Serdes.Long());
countStream.peek(动作);
返回builder.build();
}
私有void insertToInfluxDB(字符串字母,长计数){
写入(点测量(“信孔”)
.标签(“字母”,字母)
.time(System.currentTimeMillis(),TimeUnit.毫秒)
.addField(“计数”,计数)
.build()
);
}
私有列表FindAlletter(字符串s){
列表字母=新的ArrayList();
Matcher Matcher=letterPattern.Matcher(s.toLowerCase());
while(matcher.find()){
添加(匹配器组(0));
}
回信;
}
私有void start(){
influxDB=InfluxDBFactory.connect(“http://127.0.0.1:8086");
influxDB.setDatabase(“字母”);
influxDB.enableBatch(BatchOptions.DEFAULTS);
final KafkaStreams streams=new KafkaStreams(getStreamTopology(this::insertToInfluxDB),getStreamProperties());
streams.start();
Runtime.getRuntime().addShutdownHook(新线程(()->{streams.close();influxDB.close();}));
}
}
以及“等待”应用程序日志的最后几行:

2019-11-18 21:27:16 INFO  ConsumerCoordinator:982 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-1-consumer, groupId=lineStatistics4] Found no committed offset for partition lines4-2
2019-11-18 21:27:16 INFO  ConsumerCoordinator:982 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-2-consumer, groupId=lineStatistics4] Found no committed offset for partition lines4-1
2019-11-18 21:27:16 INFO  RocksDBTimestampedStore:82 - Opening store KSTREAM-AGGREGATE-STATE-STORE-0000000003 in regular mode
2019-11-18 21:27:16 INFO  RocksDBTimestampedStore:82 - Opening store KSTREAM-AGGREGATE-STATE-STORE-0000000003 in regular mode
2019-11-18 21:27:16 INFO  KafkaConsumer:1068 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-2-restore-consumer, groupId=null] Unsubscribed all topics or patterns and assigned partitions
2019-11-18 21:27:16 INFO  KafkaConsumer:1068 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-2-restore-consumer, groupId=null] Unsubscribed all topics or patterns and assigned partitions
2019-11-18 21:27:16 INFO  StreamThread:212 - stream-thread [lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-2] State transition from PARTITIONS_ASSIGNED to RUNNING
2019-11-18 21:27:16 INFO  KafkaConsumer:1068 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-1-restore-consumer, groupId=null] Unsubscribed all topics or patterns and assigned partitions
2019-11-18 21:27:16 INFO  KafkaConsumer:1068 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-1-restore-consumer, groupId=null] Unsubscribed all topics or patterns and assigned partitions
2019-11-18 21:27:16 INFO  StreamThread:212 - stream-thread [lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-1] State transition from PARTITIONS_ASSIGNED to RUNNING
2019-11-18 21:27:16 INFO  KafkaStreams:263 - stream-client [lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099] State transition from REBALANCING to RUNNING
2019-11-18 21:27:16 INFO  ConsumerCoordinator:982 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-2-consumer, groupId=lineStatistics4] Found no committed offset for partition lines4-1
2019-11-18 21:27:16 INFO  ConsumerCoordinator:982 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-1-consumer, groupId=lineStatistics4] Found no committed offset for partition lines4-2
2019-11-18 21:27:16 INFO  ConsumerCoordinator:525 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-2-consumer, groupId=lineStatistics4] Setting offset for partition lineStatistics4-KSTREAM-AGGREGATE-STATE-STORE-0000000003-repartition-0 to the committed offset FetchPosition{offset=73, offsetEpoch=Optional.empty, currentLeader=LeaderAndEpoch{leader=localhost:9092 (id: 1 rack: null), epoch=0}}
2019-11-18 21:27:16 INFO  ConsumerCoordinator:525 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-1-consumer, groupId=lineStatistics4] Setting offset for partition lines4-0 to the committed offset FetchPosition{offset=63, offsetEpoch=Optional.empty, currentLeader=LeaderAndEpoch{leader=localhost:9092 (id: 1 rack: null), epoch=0}}
2019-11-18 21:27:16 INFO  ConsumerCoordinator:525 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-1-consumer, groupId=lineStatistics4] Setting offset for partition lineStatistics4-KSTREAM-AGGREGATE-STATE-STORE-0000000003-repartition-3 to the committed offset FetchPosition{offset=40, offsetEpoch=Optional.empty, currentLeader=LeaderAndEpoch{leader=localhost:9092 (id: 1 rack: null), epoch=0}}
2019-11-18 21:27:17 INFO  SubscriptionState:348 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-2-consumer, groupId=lineStatistics4] Resetting offset for partition lines4-1 to offset 0.
2019-11-18 21:27:17 INFO  SubscriptionState:348 - [Consumer clientId=lineStatistics4-5e5e2a71-2a3b-4e51-9d4c-4e470a8a8099-StreamThread-1-consumer, groupId=lineStatistics4] Resetting offset for partition lines4-2 to offset 0.

这只是信息日志。似乎分配了主题
行4
中的分区
1
2
,因此它应该处理数据(如果有)。另一个实例分配了分区
0
3
1。确保向主题中的分区均匀生成数据。2.使用
kafka consumer groups.sh
实用程序显示您的消费者如何在组上的分区上进行操作。请在此处打印输出,我们可以返回更多信息:)