Kafka:在0.11版本中,ConsumerConnector的新API是什么

时间:2017-10-23 23:59:14

标签: java scala apache-kafka kafka-consumer-api

我将Kafka客户端从0.11.0.0更新为ConsumerConnector

在我的旧代码中,我使用createMessageStreams来获取带有ConsumerConnector方法的消息流,然后迭代每个主题的流。但是,似乎package kafka.consumer import ... /** * Main interface for consumer */ @deprecated("This trait has been deprecated and will be removed in a future release.", "0.11.0.0") trait ConsumerConnector { ... def createMessageStreams[K,V](topicCountMap: Map[String,Int], keyDecoder: Decoder[K], valueDecoder: Decoder[V]): Map[String,List[KafkaStream[K,V]]] ... } 已在新API中折旧。

Client API

我查找了新的API并找到了两个候选人:

    org.apache.kafka.clients.consumer 中的
  • Stream API org.apache.kafka.streams中的
  • var container = document.getElementById('container'); var span1 = document.getElementById('span1'); var span2 = document.getElementById('span2'); var output = document.getElementById('output'); function average() { var a = parseFloat(document.getElementById('a').value); var b = parseFloat(document.getElementById('b').value); var c = parseFloat(document.getElementById('c').value); var d = parseFloat(document.getElementById('d').value); if (document.getElementById('select1').value == '1') { span1.innerHTML = ((a+b)/(a*b)).toFixed(2); } else if (document.getElementById('select1').value == '2') { span1.innerHTML = ((a*b)/(a+b)).toFixed(2) } else { span1.innerHTML = ''; } if (isNaN(span1.innerHTML)) { span1.innerHTML = ''; } if (document.getElementById('select1').value == 'none1') { span1.innerHTML = 'None' } if (document.getElementById('select2').value == '3') { span2.innerHTML = ((c+d)*100/(c*d)).toFixed(2); } else if (document.getElementById('select2').value == '4') { span2.innerHTML = ((c*d)*100/(c+d)).toFixed(2) } else { span2.innerHTML = ''; } if (isNaN(span2.innerHTML)) { span2.innerHTML = ''; } if (document.getElementById('select2').value == 'none2') { span2.innerHTML = 'None' } var percent = document.getElementsByClassName('percent'); for (var i = 0; percent.length > i; i++) { if (percent.length > 0) { output.innerHTML = percent[i]/(percent.length); } } } container.addEventListener('change', average); container.addEventListener('input', average);

我应该使用哪一个?而且,我如何在新的Kafka API中实现相同的目标?

1 个答案:

答案 0 :(得分:1)

新消费者的一个例子如下:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test");
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<>(props);
consumer.subscribe(Arrays.asList("foo", "bar"));
while (true) {
    ConsumerRecords<String, String> records = consumer.poll(100);
    for (ConsumerRecord<String, String> record : records)
        System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
}

有关详细信息,请参阅https://kafka.apache.org/0110/javadoc/index.html