目录
首先创建maven项目,并添加依赖,pom文件如下:
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version> 0.10.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>0.10.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
通过JavaAPI操作Kafka生产者
示例代码:
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class MyKafkaProducer
{
public static void main(String[] args) throws InterruptedException
{
Properties props = new Properties();
//指定kafka服务器地址
props.put("bootstrap.servers", "node01:9092,node0:9092,node03:9092");
//指定消息的确认机制
props.put("acks", "all");
//重试机制
props.put("retries", 0);
//批量发送的大小
props.put("batch.size", 16384);
//消息的延迟
props.put("linger.ms", 1);
//消息缓冲区的大小
props.put("buffer.memory", 33554432);
//定义key的序列化
props.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
//定义value的序列化
props.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String,String> producer=new KafkaProducer<String,String>(props);
for(int i=0;i<100;i++)
{
// 发送数据 ,需要一个producerRecord对象,最少参数 String topic, V value
producer.send(new ProducerRecord<String, String>("test", "第"+i+"条记录"));
//Thread.sleep(100);