kafka consumer 0.8.2.1示例代码
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示例代码的更多相关文章
- RSS介绍、RSS 2.0规范说明和示例代码
RSS是一种消息来源格式规范,用以发布经常更新资料的网站,例如博客.新闻的网摘.RSS文件,又称做摘要.网摘.更新.频道等,包含了全文或节选文字,再加上一定的属性数据.RSS让发布者自动发布信息,也使 ...
- kafka producer 0.8.2.1 示例
package test_kafka; import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; i ...
- SkylineGlobe 7.0版本 矢量数据查询示例代码
在Pro7.0.0和7.0.1环境下测试可用. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- kafka consumer 代码示例
使用者小组 使得许多进程的多台机器 在逻辑上作为一个单个的使用者 出现. 我们使用中,一种常见的情况是,我们按照逻辑划分出多个使用者小组,每个小组都是有作为一个逻辑整体的多台使用者计算机组成的集群. ...
- kafka consumer代码梳理
kafka consumer是一个单纯的单线程程序,因此相对于producer会更好理解些.阅读consumer代码的关键是理解回调,因为consumer中使用了大量的回调函数.参看kafka中的回调 ...
- SkylineGlobe TerraExplorer Pro 7.0 Web 控件版 第一行示例代码
SkylineGlobe TerraExplorer Pro 7.0 是原生的64位应用程序,在Web端用插件方式开发的第一行示例代码如下: 常规代码,需要IE64位: <!DOCTYPE ht ...
- kafka的Java客户端示例代码(kafka_2.11-0.8.2.2)
1.使用Producer API发送消息到Kafka 从版本0.9开始被KafkaProducer替代. HelloWorldProducer.java package cn.ljh.kafka.ka ...
- iPhone调用ffmpeg2.0.2解码h264视频的示例代码
iPhone调用ffmpeg2.0.2解码h264视频的示例代码 h264demo.zip 关于怎么在MAC下编译iOS下的ffmpeg请看 编译最新ffmpeg2.0.1(ffmpeg2.0.2)到 ...
- kafka 官方示例代码--消费者
kafka 0.9.0添加了一套新的Java 消费者API,用以替换之前的high-level API (基于ZK) 和low-level API.新的Java消费者API目前为测试版.另外kafka ...
随机推荐
- 《内蒙古自治区第十三届大学生程序设计竞赛试题_H 公孙玉龙》
这个题有点小坑,最坑的地方就是 输入的b 变量 有可能 是 负数 ! 负数 ! 负数! 对 ,你没有看错,就是负数,坑死我了, 一直都是 content.charAt(0) 判断 ...
- ng/cli uses yarn as the package manager
Switch to working directory Excuting the following command ng config cli.packageManager yarn
- Jmeter接口测试实例
此文章作为工作中用到的jmeter接口测试相关内容简述,方便日后查阅参考,如有理解描述有误之处,欢迎指出. 首先Jmeter环境准备网上有很多教程,在此不多做赘述: 1.接口简述 接口可理解为从客户端 ...
- windows 根据端口查看进行PID 并杀掉进程
1. 首先用netstat -ano | find “端口号”查出进程号 明明有端口号是17568和18892, 如何确定是17568呢 2. takslist 查询当前的进行 3. 如何杀死进程呢 ...
- syncbackse操作
有4类操作,包括同步,备份,镜像和组 同步是前文件夹内和后文件夹内的文件同步,使得二者的所有文件保持一致,但是也可以修改不同类型文件的覆盖操作,如果左侧有文件123.txt,右侧无123.txt文件, ...
- MPU6050可以读取器件ID值,但读出的加速度计和陀螺仪的数据均为零
今天在调试MPU6050时发现,MPU6050可以正常读取器件ID,但读取的加速度计和陀螺仪的数据均为零. 经过排查发现,MPU6050第20脚的电容没用焊接,C6可以使用10uF的电容.
- MySQL优化查询 5.7版本
1. 变更参数 : query_cache_type 如果何配置查询缓存: query_cache_type 这个系统变量控制着查询缓存工能的开启的关闭.query_cache_type=0时表示关闭 ...
- deep learning的一些知识点
softmax loss: softmax: softmax的作用,将fc的输出映射成为(0,1)的概率,并将其差距拉大. cross entropy loss: y是样本的真实标签,为1 ...
- English trip V2 - 6 Sports Teacher:Taylor Key:phrasal verbs
In this lesson you will learn to talk about sports. 课上内容(Lesson) # How many different sports can you ...
- UML与软件建模:第一次作业(用例图绘制)
一.小结 用例图是UML用于描述软件功能的图形.用例图包括用例.参与者及其关系,用例图也可以包括注释和结束. 用例图的要素: (1)参与者,即与用例存在交互关系的系统外部实体; (2)用例,用来描述个 ...