1. 程式人生 > >kafka自定義partition

kafka自定義partition

自定義分割槽設定
	class Partition implements Partitioner{
		//設定
		public void configure(Map<String, ?> configs) {

		}

		//分割槽邏輯
		public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes){
			return 1;
		}

		//釋放資源
		public void close(){

		}
	}

	{
		//配置生產者屬性(指定多個引數)
		Properties prop = new Properties();

		//省略不寫
		-----
		//訊息在傳送前必須序列化
		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.terry.kafka.Partition");

		//2、例項化producer
		KafkaProducer<String, String> producer = new KafkaProducer<String, String>(prop);

		//3、傳送訊息
		for(int i = 0; i < 99; i++) {
			producer.send(new ProducerRecord<String, String>("test", i),new CallBack(){
				public void onCompletion(RecordMetadata metadata, Exception exception) {
					//如果metadata不為null,拿到當前的資料偏移量與分割槽
					if(metadata != null) {
						Sout(metadata.topic() + "------" + metadata.offset() + "------" + metadata.partition());
					}
				}
			});
		}

		//4、關閉資源
		producer.close();
	}