kafka 学习笔记

来源:互联网 发布:mysql 日期截断 编辑:程序博客网 时间:2024/05/01 23:31

1、用Kafka发消息

 
        Properties props=new Properties();        props.put("metadata.broker.list", "kafka服务IP(集群的话,可以是很多以逗号分隔的IP)");        props.put("serializer.class", "kafka.serializer.StringEncoder");        props.put("request.required.acks","1");//        ProducerConfig config=new ProducerConfig(props);//生产者配置器        KeyedMessage<String,String> message=new KeyedMessage<String,String>("队列的名称","标识(可以喝队列名称一样)","需要发的消息");         List<KeyedMessage<String,String>> messages=new ArrayList<KeyedMessage<String,String>>();        messages.add(message);//需要发送的消息列表        Producer<String,String> producer=new Producer<String,String>(config);//创建生产者        producer.send(messages);//批量发消息        producer.close();        messages.clear(); 

2、用户kafka收消息

    a)实现IConsumer接口 

public class SingleMessageConsumer implements IConsumer { @Overridepublic Boolean doneMessage(byte[] bytes) {Boolean flag = false;   System.out.println("-----接收到的消息为:-------"+new String(bytes));return flag;}}

    Properties props = new Properties();    props.put("zookeeper.connect","zookepper服务器IP(集群的话,可以是很多以逗号分隔的IP));    props.put("group.id", ReadProperties.bundle.getString("groupId"));    props.put("zookeeper.session.timeout.ms", "5000");    props.put("zookeeper.sync.time.ms", "2000");    props.put("zookeeper.connection.timeout.ms","10000");    props.put("auto.commit.interval.ms", "1000");    ConsumerConfig config=new ConsumerConfig(props);//消费者配置器    ConsumerConnector consumerConnector= kafka.consumer.Consumer.createJavaConsumerConnector(config);    Map<String, Integer> topicCountMap = new HashMap<String, Integer>();    topicCountMap.put(topic, new Integer(1));    Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumerConnector.createMessageStreams(topicCountMap);    KafkaStream<byte[], byte[]> stream =  consumerMap.get(topic).get(0);    ConsumerIterator<byte[], byte[]> it = stream.iterator();    SingleMessageConsumer consumer=new SingleMessageConsumer();    while(it.hasNext())      consumer.doneMessage(it.next().message());  }    




0 0