首页 技术 正文
技术 2022年11月15日
0 收藏 880 点赞 4,130 浏览 3479 个字
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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905