kafka java 批量发送_java向kafka批量均匀发送数据的方法
RT。public void sendData(String brokerList,String topic,List datas){Properties properties = new Properties();properties.put("metadata.broker.list",brokerList);properties.put("producer.type","async");pr
RT。
public void sendData(String brokerList,String topic,List datas){
Properties properties = new Properties();
properties.put("metadata.broker.list",brokerList);
properties.put("producer.type","async");
properties.put("serializer.class", "kafka.serializer.DefaultEncoder");
properties.put("key.serializer.class", "kafka.serializer.StringEncoder");
ProducerConfig producerConfig = new ProducerConfig(properties);
Producer kafkaProducer = new Producer(producerConfig);
List> kms = new ArrayList>();
for(int i=0;i
KeyedMessage km = new KeyedMessage(topic, String.valueOf(i), datas.get(i));
kms.add(km);
}
kafkaProducer.send(kms);
kafkaProducer.close();
}
更多推荐
所有评论(0)