Kafka的生产和消费简单代码示例

@[TOC]kafka1. 开启zookeeper服务在 zk目录下执行 bin/zhServer.sh start , 再在kafka目录下开启服务 执行 bin/kafka-server-start.sh -daemon config/server0.properties (最后的文件名如果更改自行选择)
2. jps 命令查看服务是否开启
Kafka的生产和消费简单代码示例_第1张图片
3. 操作创建topic示例,
创建示例
4. java代码生产者


public class ProducerTest {

    public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        prop.put("zookeeper.connect", "此处填写对应主机ip:2181");
        prop.put("metadata.broker.list", "localhost:9092");
        prop.put("serializer.class", StringEncoder.class.getName());
        //prop.put("request.required.acks", "1");
        Producer producer = new Producer(new ProducerConfig(prop));
        int i = 0;
        while (true) {
            producer.send(new KeyedMessage("此处填写topic名字", "msg:" + i++));
            System.out.println("发送成功");
            Thread.sleep(1000);
        }
    }


}

5.消费者:


public class kafka_consum {

    //kafka的消费者代码

    public static void main(String[] args)throws IOException {
        Properties props = new Properties();
        props.put("bootstrap.servers", "主机ip:9092");
        props.put("group.id", "kafkaconsumetest");
        // 自动确认设置
        props.put("auto-offset-reset: latest","earliest");
        // props.put("enable.auto.commit", "true");
        props.put("enable.auto.commit", "false");
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");
        Consumer consu = new KafkaConsumer(props);
        Collection topics = Arrays.asList("topic名字");
        //消费者订阅topic
        consu.subscribe(topics);
        ConsumerRecords consumerRecords = null;
        while(true){
            //接下来就要从topic中拉去数据
            consumerRecords = consu.poll(1000);
            //遍历每一条记录
            for(ConsumerRecord consumerRecord : consumerRecords){
                long offset = consumerRecord.offset();
                int partition = consumerRecord.partition();
                Object key = consumerRecord.key();
                Object value = consumerRecord.value();
                System.out.println(offset+" "+partition+" "+key+" "+value);
            }
        }
    }

}

你可能感兴趣的:(大数据,kafka)