一、实体类定义:
public class Company {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Company{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
public Company(String name, String address) {
this.name = name;
this.address = address;
}
public Company() {
}
}
二、自定义序列化器和反序列化器文章来源:https://www.toymoban.com/news/detail-667727.html
import org.apache.kafka.common.serialization.Serializer;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Map;
public class CompanySerializer implements Serializer<Company> {
@Override
public void configure(Map<String, ?> configs, boolean isKey) {
}
//进行字节数组序列化
@Override
public byte[] serialize(String topic, Company data) {
if(data == null){
return null;
}
byte[] name, address;
try{
if(data.getName() != null){
name = data.getName().getBytes("UTF-8");
}else {
name = new byte[0];
}
if(data.getAddress() != null){
address = data.getAddress().getBytes("UTF-8");
}else{
address = new byte[0];
}
ByteBuffer byteBuffer = ByteBuffer.allocate(4 + 4+ name.length + address.length);
byteBuffer.putInt(name.length);
byteBuffer.put(name);
byteBuffer.putInt(address.length);
byteBuffer.put(address);
return byteBuffer.array();
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
return new byte[0];
}
@Override
public void close() {
}
}
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Deserializer;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Map;
public class CompanyDeserializer implements Deserializer<Company> {
@Override
public void configure(Map<String, ?> configs, boolean isKey) {
}
@Override
public Company deserialize(String topic, byte[] data) {
if (data == null) {
return null;
}
ByteBuffer buffer = ByteBuffer.wrap(data);
int nameLen, addressLen;
String name, address;
nameLen = buffer.getInt();
byte[] nameBytes = new byte[nameLen];
buffer.get(nameBytes);
addressLen = buffer.getInt();
byte[] addressBytes = new byte[addressLen];
buffer.get(addressBytes);
try {
name = new String(nameBytes, "UTF-8");
address = new String(addressBytes, "UTF-8");
} catch (UnsupportedEncodingException ex) {
throw new SerializationException("Error:"+ex.getMessage());
}
return new Company(name,address);
}
@Override
public void close() {
}
}
三、定义生产者和消费者文章来源地址https://www.toymoban.com/news/detail-667727.html
package com.cisdi.dsp.modules.metaAnalysis.rest;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
public class CompanyProducer {
public static void main(String[] args) throws Exception{
Properties properties = new Properties();
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
//设置value的序列化器
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, CompanySerializer.class.getName());
properties.put("bootstrap.servers", "xxx.xxx.xxx.xxx:9092");
KafkaProducer<String, Company> producer = new KafkaProducer<>(properties);
Company company = new Company();
company.setAddress("Beijing");
company.setName("Connection");
ProducerRecord<String, Company> record = new ProducerRecord<>("companyTopic", company);
producer.send(record).get();
}
}
package com.cisdi.dsp.modules.metaAnalysis.rest;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import java.time.Duration;
import java.util.Collections;
import java.util.Properties;
public class CompanyConsumer {
public static void main(String[] args) {
Properties properties=new Properties();
properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, CompanyDeserializer.class.getName());
properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"xxx.xxx.xxx.xxx:9092");
properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"my");
KafkaConsumer<String,Company> kafkaConsumer=new KafkaConsumer<>(properties);
kafkaConsumer.subscribe(Collections.singletonList("companyTopic"));
while(true){
ConsumerRecords<String,Company> consumerRecords=kafkaConsumer.poll(Duration.ofMillis(1000));
for(ConsumerRecord<String,Company> consumerRecord: consumerRecords){
System.out.println(consumerRecord.value());
}
}
}
}
到了这里,关于kafka复习:(3)自定义序列化器和反序列化器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!