我正在尝试从__consumer_offsets主题进行消费,因为这似乎是检索有关诸如消息滞后之类的消费者的kafka指标的最简单方法。理想的方法是从jmx访问它,但想首先尝试使用此方法以及回来似乎是加密的或不可读的形式。也尝试添加stringDeserializer属性。有人对如何更正此有任何建议吗?
consumerProps.put("exclude.internal.topics", false);
consumerProps.put("group.id" , groupId);
consumerProps.put("zookeeper.connect", zooKeeper);
consumerProps.put("key.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
consumerProps.put("value.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
ConsumerConfig consumerConfig = new ConsumerConfig(consumerProps);
ConsumerConnector consumer =
kafka.consumer.Consumer.createJavaConsumerConnector(
consumerConfig);
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(1));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap =
consumer.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
for ( KafkaStream stream : streams) {
ConsumerIterator<byte[], byte[]> it = stream.iterator();
//errorReporting("...CONSUMER-KAFKA CONNECTION SUCCESSFUL!");
while (it.hasNext())
{
try{
String mesg = new String(it.next().message());
System.out.println( mesg);
编辑:
我不认为这是重复的,因为它特定于Java和实际的消息格式。我可以从队列中消费,但无法查看邮件内容。是否需要有关如何将其转换为字符串或可读值的指南?
39: