0
Javaコンシューマ向けの作業をローカルホストで実行できません。コンソールコンシューマは正常に動作します。 以下は私の消費者コードです。KAFKA Javaコンシューマが動作しない
パブリッククラスTestConsumer { 公共の静的な無効メイン(文字列[]引数)が例外をスロー{
//Kafka consumer configuration settings
String topicName = "test";// args[0].toString();
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-consumer-group");
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer <String, String>(props);
//Kafka Consumer subscribes list of topics here.
consumer.subscribe(Arrays.asList(topicName));
//print the topic name
System.out.println("Subscribed to topic " + topicName);
int i = 0;
while (true) {
System.out.printf("while loop");
ConsumerRecords<String, String> records = consumer.poll(1000);
for (ConsumerRecord<String, String> record : records) // print the offset,key and value for the consumer records.
System.out.printf("offset = %d, key = %s, value = %s\n",record.offset(), record.key(), record.value());
}
}}
Javaプロデューサが同じトピックに正常に動作します。
パブリッククラスTestProducer { 公共の静的な無効メイン(文字列[]引数)が、私は消費者の別の実装を持って、それが作業を開始した例外{
//Assign topicName to string variable
String topicName = "test";//args[0].toString();
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer
<String, String>(props);
for(int i = 0; i < 10; i++)
producer.send(new ProducerRecord<String, String>(topicName,
Integer.toString(i), Integer.toString(i)));
System.out.println("Message sent successfully");
producer.close();
}
}
例外はありますか?私はあなたのローカルホスト*で自分のJavaコンシューマの仕事を得ることができないですか? –