kafka_2.11-0.10.0.1生产者producer的Java实现
转载自:http://blog.csdn.net/qq_26479655/article/details/52555283
首先导入包
- 将kafka目录下的
libs
中的jar包导入 - 用maven建立
- <dependency>
- <groupId>org.apache.kafka</groupId>
- <artifactId>kafka-clients</artifactId>
- <version>0.10.0.1</version>
- </dependency>
写好properties配置文件
一下为项目结构
- #kafka集群地址
- bootstrap.servers = 192.168.168.200:9092
- client.id = testProducer
- key.serializer = org.apache.kafka.common.serialization.IntegerSerializer
- value.serializer = org.apache.kafka.common.serialization.StringSerializer
然后上代码
- package kafka.producer;
- import java.io.IOException;
- import java.util.Properties;
- import java.util.concurrent.ExecutionException;
- import org.apache.kafka.clients.producer.Callback;
- import org.apache.kafka.clients.producer.KafkaProducer;
- import org.apache.kafka.clients.producer.ProducerRecord;
- import org.apache.kafka.clients.producer.RecordMetadata;
- public class ProducerTest extends Thread{
- private final KafkaProducer<Integer, String> producer;
- private final String topic;
- private final Boolean isAsync;
- /*isAsync同步、异步*/
- public ProducerTest(String topic, Boolean isAsync) {
- Properties properties = new Properties();
- /*加载配置文件*/
- try {
- properties.load(ProducerTest.class.getClassLoader().getResourceAsStream("conf/kafka.producer.properties"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- producer = new KafkaProducer<>(properties);
- this.topic = topic;
- this.isAsync = isAsync;
- }
- public void run() {
- int messageNo = 1;
- while (true) {
- String messageStr = "Message_" + messageNo;
- long startTime = System.currentTimeMillis();
- if (isAsync) { // Send asynchronously
- producer.send(new ProducerRecord<>(topic,
- messageNo,
- messageStr), new DemoCallBack(startTime, messageNo, messageStr));
- } else { // Send synchronously
- try {
- producer.send(new ProducerRecord<>(topic,
- messageNo,
- messageStr)).get();
- System.out.println("Sent message: (" + messageNo + ", " + messageStr + ")");
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- }
- ++messageNo;
- }
- }
- }
- class DemoCallBack implements Callback {
- private final long startTime;
- private final int key;
- private final String message;
- public DemoCallBack(long startTime, int key, String message) {
- this.startTime = startTime;
- this.key = key;
- this.message = message;
- }
- /**
- * @param metadata The metadata for the record that was sent (i.e. the partition and offset). Null if an error
- * occurred.
- * @param exception The exception thrown during processing of this record. Null if no error occurred.
- */
- public void onCompletion(RecordMetadata metadata, Exception exception) {
- long elapsedTime = System.currentTimeMillis() - startTime;
- if (metadata != null) {
- System.out.println(
- "message(" + key + ", " + message + ") sent to partition(" + metadata.partition() +
- "), " +
- "offset(" + metadata.offset() + ") in " + elapsedTime + " ms");
- } else {
- exception.printStackTrace();
- }
- }
- }
测试代码
- package kafka.producer;
- public class Main {
- public static void main(String[] args) {
- ProducerTest test = new ProducerTest("TestTopic", true);
- test.start();
- }
- }
运行结果
- message(51215, Message_51215) sent to partition(0), offset(161830) in 3497 ms
- message(51216, Message_51216) sent to partition(0), offset(161831) in 3497 ms
- message(51217, Message_51217) sent to partition(0), offset(161832) in 3497 ms
- message(51218, Message_51218) sent to partition(0), offset(161833) in 3497 ms
- message(51219, Message_51219) sent to partition(0), offset(161834) in 3497 ms
- message(51220, Message_51220) sent to partition(0), offset(161835) in 3497 ms
- message(51221, Message_51221) sent to partition(0), offset(161836) in 3497 ms
- message(51222, Message_51222) sent to partition(0), offset(161837) in 3497 ms
- message(51223, Message_51223) sent to partition(0), offset(161838) in 3497 ms
- message(51224, Message_51224) sent to partition(0), offset(161839) in 3497 ms
- message(51225, Message_51225) sent to partition(0), offset(161840) in 3497 ms
- message(51226, Message_51226) sent to partition(0), offset(161841) in 3497 ms
- message(51227, Message_51227) sent to partition(0), offset(161842) in 3497 ms
- message(51228, Message_51228) sent to partition(0), offset(161843) in 3497 ms
- .............
kafka_2.11-0.10.0.1生产者producer的Java实现的更多相关文章
- kafka 0.10.2 消息生产者(producer)
package cn.xiaojf.kafka.producer; import org.apache.kafka.clients.producer.*; import org.apache.kafk ...
- Kafka: Producer (0.10.0.0)
转自:http://www.cnblogs.com/f1194361820/p/6048429.html 通过前面的架构简述,知道了Producer是用来产生消息记录,并将消息以异步的方式发送给指定的 ...
- kafka0.9.0及0.10.0配置属性
问题导读1.borker包含哪些属性?2.Producer包含哪些属性?3.Consumer如何配置?borker(0.9.0及0.10.0)配置Kafka日志本身是由多个日志段组成(log segm ...
- kafka_2.11-0.8.2.1生产者producer的Java实现
转载自:http://blog.csdn.net/ch717828/article/details/50818261 1. 开启Kafka Consumer 首先选择集群的一台机器,打开kafka c ...
- Kafka版本升级 ( 0.10.0 -> 0.10.2 )
升级Kafka集群的版本其实很简单,核心步骤只需要4步,但是我们需要在升级的过程中确保每一步操作都不会“打扰”到producer和consumer的正常运转.为此,笔者在本机搭了一个测试环境进行实际的 ...
- Kafka 0.10.0
2.1 Producer API We encourage all new development to use the new Java producer. This client is produ ...
- kafka 0.8.2 消息生产者 producer
package com.hashleaf.kafka; import java.util.Properties; import kafka.javaapi.producer.Producer; imp ...
- hive 0.10 0.11新增特性综述
我们的hive版本升迁经历了0.7.1 -> 0.8.1 -> 0.9.0,并且线上shark所依赖的hive版本也停留在0.9.0上,在这些版本上有我们自己的bug fix patch和 ...
- kafka 0.10.2 消息生产者
package cn.xiaojf.kafka.producer; import org.apache.kafka.clients.producer.KafkaProducer; import org ...
随机推荐
- python Django rest-framework 创建序列化工程步骤
11创建项目 2创建应用 3stting添加应用(apps)-添加制定数据库-修改显示汉字(zh-hans)-上海时区(Asia/Shanghai) 4主路由添加子路由 5应用里创建子路由 6创建数据 ...
- Day14作业及默写
1.整理今天所学内容,整理知识点,整理博客. pass 2.画好流程图. pass 3.都完成的做一下作业(下面题都是用内置函数或者和匿名函数结合做出): pass 4.用map来处理字符串列表,把列 ...
- Android大作业 --音乐播放器
1.项目成员(本次作业主要对上一次的音乐播放器进行完善) 韦家城 学号:1600802026 班级:161 博客:https://www.cnblogs.com/ln9969cc/ 邓乾尧 学号:1 ...
- HDU 1796 How many integers can you find(容斥原理)
题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description ...
- 性能测试-12.Web页面性能指标与建议
1.页面加载时间 从页面开始加载到页面onload事件触发的时间.一般来说onload触发代表着直接通过HTML引用的CSS,JS,图片资源已经完全加载完毕. 2.全部页面加载时间 全部页面载入时间指 ...
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- Unity 3D Shader流水线
Unity开发VR之Vuforia 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- TS 基础数据类型
1.基础数据类型 Boolean布尔值 Number数字 String字符串 Array数组 Tuple元组 Enum枚举 Any void Boolean布尔值:true/fals ...
- HDU 1087:Super Jumping! Jumping! Jumping!(LIS)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- js的以及前端框架
js定义:浏览器的脚本语言,用简单的语言实现浏览器的操控 基础语法:基础的运算.函数.对象(原型链.对象构造.class) 所有的框架都是基于原生的js来进行的 js的事件:什么时候.什么情况下处理的 ...