I will teach you step by step to use Jackson DataFormat: AVRO for data conversion
I will teach you step by step to use Jackson DataFormat: AVRO for data conversion
Jackson is an open source Java library that is used to serialize the Java object to JSON format and deepen to the Java object.Jackson provides many different data formats to meet various needs, one of which is AVRO data format.
AVRO is a data serialization system that defines a data architecture and can serialize the data into files with a small size and fast binary format.AVRO is very suitable for big data processing tasks because it has efficient compression and rapid reading and writing ability.
If you want to use Jackson DataFormat: AVRO in Java for data conversion, you can follow the steps below:
Step 1: Add dependencies
First, you need to add Jackson and Avro to your Java project.In the Maven project, the following dependencies can be added to the pom.xml file:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-avro</artifactId>
<version>2.12.1</version>
</dependency>
Step 2: Create AVROSCHEMA
Before using AVRO for data conversion, you need to define a AVROSCHEMA first.AVROSCHEMA describes the structure of the data, including field names, field types, etc.You can use Avro's schemabuilder to create AVROSCHEMA, for example::
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
Schema schema = SchemaBuilder.record("Person")
.fields()
.name("name").type().stringType().noDefault()
.name("age").type().intType().noDefault()
.endRecord();
The above code creates a AVROSCHEMA called "PERSON", which contains two fields: name and Age.
Step 3: serialized objects are avro formats
Next, you can use Jackson DataFormat: Avro to sequence the Java object into AVRO format.First of all, you need to create an AVROMAPPER object and pass the Avroschema to it:
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
AvroMapper avroMapper = new AvroMapper();
avroMapper.schema(schema);
Then, you can use the Writevalue method of Avromapper to sequence the Java object to the byte array of the Java object to the AVRO format:
Person person = new Person("Alice", 25);
byte[] avroData = avroMapper.writeValueAsBytes(person);
The above code will create a Person object and serialize it into byte array in Avro format.
Step 4: The AVRO format is the Java object
If you want to sequence the AVRO format data to the Java object, you can use the ReadValue method of AVROMAPPER.First of all, you need to pass the byte array of AVRO format to the readvalue method:
Person deserializedPerson = avroMapper.readValue(avroData, Person.class);
The above code will be read from the byte array of the AVRO format and turns its back -sequence to Person object.
The complete example code is as follows:
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
public class AvroSerializationExample {
public static void main(String[] args) throws IOException {
Schema schema = SchemaBuilder.record("Person")
.fields()
.name("name").type().stringType().noDefault()
.name("age").type().intType().noDefault()
.endRecord();
AvroMapper avroMapper = new AvroMapper();
avroMapper.schema(schema);
Person person = new Person("Alice", 25);
byte[] avroData = avroMapper.writeValueAsBytes(person);
Person deserializedPerson = avroMapper.readValue(avroData, Person.class);
System.out.println("Original Person: " + person);
System.out.println("Deserialized Person: " + deserializedPerson);
}
}
class Person {
private String name;
private int age;
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
The above code demonstrates how to sequence a Person object into a byte array in the AVRO format and sequence of this byte array into a Person object.You can run the code and check the output results on the console.
Through this example, you can understand how to use Jackson DataFormat: Avro in the Java library for data conversion.Hope this article will help you!