分区规则:
public class Partitioner1 implements Partitioner{ public Partitioner1() { } public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) { List<PartitionInfo> partitions = cluster.partitionsForTopic(topic); int num = partitions.size(); int partNum = 0; try { partNum = Integer.parseInt((String) key); } catch (Exception e) { partNum = key.hashCode(); } return Math.abs(partNum % num); } public void close() { } public void configure(Map<String, ?> configs) { } }
生产者:
public class DProducer { public static void main(String[] args){ Properties prop = new Properties(); prop.put("bootstrap.servers","node:9092,node:9093,node:9094"); prop.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); prop.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); prop.put("partitioner.class", "com.kafka.d.Partitioner1"); Producer producer = new KafkaProducer(prop); for(int i = 0; i < 10; i++) { producer.send(new ProducerRecord("test2",Integer.toString(i), Integer.toString(i))); } producer.close(); } }
消费者:
public class DConsumer { public static void main(String[] args) { Properties prop = new Properties(); prop.put("bootstrap.servers","node:9092"); prop.put("group.id","test7"); prop.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); prop.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); Consumer consumer = new KafkaConsumer(prop); consumer.subscribe(Arrays.asList("test2")); while (true) { ConsumerRecords<String, String> c = consumer.poll(100); for(ConsumerRecord<String, String> c1: c) { System.out.println("Key: " + c1.key() + " Value: " + c1.value() + " Offset: " + c1.offset() + " Partitions: " + c1.partition()); } } } }