Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8688
  • 博文数量: 3
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 41
  • 用 户 组: 普通用户
  • 注册时间: 2017-04-05 00:08
文章分类
文章存档

2017年(3)

我的朋友
最近访客

分类: 大数据

2017-09-12 21:17:42

1;首先创建topic 
./bin/kafka-topics.sh -zookeeper zk1:2181,zk2:2181,zk3:2181 -topic test -replication-factor 3 -partition 3 -create
2:查看topic
./bin/kafka-topics.sh -zookeeper zk1:2181,zk2:2181,zk3:2181 -list

3:然后执行如下代码

点击(此处)折叠或打开

  1. package com.xx.bigdata.kafkaTool;

  2. import org.apache.kafka.clients.KafkaClient;
  3. import org.apache.kafka.clients.producer.KafkaProducer;
  4. import org.apache.kafka.clients.producer.ProducerConfig;
  5. import org.apache.kafka.clients.producer.ProducerRecord;

  6. import java.util.Properties;
  7. import java.util.UUID;

  8. public class Producer {
  9.     public static void main(String[] args) {
  10.         Properties properties = new Properties() {{
  11.             put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "zk1:9092,zk2:9092,zk3:9092");
  12.             put(ProducerConfig.ACKS_CONFIG, "all");
  13.             put(ProducerConfig.RETRIES_CONFIG, 0);
  14.             put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
  15.             put(ProducerConfig.LINGER_MS_CONFIG, 1);
  16.             put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
  17.             put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
  18.             put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
  19.         }};
  20.         KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
  21.         while (true) {
  22.             try {
  23.                 Thread.sleep(500);
  24.             } catch (InterruptedException e) {
  25.                 e.printStackTrace();
  26.             }
  27.             producer.send(new ProducerRecord<>("test", UUID.randomUUID().toString(), UUID.randomUUID().toString()));
  28.         }
  29.     }
  30. }


阅读(746) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~