package test_kafka;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger; import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.ConsumerTimeoutException;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector; public class KafkaConsumerApi { public ConsumerConnector consumer;
private AtomicInteger sendCount; public Properties initProperties(String broker, String groupId) {
Properties properties = new Properties();
properties.put("zookeeper.connect", broker);
properties.put("group.id", groupId);
properties.put("auto.offset.reset", "smallest");
properties.put("auto.commit.enable", "false");
properties.put("zookeeper.session.timeout.ms", "4000");
properties.put("zookeeper.sync.time.ms", "200");
properties.put("consumer.timeout.ms", "10000");
return properties;
} public static void main(String[] args) throws Exception {
new KafkaConsumerApi().start();
} public void start() {
String topic = "test_010_kafka";
String groupId = "test3";
Properties properties = initProperties("192.168.137.131:2181", groupId);
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(1));
ConsumerConfig config = new ConsumerConfig(properties);
sendCount = new AtomicInteger(0);
while (true) {
System.out.println("start to consumer Messages.");
List<byte[]> bytes = new ArrayList<byte[]>();
try {
consumer = Consumer.createJavaConsumerConnector(config);
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer
.createMessageStreams(topicCountMap);
KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0);
ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (true) {
try {
while (it.hasNext()) {
bytes.add(it.next().message());
if (bytes.size() >= 8) {
submitMessage(bytes);
}
}
} catch (ConsumerTimeoutException e) {
System.out.println("-------------------------ConsumerTimeoutException---------------------------");
if (bytes.size() > 0) {
submitMessage(bytes);
}
}
}
} catch (Exception e) {
e.printStackTrace();
sleepSeconds(60);
}
}
} public void submitMessage(List<byte[]> bytes) {
int size = bytes.size();
if (exceute(bytes)) {
consumer.commitOffsets();
System.out.println("consumer message size:" + size + ", and consumer total count:" + sendCount.addAndGet(size));
bytes.clear();
}
} public boolean exceute(List<byte[]> bytes) {
for (byte[] str : bytes) {
System.out.println(new String(str));
}
return true;
} public void sleepSeconds(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }

kafka consumer 0.8.2.1示例代码的更多相关文章

  1. RSS介绍、RSS 2.0规范说明和示例代码

    RSS是一种消息来源格式规范,用以发布经常更新资料的网站,例如博客.新闻的网摘.RSS文件,又称做摘要.网摘.更新.频道等,包含了全文或节选文字,再加上一定的属性数据.RSS让发布者自动发布信息,也使 ...

  2. kafka producer 0.8.2.1 示例

    package test_kafka; import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; i ...

  3. SkylineGlobe 7.0版本 矢量数据查询示例代码

    在Pro7.0.0和7.0.1环境下测试可用. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  4. kafka consumer 代码示例

    使用者小组 使得许多进程的多台机器 在逻辑上作为一个单个的使用者 出现. 我们使用中,一种常见的情况是,我们按照逻辑划分出多个使用者小组,每个小组都是有作为一个逻辑整体的多台使用者计算机组成的集群. ...

  5. kafka consumer代码梳理

    kafka consumer是一个单纯的单线程程序,因此相对于producer会更好理解些.阅读consumer代码的关键是理解回调,因为consumer中使用了大量的回调函数.参看kafka中的回调 ...

  6. SkylineGlobe TerraExplorer Pro 7.0 Web 控件版 第一行示例代码

    SkylineGlobe TerraExplorer Pro 7.0 是原生的64位应用程序,在Web端用插件方式开发的第一行示例代码如下: 常规代码,需要IE64位: <!DOCTYPE ht ...

  7. kafka的Java客户端示例代码(kafka_2.11-0.8.2.2)

    1.使用Producer API发送消息到Kafka 从版本0.9开始被KafkaProducer替代. HelloWorldProducer.java package cn.ljh.kafka.ka ...

  8. iPhone调用ffmpeg2.0.2解码h264视频的示例代码

    iPhone调用ffmpeg2.0.2解码h264视频的示例代码 h264demo.zip 关于怎么在MAC下编译iOS下的ffmpeg请看 编译最新ffmpeg2.0.1(ffmpeg2.0.2)到 ...

  9. kafka 官方示例代码--消费者

    kafka 0.9.0添加了一套新的Java 消费者API,用以替换之前的high-level API (基于ZK) 和low-level API.新的Java消费者API目前为测试版.另外kafka ...

随机推荐

  1. 国内环境安装k8s

    环境准备 1. 配置/etc/hosts文件,将所有机器配置成通过主机名可以访问. 2. 如果环境中有代理,请一定要在环境变量中将no_proxy配置正确. 3.  master还需要执行下面的命令 ...

  2. vue页面优化中的v-show和v-if使用比较

    在页面中使用了v-if做了一个tab框,点击不同的tab框,并加载不同的内容,由于各tab框对应的内容是4到5张统计图,加载的数据量比较大,发现后台请求响应返回的时间很快,在100ms以内,但点击ta ...

  3. Lintcode489-Convert Array List to Linked List-Easy

    489. Convert Array List to Linked List Convert an array list to a linked list. Example Example 1: In ...

  4. Android虹软人脸识别sdk使用工具类

    public class FaceUtil { private static final String TAG = FaceUtil.class.getSimpleName(); private st ...

  5. C#防盗链处理类的代码

    如下的内容是关于C#防盗链处理类的内容. public class FileHandler:IHttpHandler{public FileHandler(){} public void Proces ...

  6. 登录获取token,token参数关联至所有请求的请求体内

    问题描述: 有些系统接口判断用户是否登录,是校验登录接口成功后传的token值,也就是请求系统所有接口时,前端传参必带登录成功后接口返回的token,后台以此检验是否过期或是否有登录.所有接口都依赖登 ...

  7. php 两次encodeURI,解决浏览器跳转请求页乱码报错找不到页面的bug

    Not Found The requested URL /index.php/XXX/mid/97329240798095910/bname/3000T/D/sname/水泥粉磨/un ...

  8. linux基础命令touch

    touch /home/ceshi    创建文件命令 touch    /home/ceshi1 ceshi2    同时创建两个文件. cat   显示文件内容(短的文件) -n  带行号 tac ...

  9. mysql学习之check无效的解决及触发器的使用

    SQL的约束种类: 一.非空约束 not null 二.唯一约束 unique 三.主键约束 四.外键约束 五.check约束 该约束可用于列之间检查语义限制的,实际应用过程中非常常用!! 然鹅,My ...

  10. 数模美赛准备——我的第一个LaTex文档

    软件下载地址:清华大学镜像网站(点击下载) https://mirrors.tuna.tsinghua.edu.cn/ctex/legacy/2.9/ 上述网站页面 根据个人需求下载不同的版本安装完成 ...