The old version of Kafka, producer, was written in Scala and has been abolished since 0.9
Example code is as follows:
import kafka.producer.KeyedMessage;
import kafka.javaapi.producer.Producer;
import kafka.producer.ProducerConfig;
import java.util.Properties;
public class ProducerDemo {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("metadata.broker.list"."kafka01:9092,kafka02:9092");
properties.put("serializer.class"."kafka.serializer.StringEncoder");
properties.put("request.requird.acks"."1");
ProducerConfig config = new ProducerConfig(properties);
Producer<String, String> producer = new Producer<String, String>(config);
KeyedMessage<String,String> msg = new KeyedMessage<String,String>("topic"."key"."hello"); producer.send(msg); }}Copy the code
The following is an example code for a custom partition:
import kafka.producer.Partitioner; import kafka.utils.VerifiableProperties; public class SimplePartitioner implements Partitioner { public SimplePartitioner (VerifiableProperties props) { } public int partition(Object key, int a_numPartitions) { int partition = 0; String stringKey = (String) key; int offset = stringKey.lastIndexOf('. ');
if (offset > 0) {
partition = Integer.parseInt( stringKey.substring(offset+1)) % a_numPartitions;
}
returnpartition; }}Copy the code
For more blog posts on real-time computing, Kafka and other related technologies, welcome to real-time streaming computing