kafka java api生产者
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.Test;
import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
public class KafkaProducer {
private final Producer<String, String> producer;
public final static String TOPIC = "test";
private KafkaProducer(){
Properties props = new Properties();
// 此处配置的是kafka的broker地址:端口列表
props.put("metadata.broker.list", "192.168.170.185:9092,192.168.170.185:9093,192.168.170.185:9094");
//配置value的序列化类
props.put("serializer.class", "kafka.serializer.StringEncoder");
//配置key的序列化类
props.put("key.serializer.class", "kafka.serializer.StringEncoder");
//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 = new Producer<String, String>(new ProducerConfig(props));
}
void produce() {
int messageNo = 1;
final int COUNT = 101;
int messageCount = 0;
while (messageNo < COUNT) {
String key = String.valueOf(messageNo);
String data = "Hello kafka message :" + key;
producer.send(new KeyedMessage<String, String>(TOPIC, key ,data));
System.out.println(data);
messageNo ++;
messageCount++;
}
System.out.println("Producer端一共产生了" + messageCount + "条消息!");
}
public static void main( String[] args )
{
new KafkaProducer().produce();
}
}
kafka java api生产者的更多相关文章
- Kafka笔记整理(二):Kafka Java API使用
下面的测试代码使用的都是下面的topic: $ kafka-topics.sh --describe hadoop --zookeeper uplooking01:,uplooking02:,uplo ...
- kafka java API的使用
Kafka包含四种核心的API: 1.Producer API支持应用将数据流发送到Kafka集群的主题 2.Consumer API支持应用从Kafka集群的主题中读取数据流 3.Streams A ...
- Kafka Java API操作topic
Kafka官方提供了两个脚本来管理topic,包括topic的增删改查.其中kafka-topics.sh负责topic的创建与删除:kafka-configs.sh脚本负责topic的修改和查询,但 ...
- Kafka Java API获取非compacted topic总消息数
目前Kafka并没有提供直接的工具来帮助我们获取某个topic的当前总消息数,需要我们自行写程序来实现.下列代码可以实现这一功能,特此记录一下: /** * 获取某个topic的当前消息数 * Jav ...
- kafka java api消费者
import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties; imp ...
- kafka java使用
首先添加maven依赖 Kafka <dependency> <groupId>org.apache.kafka</groupId> <artifactId& ...
- Kafka系列三 java API操作
使用java API操作kafka 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...
- _00017 Kafka的体系结构介绍以及Kafka入门案例(0基础案例+Java API的使用)
博文作者:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...
- 使用Java API创建(create),查看(describe),列举(list),删除(delete)Kafka主题(Topic)
使用Kafka的同学都知道,我们每次创建Kafka主题(Topic)的时候可以指定分区数和副本数等信息,如果将这些属性配置到server.properties文件中,以后调用Java API生成的主题 ...
随机推荐
- linux内存占用分析
概述 想必在linux上写过程序的同学都有分析进程占用多少内存的经历,或者被问到这样的问题——你的程序在运行时占用了多少内存(物理内存)?通常我们可以通过top命令查看进程占用了多少内存.这里我们可以 ...
- HDU 1627 Krypton Factor
回溯法:避免无用判断,强化回溯代码的实现过程 题目的大意就是以字典序为准,排列字符串,但要保证一个字符串中不包含相邻的重复子串. Problem Description For example, th ...
- jsoup解析xml某片段的问题
参考: http://blog.csdn.net/qy20115549/article/details/53556928 <tr> <td class='center'> &l ...
- 1.5-1.6 oozie部署
一.部署 可参考文档:http://archive.cloudera.com/cdh5/cdh/5/oozie-4.0.0-cdh5.3.6/DG_QuickStart.html 1.解压oozie ...
- sql之外键变种
多对一 : 只需设个外键 外键变种之一对一:普通外键关联的表是一对多关系,如果外键上再加上唯一索引,表就会变成一对一关系. 外键变种之多对多:
- Flutter实战视频-移动电商-60.购物车_全选按钮的交互效果制作
60.购物车_全选按钮的交互效果制作 主要做全选和复选框的这两个功能 provide/cart.dart 业务逻辑写到provide里面 先持久化取出来字符串,把字符串编程list.循环list ca ...
- SqlServer规则
定义:规则时单独的SQLServer对象,可以关联到一个或几个表中的一列或几列.它可以使用多种方式来完成对数据值的校验,可以使用函数返回验证信息,也可以使用关键字BETWEEN,LIKE和IN完成对输 ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- Swift3.0 字典简单使用
Dictionary //定义一个只能存放string 类型的字典 var dict:Dictionary<String,String> = ["一班":"2 ...
- now code——处女座的期末复习
题目描述 快要期末考试了,处女座现在有n门课程需要考试,每一门课程需要花ai小时进行复习,考试的起始时间为bi,处女座为了考试可以不吃饭不睡觉,处女座想知道他能否复习完所有的科目(即在每一门考试之前复 ...