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

@[TOC]kafka1. 开启zookeeper服务在 zk目录下执行 bin/zhServer.sh start , 再在kafka目录下开启服务 执行 bin/kafka-server-start.sh -daemon config/server0.properties (最后的文件名如果更改自行选择)
2. jps 命令查看服务是否开启
服务信息
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<String, String> producer = new Producer<String, String>(new ProducerConfig(prop));
        int i = 0;
        while (true) {
            producer.send(new KeyedMessage<String, String>("此处填写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<String,String> consu = new KafkaConsumer<String,String>(props);
        Collection<String> topics = Arrays.asList("topic名字");
        //消费者订阅topic
        consu.subscribe(topics);
        ConsumerRecords<String,String> consumerRecords = null;
        while(true){
            //接下来就要从topic中拉去数据
            consumerRecords = consu.poll(1000);
            //遍历每一条记录
            for(ConsumerRecord<String, String> 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);
            }
        }
    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值