Created
June 13, 2019 05:55
-
-
Save tkstone/074e49c5cc24d1c2b073126a599d9627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.kafka.clients.producer.Producer; | |
import org.apache.kafka.clients.producer.ProducerRecord; | |
import org.apache.kafka.clients.producer.KafkaProducer; | |
import org.apache.kafka.clients.producer.RecordMetadata; | |
import java.util.Properties; | |
import java.util.concurrent.Future; | |
public class TestProducer { | |
private Producer<String, String> producer; | |
public static void main(String[] args) throws Exception { | |
Properties props = new Properties(); | |
props.put("bootstrap.servers", "localhost:9092"); | |
props.put("acks", "all"); | |
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); | |
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); | |
props.put("partitioner.class", "test.producer.TestPartitioner"); | |
TestProducer test = new TestProducer(); | |
test.producer = new KafkaProducer<>(props); | |
test.sendAMessage("dept1.key-1", "value-1"); | |
test.sendAMessage("dept1.key-2", "value-2"); | |
test.sendAMessage("dept2.key-3", "value-3"); | |
test.sendAMessage("dept2.key-4", "value-4"); | |
test.sendAMessage("dept3.key-5", "value-5"); | |
test.sendAMessage("dept3.key-6", "value-6"); | |
test.sendAMessage("dept4.key-7", "value-7"); | |
test.sendAMessage("dept4.key-8", "value-8"); | |
test.sendAMessage("dept5.key-9", "value-9"); | |
test.sendAMessage("dept5.key-10", "value-10"); | |
test.producer.close(); | |
} | |
private void sendAMessage(String key, String value) throws Exception{ | |
Future<RecordMetadata> metaData = producer.send(new ProducerRecord<String, String>("test-topic", key, value)); | |
int partition = metaData.get().partition(); | |
System.out.println(String.format("Partition %d for key %s", partition, key)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment