我可以在java api中编写Kafka使用者来使用python生成者生成的消息

时间:2016-07-26 13:15:59

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

我有用python编写的生产者代码,它从twitter获取推文。我创建了名为twitter_test的主题。

当我使用kafka-console-consumer时,我可以看到该主题中有很多推文。

但是当我尝试从java使用者那里使用这些消息时,它不会获取任何数据。

以下是我的消费者代码。

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

public class avro_twitter {
 public static void main(String[] args) throws IOException {
     Properties props = new Properties();
     props.put("bootstrap.servers", "10.16.111.12:9092");
     props.put("group.id", "groupid");

     props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
     props.put("value.deserializer", "io.confluent.kafka.serializers.KafkaAvroDeserializer");
     props.put("auto.offset.reset", "earliest");
     props.put("schema.registry.url", "10.16.111.12:8081"); 
     String topic = "twitter_test";

     KafkaConsumer<String, GenericRecord> consumer = new KafkaConsumer<String, GenericRecord>(props);
     consumer.subscribe(Collections.singletonList(topic));
     System.out.println("Reading topic:" + topic);


     while (true) { 

         ConsumerRecords<String, GenericRecord> records = consumer.poll(1000); 
         for (ConsumerRecord<String, GenericRecord> record: records) {
             String authid=record.value().get(1).toString();
             String screen_name=record.value().get(1).toString();
             String description=record.value().get(2).toString();     


             System.out.println(authid);

         }   
         }
     }

任何帮助都会非常感激

0 个答案:

没有答案