Kafka学习之consumer端部署及API

1、consumer.properties:文件位于/resources目录下

zookeeper.connect=192.168.0.1:2181test-datacenter/test-server
# timeout in ms for connecting to zookeeper
zookeeper.connectiontimeout.ms=1000000
#consumer group id
group.id=test-group
#consumer timeout
#consumer.timeout.ms=5000

2、JAVA API实现

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
 
import kafka.consumer.*;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.message.MessageAndMetadata;
 
import org.apache.commons.collections.CollectionUtils;
 

public class kafkaConsumer {
 
  public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {
 
    Properties properties = new Properties();
    properties.put("zookeeper.connect", "192.168.0.1:2181/test-datacenter/test-server");
    properties.put("auto.commit.enable", "true");
    properties.put("auto.commit.interval.ms", "60000");
    properties.put("group.id", "test");
 
    ConsumerConfig consumerConfig = new ConsumerConfig(properties);
 
    ConsumerConnector javaConsumerConnector = Consumer.createJavaConsumerConnector(consumerConfig);
 
    //topic的过滤器
    Whitelist whitelist = new Whitelist("test");
    List<KafkaStream<byte[], byte[]>> partitions = javaConsumerConnector.createMessageStreamsByFilter(whitelist);
 
    if (CollectionUtils.isEmpty(partitions)) {
      System.out.println("empty!");
      TimeUnit.SECONDS.sleep(1);
    }
 
    //消费消息
    for (KafkaStream<byte[], byte[]> partition : partitions) {
 
      ConsumerIterator<byte[], byte[]> iterator = partition.iterator();
      while (iterator.hasNext()) {
        MessageAndMetadata<byte[], byte[]> next = iterator.next();
        System.out.println("partiton:" + next.partition());
        System.out.println("offset:" + next.offset());
        System.out.println("message:" + new String(next.message(), "utf-8"));
      }
    } 
  }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值