kafka消息中间件及java示例
kafka是一个消息中间件,用于各个系统之间传递消息,并且消息可持久化!
可以认为是队列模型,也可以看作是生产者消费着模型;
简单的生产者消费者客户端代码如下:
package com.pt.util.kafka; import java.util.Date;
import java.util.Properties; import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig; public class MyProducer {
public static void sendMsg(String msg) {
Properties props = new Properties();
//brokers list
props.put("metadata.broker.list", "192.168.91.231:9092,192.168.91.231:9093");
/* *
* the serializer when preparing the message for transmission to the Broker
* Note that the encoder must accept the same type
* as defined in the KeyedMessage object in the next step.
*
*/
props.put("serializer.class", "kafka.serializer.StringEncoder");
/* *
* defines what class to use to determine
* which Partition in the Topic the message is to be sent to
*/
props.put("partitioner.class", "example.producer.SimplePartitioner");
/* *
* tells Kafka that you want your Producer to require an
* acknowledgement from the Broker that the message was received
*/
props.put("request.required.acks", "1"); ProducerConfig config = new ProducerConfig(props);
/*
* Note that the Producer is a Java Generic and you need to tell it the type of two parameters.
* The first is the type of the Partition key, the second the type of the message.
*/
Producer<String, String> producer = new Producer<String, String>(config); long runtime = new Date().getTime();
String ip = "192.168.91.231";
/*
* The “panteng” is the Topic to write to.
* Here we are passing the IP as the partition key.
* Note that if you do not include a key,
* even if you've defined a partitioner class, Kafka will assign the message to a random partition.
*/
KeyedMessage<String, String> data = new KeyedMessage<String, String>(
"panteng", ip, msg);
producer.send(data);
producer.close();
}
}
Producer,java
package cn.outofmemory.kafka; 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;
import kafka.serializer.StringDecoder;
import kafka.utils.VerifiableProperties; public class KafkaConsumer { private final ConsumerConnector consumer; public KafkaConsumer() {
Properties props = new Properties();
//zookeeper 配置
props.put("zookeeper.connect", "192.168.91.231:2181");
//group 代表一个消费组
props.put("group.id", "jd-group"); //zk连接超时
props.put("zookeeper.session.timeout.ms", "4000");
props.put("zookeeper.sync.time.ms", "200");
props.put("auto.commit.interval.ms", "1000");
props.put("auto.offset.reset", "smallest");
//序列化类
props.put("serializer.class", "kafka.serializer.StringEncoder");
ConsumerConfig config = new ConsumerConfig(props);
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(config);
} public void consume() {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put("panteng", new Integer(1)); StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties());
StringDecoder valueDecoder = new StringDecoder(new VerifiableProperties()); Map<String, List<KafkaStream<String, String>>> consumerMap =
consumer.createMessageStreams(topicCountMap,keyDecoder,valueDecoder);
KafkaStream<String, String> stream = consumerMap.get("panteng").get(0);
ConsumerIterator<String, String> it = stream.iterator();
while (it.hasNext())
System.out.println(it.next().message());
} public void stop(){
try {
consumer.shutdown();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} }
public static void main(String[] args) {
new KafkaConsumer().consume();
}
}
Consumer.java
kafka消息中间件及java示例的更多相关文章
- Kafka+Storm+HDFS 整合示例
消息通过各种方式进入到Kafka消息中间件,比如可以通过使用Flume来收集日志数据,然后在Kafka中路由暂存,然后再由实时计算程序Storm做实时分析,最后将结果保存在HDFS中,这时我们就需要将 ...
- KClient——kafka消息中间件源码解读
目录 kclient消息中间件 kclient-processor top.ninwoo.kclient.app.KClientApplication top.ninwoo.kclient.app.K ...
- 【Apache Kafka】二、Kafka安装及简单示例
(一)Apache Kafka安装 1.安装环境与前提条件 安装环境:Ubuntu16.04 前提条件: ubuntu系统下安装好jdk 1.8以上版本,正确配置环境变量 ubuntu系统下安 ...
- 课程作业01:模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。
1.设计思想: 首先是从JavaAppArguments.java示例开始,此示例已打印参数,定义数字 之和和作为存储单位的整型,然后将输入参数的字符串转化为整型,之后求和即可. 2.程序流程图: 3 ...
- 左右JAVA示例代码事件分发和监督机制来实现-绝对原创有用
文章标题:左右JAVA示例代码事件分发和监督机制来实现 文章地址: http://blog.csdn.net/5iasp/article/details/37054171 作者: javaboy201 ...
- Spark 用户自定义函数 Java 示例
Spark UDF Java 示例 在这篇文章中提到了用Spark做用户昵称文本聚类分析,聚类需要选定K个中心点,然后迭代计算其他样本点到中心点的距离.由于中文文字分词之后(n-gram)再加上昵称允 ...
- FATAL Fatal error during KafkaServerStable startup. Prepare to shutdown (kafka.server.KafkaServerStartable) java.io.FileNotFoundException: /tmp/kafka-logs/.lock (Permission denied)
1.启动kafka的时候,报错如下所示: [-- ::,] INFO zookeeper state changed (SyncConnected) (org.I0Itec.zkclient.ZkCl ...
- kafka学习之-java api测试
1.配置 package com.storm.storm_my.kafka; /** * * @author Peng.Li * */ public class KafkaConfigApiConst ...
- kafka启动报java.net.UnknownHostException
kafka启动报java.net.UnknownHostException 参考资料: 1.https://blog.csdn.net/zdxiq000/article/details/6258765 ...
随机推荐
- 【3】JavaScript编程全解笔记(三)
减少重复劳动,抓住核心. 第 4 部分 HTML5 1. HTML 技术分类 与 API 2. ApplicationCache 缓存 第 15 章 与桌面应用的协作 第 17 章 WebSocket ...
- 构建maven的web项目时注意的问题
构建项目后或者导入项目后,我们需要bulid path--->config build path 特别是maven的依赖一定要 发布到WEB_INF的lib下面,不然在发布项目的时候,这些依赖都 ...
- centos 设置删除提示
centos 设置删除.覆盖提示 相信好多用linux的同学都用过cp这个命令来覆盖文件,但是新建的普通用户不会有任何提示,这时候心里就咯噔一下,赶紧看看新文件的是不是传上去了.如果加上提示,是不是就 ...
- Linux 查公网出口IP
wget http://members.3322.org/dyndns/getipcat getip
- centos 安装 ntpdate 并同步时间
1.安装ntp yum install -y ntp 2.与一个已知的时间服务器同步 # time.nist.gov 是一个时间服务器 ntpdate time.nist.gov 3.删除本地时间并设 ...
- Location-aware Associated Data Placement for Geo-distributed Data-intensive Applications--INFOCOM 2015
[标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption future work [相关方法或论文] [重点提示] [其它]
- RabbitMQ队列
AMQP ,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的解耦,消息的发送者无 ...
- SuperSocket源码解析之开篇 (转)
一 简介 官方介绍:SuperSocket 是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架.你无须了解如何使用 Socket, 如何维护 Socket 连接和 S ...
- EasyUI 日期选择插件封装成选择到月份的插件
将普通的日期选择插件封装成选择到月份的插件: var nowMonth = new Date(); var month = ...
- jqGrid使用整理
jqGrid使用整理 jqGrid是一款处理表格展现的jQuery插件,支持分页.滚动加载.搜索.锁定.拖动等一系列对表格的常规操作.以下是最近项目中实践jqGrid的整理 1.引入到项目中来 jqG ...