读取kafka写入另一个kafka

/**
 * Created by lmy on 2018/10/10.
 */

public class TEST {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers", "host11:9092,host13:9092");
        props.put("auto.offset.reset", "latest");
        props.put("socket.timeout.ms", "300000");
        props.put("zookeeper.session.timeout.ms", "300000");
        props.put("auto.commit.enable", "true");

       // 必须要使用别的组名称, 如果生产者和消费者都在同一组,则不能访问同一组内的topic数据
        props.put("group.id", "111");
         /* key的序列化类 */
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        /* value的序列化类 */
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        /* 定义consumer */
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
        /* 消费者订阅的topic, 可同时订阅多个topic,用逗号隔开 */
        consumer.subscribe(Arrays.asList("a3"));
        props.put("metadata.broker.list", "host58:9092,host59:9092,host60:9092");
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        ProducerConfig config = new ProducerConfig(props);
        Producer<String, String> producer = new Producer<String, String>(config);

        /* 读取数据,读取超时时间为100ms */
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(1000);
            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.value());
                KeyedMessage<String, String> message = new KeyedMessage<String, String>("a4", record.value());
                producer.send(message);
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值