我正在尝试用Java实现Kafka使用者,以便接收和处理Kafka主题中的记录。首先,我想在标准输出上打印流的内容。
我的问题是,即使数据长度永远不为零,流的内容也始终为空。
这是我的代码:
public class PrintData {
private static Consumer<Long, String> createConsumer() {
final Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "KafkaExampleConsumer");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, LongDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
// Create the consumer using props.
final Consumer<Long, String> consumer = new KafkaConsumer<>(props);
// Subscribe to the topic.
consumer.subscribe(Collections.singletonList("print_label"));
return consumer;
}
static void runConsumer() throws InterruptedException {
final Consumer<Long, String> consumer = createConsumer();
final int giveUp = 100; int noRecordsCount = 0;
while (true) {
final ConsumerRecords<Long, String> consumerRecords = consumer.poll(Duration.ofSeconds(1));
if (consumerRecords.count()==0) {
noRecordsCount++;
if (noRecordsCount > giveUp) break;
else continue;
}
consumerRecords.forEach(record -> {
System.out.println(record.toString());
System.out.println("Record value: " + record.value());
System.out.println("Value length: " + record.value().length());
System.out.println("");
});
consumer.commitAsync();
}
consumer.close();
System.out.println("DONE");
}
public static void main(String... args) throws Exception {
runConsumer();
}
}
这是我在Kafka中注入数据时的输出:
ConsumerRecord(topic = print_label, partition = 0, leaderEpoch = 0, offset = 41, CreateTime = 1551367936305, serialized key size = -1, serialized value size = 144, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value =
Record value:
Value length: 141
我在做什么错了?