赞
踩
- package com.yrl.interceptor;
-
- import org.apache.kafka.clients.producer.ProducerInterceptor;
- import org.apache.kafka.clients.producer.ProducerRecord;
- import org.apache.kafka.clients.producer.RecordMetadata;
-
- import java.util.Map;
-
- public class TimeInterceptor implements ProducerInterceptor<String,String> {
- @Override
- public void configure(Map<String, ?> map) {
-
- }
-
- @Override
- public ProducerRecord<String,String> onSend(ProducerRecord<String,String> producerRecord) {
- //取出数据
- String value = producerRecord.value();
-
- //添加时间戳
- ProducerRecord<String, String> record = new ProducerRecord<String, String>(producerRecord.topic(),producerRecord.partition(),
- producerRecord.key(),value+System.currentTimeMillis()+"");
-
- return record;
- }
-
- @Override
- public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {
-
- }
-
- @Override
- public void close() {
-
- }
-
-
- }
- package com.yrl.interceptor;
-
- import org.apache.kafka.clients.producer.ProducerInterceptor;
- import org.apache.kafka.clients.producer.ProducerRecord;
- import org.apache.kafka.clients.producer.RecordMetadata;
-
- import java.util.Map;
-
- public class CountInterceptor implements ProducerInterceptor<String ,String> {
-
- int success = 0;
- int error = 0;
-
- @Override
- public void configure(Map<String, ?> map) {
-
- }
-
- @Override
- public ProducerRecord<String, String> onSend(ProducerRecord<String, String> producerRecord) {
- return producerRecord;
- }
-
- @Override
- public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {
- if(recordMetadata != null){
- success++;
- }else {
- error++;
- }
- }
-
- @Override
- public void close() {
- System.out.println("success="+success);
- System.out.println("error="+error);
- }
-
- }
- package com.yrl.producer;
-
- import org.apache.kafka.clients.producer.KafkaProducer;
- import org.apache.kafka.clients.producer.ProducerConfig;
- import org.apache.kafka.clients.producer.ProducerRecord;
-
- import java.util.ArrayList;
- import java.util.Properties;
-
- public class MyProducer {
-
- public static void main(String[] args) {
- Properties props = new Properties();
- //kafka 集群,broker-list
- props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop112:9092,hadoop113:9092,hadoop114:9092");
- // props.put("bootstrap.servers", "hadoop112:9092");
- props.put("acks", "all");
- //重试次数
- props.put("retries", 3);
- //批次大小
- props.put("batch.size", 16384);
- //等待时间
- props.put("linger.ms", 1);
- //RecordAccumulator 缓冲区大小
- props.put("buffer.memory", 33554432);
- props.put("key.serializer",
- "org.apache.kafka.common.serialization.StringSerializer");
- props.put("value.serializer",
- "org.apache.kafka.common.serialization.StringSerializer");
-
- //添加拦截器,注意顺序
- ArrayList<Object> interceptors = new ArrayList<>();
- interceptors.add("com.yrl.interceptor.TimeInterceptor");
- interceptors.add("com.yrl.interceptor.CountInterceptor");
- props.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG,interceptors);
-
-
- KafkaProducer<String, String> producer = new KafkaProducer<>(props);
-
- for (int i = 0; i < 10; i++) {
- producer.send(new ProducerRecord<String, String>("first",
- "yrl"+i));
- }
-
-
- producer.close();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。