package com.hashleaf.kafka;

import java.util.Properties;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig; /**
* 消息生产者
* @author xiaojf 294825811@qq.com
* @since 2015-7-15 下午10:50:01
*/
public class MyProducer {
//发送消息的topic
public static final String HASHLEAF_KAFKA_TOPIC = "hashleaf_topic";
private final Producer<String, String> producer; public MyProducer() {
Properties props = new Properties();
//指定代理服务器的地址
props.put("metadata.broker.list", "192.168.66.2:9092,192.168.66.3:9092,192.168.66.4:9092"); //配置value的序列化类
props.put("serializer.class", "kafka.serializer.StringEncoder");
//配置key的序列化类
props.put("key.serializer.class", "kafka.serializer.StringEncoder");
//指定分区
props.put("partitioner.class", "com.hashleaf.kafka.MyPartitioner"); //request.required.acks
//0, which means that the producer never waits for an acknowledgement from the broker (the same behavior as 0.7). This option provides the lowest latency but the weakest durability guarantees (some data will be lost when a server fails).
//1, which means that the producer gets an acknowledgement after the leader replica has received the data. This option provides better durability as the client waits until the server acknowledges the request as successful (only messages that were written to the now-dead leader but not yet replicated will be lost).
//-1, which means that the producer gets an acknowledgement after all in-sync replicas have received the data. This option provides the best durability, we guarantee that no messages will be lost as long as at least one in sync replica remains.
props.put("request.required.acks","-1"); //创建producer 对象
producer = new Producer<String, String>(new ProducerConfig(props));
} public void produce(){
int count = 1000;
for (int i = 0; i < count; i++) {
String key = String.valueOf(i);
String message = "hashleaf-"+i;
producer.send(new KeyedMessage<String, String>(HASHLEAF_KAFKA_TOPIC, key ,message));
} //发送完成后关闭
//producer.close();
} public static void main(String[] args) {
new MyProducer().produce();
}
}

自定义分区

package com.hashleaf.kafka;

import kafka.producer.Partitioner;
import kafka.utils.VerifiableProperties; /**
* 自定义分区规则
* @author xiaojf 294825811@qq.com
* @since 2015-7-15 下午11:57:23
*/
public class MyPartitioner implements Partitioner { public MyPartitioner(VerifiableProperties props){
}
@Override
public int partition(Object obj, int numPartitions) {
System.out.println(obj);
return Integer.parseInt( obj +"") % numPartitions;
} }

maven

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hashleaf</groupId>
<artifactId>kafka</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>0.8.2.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<artifactId>jmxtools</artifactId>
<groupId>com.sun.jdmk</groupId>
</exclusion>
<exclusion>
<artifactId>jmxri</artifactId>
<groupId>com.sun.jmx</groupId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

kafka 0.8.2 消息生产者 producer的更多相关文章

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

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

  2. kafka 0.8.2 消息生产者 KafkaProducer

    package com.hashleaf.kafka; import java.util.Properties; import java.util.concurrent.ExecutorService ...

  3. kafka 0.10.2 消息生产者

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

  4. kafka 0.10.2 消息消费者

    package cn.xiaojf.kafka.consumer; import org.apache.kafka.clients.consumer.ConsumerConfig; import or ...

  5. kafka 0.8.2 消息消费者 consumer

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. Kafka 详解(三)------Producer生产者

    在第一篇博客我们了解到一个kafka系统,通常是生产者Producer 将消息发送到 Broker,然后消费者 Consumer 去 Broker 获取,那么本篇博客我们来介绍什么是生产者Produc ...

  7. 【原创】Kafka 0.11消息设计

    Kafka 0.11版本增加了很多新功能,包括支持事务.精确一次处理语义和幂等producer等,而实现这些新功能的前提就是要提供支持这些功能的新版本消息格式,同时也要维护与老版本的兼容性.本文将详细 ...

  8. Kafka设计解析(十六)Kafka 0.11消息设计

    转载自 huxihx,原文链接 [原创]Kafka 0.11消息设计 目录 一.Kafka消息层次设计 1. v1格式 2. v2格式 二.v1消息格式 三.v2消息格式 四.测试对比 Kafka 0 ...

  9. 【转】 详解Kafka生产者Producer配置

    粘贴一下这个配置,与我自己的程序做对比,看看能不能完善我的异步带代码:   -----------------------------------------    详解Kafka生产者Produce ...

随机推荐

  1. 【Scala】Scala之Packaging and Imports

    一.前言 前面介绍了Scala的Object,由于中间论文的时间耽误了些时间,接着继续学习Scala的Packaging和Imports 二.Packaging and Imports Scala的包 ...

  2. KMP算法【代码】

    废话不多说,看毛片算法的核心在于next数组. 很多地方用的都是严书上的方法来求解next数组,代码确实简洁明了,但是可能对于初学者来说不容易想到,甚至不是一下子能理解.(好了,其实我说的就是自己,别 ...

  3. 百度Web前端面试经历

    今天面了百度的前端实习职位.一面.时间大概是50分钟.面试官是位很帅气的小伙子,非常友好的一个人.进门的时候他让我等一会,我瞄了一眼他的电脑屏幕,发现他在coding…… 9点50开始的面试. 面试官 ...

  4. 如何选择合适的PHP开发框架

    PHP作为一门成熟的WEB应用开发语言,已经深受广大开发者的青睐.与此同时,各式各样的PHP开发框架也从出不穷,面对如此多而且良莠不齐的开发框架,开发者们想必都会眼花缭乱,不知道该选择用哪个.其实并没 ...

  5. struts2之拦截器

    1. 为什么需要拦截器 早期MVC框架将一些通用操作写死在核心控制器中,致使框架灵活性不足.可扩展性降低, Struts 2将核心功能放到多个拦截器中实现,拦截器可自由选择和组合,增强了灵活性,有利于 ...

  6. 3.QT5.8支持中文输入法(附带老版本的解决+不理想的情况解决)

    安装过程:http://www.cnblogs.com/dotnetcrazy/p/6725945.html 用了QT发现,中文输入法不能输入...一开始以为是输入法问题,后来发现,其他地方都可以中文 ...

  7. Maven(二)之Maven项目构建演练

    从上一篇的讲解中我们知道了什么是Maven,然后它的安装配置,到修改本地仓库,这篇我们用一个实际的例子,带领大家走进我们的Maven之旅.让我们一起来体验一下Maven的高度自动化构建项目的过程. 一 ...

  8. mybatis中oracle实现分页效果

    首先当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致 ...

  9. C# TreeGridView 实现进程列表

    效果如图 0x01 获取进程列表,使用Win32Api规避"拒绝访问"异常 public List<AppProcess> GetAppProcesses() { In ...

  10. SQL*Plus快速入门

    连接数据库sqlplus hr@\"//mymachine.mydomain:port/MYDB\" --连接到MYDB数据库的一个HR数据集里sqlplus hr@MYDB -- ...