KafkaConsuerMain.java
public class KafkaConsuerMain {
private static final String brokerList = "xxx:xxx";
private static final String topic = "Topic5";
private static final String groupId = "group.rrr";
public static void main( String[] args ) {
Properties properties = new Properties();
// 设置 key 反序列化器
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
// 设置值的 反序列化器
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
properties.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
// 指定消费者拦截器
properties.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, ConsumerInterceptorSn.class.getName());
// 创建消费者对象
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
// 订阅主题
consumer.subscribe(Collections.singletonList(topic));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(1000);
for (ConsumerRecord<String, String> record : records) {
System.out.println("消费数据:" + record);
}
}
}
}
ConsumerInterceptorMain.java
功能: 过滤掉数据值不等于‘333’的数据。没有实际意义,只用作测试
public class ConsumerInterceptorMain implements ConsumerInterceptor<String, String> {
private static final long EXPIRE_INTERVAL = 10 * 1000;
@Override
public ConsumerRecords<String, String> onConsume(ConsumerRecords<String, String> records) {
System.out.println("before: " + records);
Map<TopicPartition, List<ConsumerRecord<String, String>>> newRecords = new HashMap<>();
for (TopicPartition tp : records.partitions()) {
List<ConsumerRecord<String, String>> tpRecords = records.records(tp);
List<ConsumerRecord<String, String>> newTpRecords = new ArrayList<>();
for (ConsumerRecord<String, String> record : tpRecords) {
if("333".equals(record.value())){
newTpRecords.add(record);
}
}
if (!newTpRecords.isEmpty()) {
newRecords.put(tp, newTpRecords);
}
}
return new ConsumerRecords<>(newRecords);
}
@Override
public void onCommit( Map<TopicPartition, OffsetAndMetadata> offsets ) {
}
@Override
public void close() {
}
@Override
public void configure( Map<String, ?> configs ) {
}
}