漫游kafka实战篇之搭建Kafka开发环境
- <dependency>
- <groupId> org.apache.kafka</groupId >
- <artifactId> kafka_2.10</artifactId >
- <version> 0.8.0</ version>
- </dependency>
- package com.sohu.kafkademon;
- public interface KafkaProperties
- {
- final static String zkConnect = "10.22.10.139:2181";
- final static String groupId = "group1";
- final static String topic = "topic1";
- final static String kafkaServerURL = "10.22.10.139";
- final static int kafkaServerPort = 9092;
- final static int kafkaProducerBufferSize = 64 * 1024;
- final static int connectionTimeOut = 20000;
- final static int reconnectInterval = 10000;
- final static String topic2 = "topic2";
- final static String topic3 = "topic3";
- final static String clientId = "SimpleConsumerDemoClient";
- }
- package com.sohu.kafkademon;
- import java.util.Properties;
- import kafka.producer.KeyedMessage;
- import kafka.producer.ProducerConfig;
- /**
- * @author leicui bourne_cui@163.com
- */
- public class KafkaProducer extends Thread
- {
- private final kafka.javaapi.producer.Producer<Integer, String> producer;
- private final String topic;
- private final Properties props = new Properties();
- public KafkaProducer(String topic)
- {
- props.put("serializer.class", "kafka.serializer.StringEncoder");
- props.put("metadata.broker.list", "10.22.10.139:9092");
- producer = new kafka.javaapi.producer.Producer<Integer, String>(new ProducerConfig(props));
- this.topic = topic;
- }
- @Override
- public void run() {
- int messageNo = 1;
- while (true)
- {
- String messageStr = new String("Message_" + messageNo);
- System.out.println("Send:" + messageStr);
- producer.send(new KeyedMessage<Integer, String>(topic, messageStr));
- messageNo++;
- try {
- sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- package com.sohu.kafkademon;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import kafka.consumer.ConsumerConfig;
- import kafka.consumer.ConsumerIterator;
- import kafka.consumer.KafkaStream;
- import kafka.javaapi.consumer.ConsumerConnector;
- /**
- * @author leicui bourne_cui@163.com
- */
- public class KafkaConsumer extends Thread
- {
- private final ConsumerConnector consumer;
- private final String topic;
- public KafkaConsumer(String topic)
- {
- consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
- createConsumerConfig());
- this.topic = topic;
- }
- private static ConsumerConfig createConsumerConfig()
- {
- Properties props = new Properties();
- props.put("zookeeper.connect", KafkaProperties.zkConnect);
- props.put("group.id", KafkaProperties.groupId);
- props.put("zookeeper.session.timeout.ms", "40000");
- props.put("zookeeper.sync.time.ms", "200");
- props.put("auto.commit.interval.ms", "1000");
- return new ConsumerConfig(props);
- }
- @Override
- public void run() {
- Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
- topicCountMap.put(topic, new Integer(1));
- 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 (it.hasNext()) {
- System.out.println("receive:" + new String(it.next().message()));
- try {
- sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- package com.sohu.kafkademon;
- /**
- * @author leicui bourne_cui@163.com
- */
- public class KafkaConsumerProducerDemo
- {
- public static void main(String[] args)
- {
- KafkaProducer producerThread = new KafkaProducer(KafkaProperties.topic);
- producerThread.start();
- KafkaConsumer consumerThread = new KafkaConsumer(KafkaProperties.topic);
- consumerThread.start();
- }
- }
- package com.sohu.kafkademon;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import kafka.consumer.ConsumerConfig;
- import kafka.consumer.ConsumerIterator;
- import kafka.consumer.KafkaStream;
- import kafka.javaapi.consumer.ConsumerConnector;
- /**
- * @author leicui bourne_cui@163.com
- */
- public class KafkaConsumer extends Thread
- {
- private final ConsumerConnector consumer;
- private final String topic;
- public KafkaConsumer(String topic)
- {
- consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
- createConsumerConfig());
- this.topic = topic;
- }
- private static ConsumerConfig createConsumerConfig()
- {
- Properties props = new Properties();
- props.put("zookeeper.connect", KafkaProperties.zkConnect);
- props.put("group.id", KafkaProperties.groupId);
- props.put("zookeeper.session.timeout.ms", "40000");
- props.put("zookeeper.sync.time.ms", "200");
- props.put("auto.commit.interval.ms", "1000");
- return new ConsumerConfig(props);
- }
- @Override
- public void run() {
- Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
- topicCountMap.put(topic, new Integer(1));
- 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 (it.hasNext()) {
- System.out.println("receive:" + new String(it.next().message()));
- try {
- sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
漫游kafka实战篇之搭建Kafka开发环境的更多相关文章
- 漫游kafka实战篇之搭建Kafka开发环境(3)
上篇文章中我们搭建了kafka的服务器,并可以使用Kafka的命令行工具创建topic,发送和接收消息.下面我们来搭建kafka的开发环境. 添加依赖 搭建开发环境需要引入kafka的jar包 ...
- 漫游Kafka实战篇之搭建Kafka运行环境
接下来一步一步搭建Kafka运行环境. Step 1: 下载Kafka 点击下载最新的版本并解压. > tar -xzf kafka_2.9.2-0.8.1.1.tgz > cd kafk ...
- 漫游Kafka实战篇之搭建Kafka运行环境(2)
接下来一步一步搭建Kafka运行环境. Step 1: 下载Kafka 点击下载最新的版本并解压. > tar -xzf kafka_2.9.2-0.8.1.1.tgz > cd kafk ...
- 2017.2.20 activiti实战--第二章--搭建Activiti开发环境及简单示例(二)简单示例
学习资料:<Activiti实战> 第一章 搭建Activiti开发环境及简单示例 2.5 简单流程图及其执行过程 (1)leave.bpmn 后缀名必须是bpmn.安装了activiti ...
- activiti实战--第二章--搭建Activiti开发环境及简单示例
(一)搭建开发环境 学习资料:<Activiti实战> 第一章 认识Activiti 2.1 下载Activiti 官网:http://activiti.org/download.html ...
- 你必须知道的指针基础-1.预备篇:搭建GCC开发环境
一.关于GCC编译器 GCC(GNU Compiler Collection)是一套功能强大.性能优越的编程语言编译器,它是GNU计划的代表作品之一.GCC是Linux平台下最常用的编译器,GCC原名 ...
- 2017.2.20 activiti实战--第二章--搭建Activiti开发环境及简单示例(一)搭建开发环境
学习资料:<Activiti实战> 第一章 认识Activiti 2.1 下载Activiti 官网:http://activiti.org/download.html 进入下载页后,可以 ...
- vue第一篇(搭建vue开发环境)
1.下载node并安装 下载地址: https://nodejs.org/zh-cn/ 下载后双击文件安装 2.检查是否安装成功 node -v v10.16.0 npm -v 6.9.0 如果能正常 ...
- Visual Studio搭建Python开发环境
一.搭建开发环境 1.创建工程: 2.下载环境: 创建好工作以后,点击运行,就会出现下面这个界面,然后点击下载,并安装 http://jingyan.baidu.com/article/fec4bce ...
随机推荐
- Appstore提交 被拒绝
Reasons 16.1: Apps that present excessively objectionable or crude content will be rejected 16.1 We ...
- Sqli-labs less 27a
Less-27a 本关与27关的区别在于对于id的处理,这里用的是 " ,同时mysql的错误不会在前端页面显示. 我们根据27关直接给出一个示例payload: http://127.0. ...
- PHP 使用 Redis
PHP 使用 Redis 安装 开始在 PHP 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 PHP redis 驱动,且你的机器上能正常使用 PHP. 接下来让我们安装 PH ...
- ***mysql索引总结----mysql索引类型以及创建
文章归属:http://feiyan.info/16.html,我想自己去写了,但是发现此君总结的非常详细.直接搬过来了 关于MySQL索引的好处,如果正确合理设计并且使用索引的MySQL是一辆兰博基 ...
- Node.js缓冲模块Buffer
前言 Javascript是为浏览器而设计的,能很好的处理unicode编码的字符串,但对于二进制或非unicode编码的数据就显得无能为力. Node.js继承Javascript的语言特性,同时又 ...
- hdu1715
http://acm.hdu.edu.cn/showproblem.php?pid=1715 模板大数: #include <stdio.h> #include <string.h& ...
- 李洪强iOS开发之添加手势
李洪强iOS开发之添加手势 02 - 添加手势
- 绑定CPU
处理器的亲和性 软亲和性(affinity) 意味着进程并不会在处理器之间频繁迁移,而 硬亲和性(affinity) 则意味着进程需要在您指定的处理器上运行. 通常 Linux 内核都可以很好地对进程 ...
- hdu 3389 Game (阶梯博弈)
#include<stdio.h> int main() { int t,n,ans; int i,j,x; scanf("%d",&t); ;j<=t; ...
- 安卓Intent.ACTION_TIME_TICK 广播
Intent.ACTION_TIME_TICK 广播需要动态注册,不能在清单文件配置. TimeReceiver mBroadcastReceiver = new TimeReceiver(); In ...