This tutorial is written in reference to the quick start of the Apache Avro official website: http://avro.apache.org/docs/current/gettingstartedjava.html
1. Download Avro-tools-1.7.5.jar
http://mirrors.cnnic.cn/apache/avro/avro-1.7.5/java/
2. Generate Java code using Avro-tools
Write Avro Sample USER.AVRC
{' namespace ': ' Example.avro ', '
type ': ' Record ',
' name ': ' User ',
' Fields ': [
{' name ': ' Name ', ' type ': ' String '},
{' name ': ' Favorite_number ', ' type ': [' int ', ' null ']},
{' name ': ' Favorite_Color ', ' type ': [' string "," null "]}
]
}
Execute in the directory where USER.AVRC is located:
Java-jar/path/to/avro-tools-1.7.5.jar Compile schema User.avsc.
Will be generated in this directory./example/avro/user.java program
3.maven build Eclipse Project write Java program serialization and deserialization of Avro files
The Pom.xml file for Maven is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" HT Tp://www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http:// Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid >com.iflytek.cpcloud</groupId> <artifactId>avro-test</artifactId> <version>0.1.0- snapshot</version> <dependencies> <dependency> <groupid>log4j</groupid > <artifactId>log4j</artifactId> <version>1.2.17</version> </dependenc y> <dependency> <groupId>commons-logging</groupId> <artifactid>commons-lo
gging</artifactid> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId> <version>1.2</version> </dependency&
Gt
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupid>org.apache. avro</groupid> <artifactId>avro</artifactId> <version>1.7.5</version> </d ependency> </dependencies> <build> <plugins> <plugin> <ar Tifactid>maven-assembly-plugin</artifactid> <version>2.2-beta-5</version> <configurati On> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> & lt;/descriptorrefs> </configuration> </plugin> <plugin> <groupid>or G.apache.maven.plugins</groupid> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</v
ersion> <configuration> <source>1.6</source> <target>1.6</target>
<encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
More Wonderful content: http://www.bianceng.cn/Servers/web/
MVN Build Eclipse Project
MVN Eclipse:eclipse
To import the generated User.java file above in the Example.avro package using Avro-tools
To import a generated project into eclipse
Write program serialization and reverse sequence Avro files Test.java
Package Example.avro;
Import Java.io.File;
Import java.io.IOException;
Import Org.apache.avro.file.DataFileReader;
Import Org.apache.avro.file.DataFileWriter;
Import Org.apache.avro.io.DatumReader;
Import Org.apache.avro.io.DatumWriter;
Import Org.apache.avro.specific.SpecificDatumReader;
Import Org.apache.avro.specific.SpecificDatumWriter;
public class Test {public static void main (string[] args) throws IOException {code ();
Decode ();
public static void code () throws ioexception{User User1 = new user ();
User1.setname ("Alyssa");
User1.setfavoritenumber (256);
Leave favorite Color Null//Alternate constructor User user2 = new User ("Ben", 7, "Red");
Construct via builder User User3 = User.newbuilder (). SetName ("Charlie"). Setfavoritecolor ("Blue")
. Setfavoritenumber (NULL). Build (); User User4 = New User ("Jimmy", 7, "yellow");
Serialize user1 and user2 to disk File = new file ("Users.avro");
datumwriter<user> userdatumwriter = new specificdatumwriter<user> (user.class);
datafilewriter<user> datafilewriter = new datafilewriter<user> (userdatumwriter);
Datafilewriter.create (User1.getschema (), New File ("Users.avro"));
Datafilewriter.append (user1);
Datafilewriter.append (User2);
Datafilewriter.append (USER3);
Datafilewriter.append (USER4);
Datafilewriter.close (); public static void Decode () throws ioexception{//deserialize Users from disk Datumreader<user
> userdatumreader = new specificdatumreader<user> (user.class);
File File = new file ("Users.avro");
datafilereader<user> Datafilereader = new datafilereader<user> (file, userdatumreader);
User user = null; while (Datafilereader.hasnext ()) {//Reuse user object by passing itTo next ().
This is saves us from//allocating and garbage collecting many to files with//objects items.
user = Datafilereader.next (user);
SYSTEM.OUT.PRINTLN (user); }
}
}
The results of the operation are as follows:
{' name ': ' Alyssa ', ' favorite_number ': 256, ' Favorite_Color ': null}
{"Name": "Ben", "Favorite_number": 7, "Favorite_Color": "Red"}
{"Name": "Charlie", "Favorite_number": null, "Favorite_Color": "Blue"}
{"Name": "Jimmy", "Favorite_number": 7, "Favorite_Color": "Yellow"}
Generates Users.avro files in the root directory of Eclipse engineering