kafka 自定义生产者示例

来源:互联网 发布:手机淘宝看评价管理 编辑:程序博客网 时间:2024/05/18 00:56

代码如下:

public class SendMessage {/** * 自定义kafka生产者demo */public static void main(String[] args) {Properties props = new Properties();//zookeeper地址props.put("zookeeper.connect", "zoo1:2181,zoo2:2181,zoo3:2181");//序列化类props.put("serializer.class", "kafka.serializer.StringEncoder");//同步方式props.put("producer.type", "async");//压缩方式props.put("compression.codec", "1");//broker地址props.put("metadata.broker.list", "broker01:9092");ProducerConfig config = new ProducerConfig(props);//生产者对象Producer<String, String> producer = new Producer<String, String>(config);Random r = new Random();for(int i=0;i<10;i++){int id = r.nextInt(10000000);int memberid = r.nextInt(100000);int totalprice = r.nextInt(1000)+100;int youhui = r.nextInt(100);int sendpay = r.nextInt(3);StringBuffer data = new StringBuffer();data.append(String.valueOf(id)).append("\t").append(String.valueOf(memberid)).append("\t").append(String.valueOf(totalprice)).append("\t").append(String.valueOf(youhui)).append("\t").append(String.valueOf(sendpay)).append("\t").append("2015-12-01");System.out.println(data.toString());///topicstring数据producer.send(new KeyedMessage<String, String>("test",data.toString()));}producer.close();System.out.println("send over ------------------");}}


0 0
原创粉丝点击