完成对序列化接口的自定义
一般在我们开发过程中我们传输的可能是一个对象,不再是单纯的String,这就要求我们能够对一个对象直接进行序列化完成传输
1.创建实体类
public class User implements Serializable {
private Integer id;
private String name;
private Date birthDay;
public User ( ) {
}
public User ( Integer id, String name, Date birthDay) {
this . id = id;
this . name = name;
this . birthDay = birthDay;
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Date getBirthDay ( ) {
return birthDay;
}
public void setBirthDay ( Date birthDay) {
this . birthDay = birthDay;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", birthDay=" + birthDay +
'}' ;
}
}
2.实现kafka提供的Serializer接口
public class UserDefineSerializer implements Serializer < Object> {
@Override
public void configure ( Map< String, ? > configs, boolean isKey) {
System. out. println ( "configure" ) ;
}
@Override
public byte [ ] serialize ( String s, Object o) {
return SerializationUtils. serialize ( ( Serializable) o) ;
}
@Override
public byte [ ] serialize ( String topic, Headers headers, Object data) {
return SerializationUtils. serialize ( ( Serializable) data) ;
}
@Override
public void close ( ) {
System. out. println ( "close" ) ;
}
}
3.创建生产者
public class KafkaProducerUser {
public static void main ( String[ ] args) {
Properties props = new Properties ( ) ;
props. put ( ProducerConfig. BOOTSTRAP_SERVERS_CONFIG, "ip:9092" ) ;
props. put ( ProducerConfig. KEY_SERIALIZER_CLASS_CONFIG, StringSerializer. class . getName ( ) ) ;
props. put ( ProducerConfig. VALUE_SERIALIZER_CLASS_CONFIG, UserDefineSerializer. class . getName ( ) ) ;
KafkaProducer< String, User> producer = new KafkaProducer < > ( props) ;
for ( int i = 0 ; i < 5 ; i++ ) {
ProducerRecord< String, User> record = new ProducerRecord < > ( "topic04" , "key" + i, new User ( i, "user" + i, new Date ( ) ) ) ;
producer. send ( record) ;
}
producer. close ( ) ;
}
}
4.实现kafka提供的Deserializer接口
public class UserDefineDeserializer implements Deserializer < Object> {
@Override
public void configure ( Map< String, ? > configs, boolean isKey) {
System. out. println ( "configure" ) ;
}
@Override
public Object deserialize ( String s, byte [ ] bytes) {
return SerializationUtils. deserialize ( bytes) ;
}
@Override
public Object deserialize ( String topic, Headers headers, byte [ ] data) {
return SerializationUtils. deserialize ( data) ;
}
@Override
public void close ( ) {
System. out. println ( "close" ) ;
}
}
5.创建消费者
public class KafkaConsumerUser {
public static void main ( String[ ] args) {
Properties props = new Properties ( ) ;
props. put ( ConsumerConfig. BOOTSTRAP_SERVERS_CONFIG, "ip:9092" ) ;
props. put ( ConsumerConfig. KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer. class . getName ( ) ) ;
props. put ( ConsumerConfig. VALUE_DESERIALIZER_CLASS_CONFIG, UserDefineDeserializer. class . getName ( ) ) ;
props. put ( ConsumerConfig. GROUP_ID_CONFIG, "g1" ) ;
KafkaConsumer< String, User> consumer = new KafkaConsumer < > ( props) ;
consumer. subscribe ( Arrays. asList ( "topic01" ) ) ;
while ( true ) {
ConsumerRecords< String, User> records = consumer. poll ( Duration. ofSeconds ( 1 ) ) ;
if ( ! records. isEmpty ( ) ) {
Iterator< ConsumerRecord< String, User> > iterator = records. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
ConsumerRecord< String, User> record = iterator. next ( ) ;
String topic = record. topic ( ) ;
int partition = record. partition ( ) ;
long offset = record. offset ( ) ;
String key = record. key ( ) ;
User value = record. value ( ) ;
long timestamp = record. timestamp ( ) ;
System. out. println ( topic+ "\t" + partition+ "," + offset+ "\t" + key+ " " + value+ " " + timestamp) ;
}
}
}
}
}