转载自:http://blog.csdn.net/qq_26479655/article/details/52555283

首先导入包

  1. 将kafka目录下的libs中的jar包导入
  2. 用maven建立

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka-clients</artifactId>
  4. <version>0.10.0.1</version>
  5. </dependency>

写好properties配置文件 
一下为项目结构 


  1. #kafka集群地址
  2. bootstrap.servers = 192.168.168.200:9092
  3. client.id = testProducer
  4. key.serializer = org.apache.kafka.common.serialization.IntegerSerializer
  5. value.serializer = org.apache.kafka.common.serialization.StringSerializer

然后上代码


  1. package kafka.producer;
  2. import java.io.IOException;
  3. import java.util.Properties;
  4. import java.util.concurrent.ExecutionException;
  5. import org.apache.kafka.clients.producer.Callback;
  6. import org.apache.kafka.clients.producer.KafkaProducer;
  7. import org.apache.kafka.clients.producer.ProducerRecord;
  8. import org.apache.kafka.clients.producer.RecordMetadata;
  9. public class ProducerTest extends Thread{
  10. private final KafkaProducer<Integer, String> producer;
  11. private final String topic;
  12. private final Boolean isAsync;
  13. /*isAsync同步、异步*/
  14. public ProducerTest(String topic, Boolean isAsync) {
  15. Properties properties = new Properties();
  16. /*加载配置文件*/
  17. try {
  18. properties.load(ProducerTest.class.getClassLoader().getResourceAsStream("conf/kafka.producer.properties"));
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. producer = new KafkaProducer<>(properties);
  23. this.topic = topic;
  24. this.isAsync = isAsync;
  25. }
  26. public void run() {
  27. int messageNo = 1;
  28. while (true) {
  29. String messageStr = "Message_" + messageNo;
  30. long startTime = System.currentTimeMillis();
  31. if (isAsync) { // Send asynchronously
  32. producer.send(new ProducerRecord<>(topic,
  33. messageNo,
  34. messageStr), new DemoCallBack(startTime, messageNo, messageStr));
  35. } else { // Send synchronously
  36. try {
  37. producer.send(new ProducerRecord<>(topic,
  38. messageNo,
  39. messageStr)).get();
  40. System.out.println("Sent message: (" + messageNo + ", " + messageStr + ")");
  41. } catch (InterruptedException | ExecutionException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. ++messageNo;
  46. }
  47. }
  48. }
  49. class DemoCallBack implements Callback {
  50. private final long startTime;
  51. private final int key;
  52. private final String message;
  53. public DemoCallBack(long startTime, int key, String message) {
  54. this.startTime = startTime;
  55. this.key = key;
  56. this.message = message;
  57. }
  58. /**
  59. * @param metadata  The metadata for the record that was sent (i.e. the partition and offset). Null if an error
  60. *                  occurred.
  61. * @param exception The exception thrown during processing of this record. Null if no error occurred.
  62. */
  63. public void onCompletion(RecordMetadata metadata, Exception exception) {
  64. long elapsedTime = System.currentTimeMillis() - startTime;
  65. if (metadata != null) {
  66. System.out.println(
  67. "message(" + key + ", " + message + ") sent to partition(" + metadata.partition() +
  68. "), " +
  69. "offset(" + metadata.offset() + ") in " + elapsedTime + " ms");
  70. } else {
  71. exception.printStackTrace();
  72. }
  73. }
  74. }

测试代码


  1. package kafka.producer;
  2. public class Main {
  3. public static void main(String[] args) {
  4. ProducerTest test = new ProducerTest("TestTopic", true);
  5. test.start();
  6. }
  7. }

运行结果


  1. message(51215, Message_51215) sent to partition(0), offset(161830) in 3497 ms
  2. message(51216, Message_51216) sent to partition(0), offset(161831) in 3497 ms
  3. message(51217, Message_51217) sent to partition(0), offset(161832) in 3497 ms
  4. message(51218, Message_51218) sent to partition(0), offset(161833) in 3497 ms
  5. message(51219, Message_51219) sent to partition(0), offset(161834) in 3497 ms
  6. message(51220, Message_51220) sent to partition(0), offset(161835) in 3497 ms
  7. message(51221, Message_51221) sent to partition(0), offset(161836) in 3497 ms
  8. message(51222, Message_51222) sent to partition(0), offset(161837) in 3497 ms
  9. message(51223, Message_51223) sent to partition(0), offset(161838) in 3497 ms
  10. message(51224, Message_51224) sent to partition(0), offset(161839) in 3497 ms
  11. message(51225, Message_51225) sent to partition(0), offset(161840) in 3497 ms
  12. message(51226, Message_51226) sent to partition(0), offset(161841) in 3497 ms
  13. message(51227, Message_51227) sent to partition(0), offset(161842) in 3497 ms
  14. message(51228, Message_51228) sent to partition(0), offset(161843) in 3497 ms
  15. .............​

kafka_2.11-0.10.0.1生产者producer的Java实现的更多相关文章

  1. kafka 0.10.2 消息生产者(producer)

    package cn.xiaojf.kafka.producer; import org.apache.kafka.clients.producer.*; import org.apache.kafk ...

  2. Kafka: Producer (0.10.0.0)

    转自:http://www.cnblogs.com/f1194361820/p/6048429.html 通过前面的架构简述,知道了Producer是用来产生消息记录,并将消息以异步的方式发送给指定的 ...

  3. kafka0.9.0及0.10.0配置属性

    问题导读1.borker包含哪些属性?2.Producer包含哪些属性?3.Consumer如何配置?borker(0.9.0及0.10.0)配置Kafka日志本身是由多个日志段组成(log segm ...

  4. kafka_2.11-0.8.2.1生产者producer的Java实现

    转载自:http://blog.csdn.net/ch717828/article/details/50818261 1. 开启Kafka Consumer 首先选择集群的一台机器,打开kafka c ...

  5. Kafka版本升级 ( 0.10.0 -> 0.10.2 )

    升级Kafka集群的版本其实很简单,核心步骤只需要4步,但是我们需要在升级的过程中确保每一步操作都不会“打扰”到producer和consumer的正常运转.为此,笔者在本机搭了一个测试环境进行实际的 ...

  6. Kafka 0.10.0

    2.1 Producer API We encourage all new development to use the new Java producer. This client is produ ...

  7. kafka 0.8.2 消息生产者 producer

    package com.hashleaf.kafka; import java.util.Properties; import kafka.javaapi.producer.Producer; imp ...

  8. hive 0.10 0.11新增特性综述

    我们的hive版本升迁经历了0.7.1 -> 0.8.1 -> 0.9.0,并且线上shark所依赖的hive版本也停留在0.9.0上,在这些版本上有我们自己的bug fix patch和 ...

  9. kafka 0.10.2 消息生产者

    package cn.xiaojf.kafka.producer; import org.apache.kafka.clients.producer.KafkaProducer; import org ...

随机推荐

  1. IE9中input事件与异步事件连用会发生跨域问题

    IE版本中IE8以及IE9以上版本不会存在这个问题唯独IE9 发生跨域问题代码 $("#stock_code").bind("input",function(e ...

  2. SQL注入之Sqli-labs系列第十八关(基于错误的用户代理,头部POST注入)

    开始挑战第十八关(Header Injection - Uagent field - Error based) 常见的HTTP注入点产生位置为[Referer].[X-Forwarded-For].[ ...

  3. Nginx 浏览器打开是下载状态

    location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_inf ...

  4. Python学习笔记第十周

    目录: 一.基础概念 1.多进程 2.进程间通信 3.进程锁 4.进程池 5.协程 a) greenlet b) Gevent 6.论事件驱动与异步IO 7.IO多路复用 8.Python Selec ...

  5. 又一个opengl教程,多多益善

    http://ogldev.atspace.co.uk/index.html http://wiki.jikexueyuan.com/project/modern-opengl-tutorial/tu ...

  6. Jquery的deferred对象,看这2篇牛人的文章,基本就够了。

    http://blog.csdn.net/ligang2585116/article/details/51589073 http://www.ruanyifeng.com/blog/2011/08/a ...

  7. 软件安装配置笔记(一)——Oracle及PLSQL Developer的安装与配置

    一.Oracle: Oracle服务器端或桌面端可以创建本地的Oracle数据库,而Oracle客户端是用来远程连接其他服务器或电脑上的Oracle服务器端或桌面端的,安装客户端软件只需配置网络连接文 ...

  8. 20155219 2016-2017-2 《Java程序设计》第9周学习总结

    20155219 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC入门 JDBC简介 1.JDBC是java联机数据库的标准规范,它定义了一组标准类与 ...

  9. 更换JDK版本时的问题:Error: could not open `C:\Java\jre7\lib\amd64\jvm.cfg'

    1.先把oracle自带的weblogic给卸载了,然后打开eclipse,发现报错了:Error: could not open `C:\Java\jre7\lib\amd64\jvm.cfg' J ...

  10. 硬盘安装Linux(ubuntu,centos)

    硬盘安装Linux 使用硬盘安装Linux最大的好处不只是方便,是快速.之前使用U盘安装,很慢,没有记录具体时间.Ubuntu区别不大,本身比较小,安装介质只有2G(ubuntu18.10):Cent ...