java操作kafka发送消息和接收消息

<!-- java程序的Kakfa-->
    <dependency>
      <groupId>org.apache.kafka</groupId>
      <artifactId>kafka_2.10</artifactId>
      <version>0.10.0.0</version>
    </dependency>







/*消费者*/
public class KafKaConsumer {

  private final ConsumerConnector consumer;

    private KafKaConsumer() {
        Properties properties = new Properties();
        //zooKeeper配置
       properties.put("zookeeper.connect", "127.0.0.1:2181");

        //group代表一个消费组
        properties.put("group.id", "lingroup");
        //properties.put("zookeeper.sync.time.ms", "2000");
        properties.put("rebalance.max.retries", "5");
        properties.put("rebalance.backoff.ms", "1200");


        properties.put("auto.commit.interval.ms", "1000");
        properties.put("auto.offset.reset", "smallest");

        //序列化类
        properties.put("serializer.class", "kafka.serializer.StringEncoder");
        ConsumerConfig consumerConfig = new ConsumerConfig(properties);
        consumer = kafka.consumer.Consumer.createJavaConsumerConnector(consumerConfig);
    }

    void consume(){
        Map<String,Integer> topicCountMap=new HashMap<String, Integer>();
        topicCountMap.put(KafKaProducer.TOPIC,new Integer(1));
        StringDecoder keyDecoder=new StringDecoder(new VerifiableProperties());
        StringDecoder valueDecoder=new StringDecoder(new VerifiableProperties());
        Map<String,List<KafkaStream<String,String>>> consumerMap=consumer.createMessageStreams(topicCountMap,keyDecoder,valueDecoder);

         KafkaStream stream=consumerMap.get(KafKaProducer.TOPIC).get(0);
        ConsumerIterator<String,String> iterator=stream.iterator();
         while (iterator.hasNext()){


             System.out.println("接受消息>>>"+iterator.next().message());

    }

    }

  public static void main(String[] args){
      new KafKaConsumer().consume();
  }

}




生产者:

public class KafKaProducer {
    private final Producer<String,String> producer;
    public final static String TOPIC="linlin";

    private KafKaProducer(){
        Properties properties=new Properties();
        //此处设置kafka的端口
        properties.put("metadata.broker.list","127.0.0.1:9092");
       // properties.put("zk.connect","127.0.0.1:2181");
        //配置value的序列化类
        properties.put("serializer.class","kafka.serializer.StringEncoder");
        properties.put("key.serializer.class","kafka.serializer.StringEncoder");
        properties.put("request.required.acks", "-1");

        producer=new Producer<String, String>(new ProducerConfig(properties));
    }


    void produce(){
        int messageNo=1000;
        final int count=10000;
        while (messageNo<count){
            String key=String.valueOf(messageNo);
            String data ="hello kafka message"+key;
            producer.send(new KeyedMessage<String, String>(TOPIC,key,data));
            System.out.println("发送消息:"+data);
            messageNo++;
        }


    }

  public static void main(String[] args){
       new KafKaProducer().produce();
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值