Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.anhth12.kafka.ulti; import com.anhth12.lambda.common.collection.Pair; import com.anhth12.lambda.common.random.RandomManager; import com.google.common.base.Preconditions; import java.util.Properties; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; import org.apache.commons.math3.random.RandomGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author Tong Hoang Anh */ public final class ProduceData { private final static Logger log = LoggerFactory.getLogger(ProduceData.class); private final DatumGenerator<String, String> datumGenerator; private final int zkPort; private final int kafkaPort; private final String topic; private final int howMany; private final int intervalMsec; private String host; public ProduceData(DatumGenerator<String, String> datumGenerator, int zkPort, int kafkaPort, String topic, int howMany, int intervalMsec, String host) { Preconditions.checkNotNull(datumGenerator); Preconditions.checkArgument(zkPort > 0); Preconditions.checkArgument(kafkaPort > 0); Preconditions.checkNotNull(topic); Preconditions.checkArgument(howMany > 0); Preconditions.checkArgument(intervalMsec >= 0); this.datumGenerator = datumGenerator; this.zkPort = zkPort; this.kafkaPort = kafkaPort; this.topic = topic; this.howMany = howMany; this.intervalMsec = intervalMsec; this.host = host; } public void start() throws InterruptedException { KafkaUtils.maybeCreateTopic(host, zkPort, topic); RandomGenerator random = RandomManager.getRandom(); Properties producerProps = new Properties(); producerProps.setProperty("metadata.broker.list", "localhost:" + kafkaPort); producerProps.setProperty("serializer.class", "kafka.serializer.StringEncoder"); Producer<String, String> producer = new Producer<>(new ProducerConfig(producerProps)); try { for (int i = 0; i < howMany; i++) { Pair<String, String> datum = datumGenerator.generate(i, random); KeyedMessage<String, String> keyedMessage = new KeyedMessage<>(topic, datum.getFirst(), datum.getSecond()); producer.send(keyedMessage); log.debug("Sent datum {} = {}", keyedMessage.key(), keyedMessage.message()); if (intervalMsec > 0) { Thread.sleep(intervalMsec); } } } finally { producer.close(); } } public void deleteTopic() { KafkaUtils.deleteTopic(host, zkPort, topic); } public static void main(String[] args) throws InterruptedException { int howMany = Integer.parseInt(args[0]); int intervalMsec = Integer.parseInt(args[1]); String topic = args[2]; int zkPort = Integer.parseInt(args[3]); int kafkaPort = Integer.parseInt(args[4]); String host = args[5]; ProduceData producer = new ProduceData(new SampleALSUpdateGenerator(), zkPort, kafkaPort, topic, howMany, intervalMsec, host); producer.start(); } }