Sending data via AVRO

I got a better understanding when I used AVRO to write data via PHP and to read them via Java. It demonstrated to me how data can be written in one language and subsequently be read in another language.
I use a file to have the data be written by PHP. Subsequently the data can be read by Java.
The question then is: what is the advantage of using AVRO to have data been written in file. This can be compared to ordinary CSV files or a more advanced XML format.
Let us first write the data via PHP:
using this script
We have now written in PHP some data to a file. The nice thing about it that the data are written with their description, as provided by the schema. Apparently, the schema shows that we write records with two elements: a number and a name. This schema is only written once and the data are written after the schema.
When one compares this to a normal CSV file, one may notice that the schema is added to the file. Hence a programme that reads the data may verify that the data are in the correct format.
One may argue that this is similar to XML but with XML, the schema is repeated with every data element. Hence XML files tend to very large as compared to CSV. An avro file avoids this by providing the sceme just once.

The file may be read in Java by:

package avro;

import java.io.File;
import java.io.IOException;

import org.apache.avro.Schema;
import org.apache.avro.Schema.Parser;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;

public class GenericMain {
	public static void main(String[] args) throws IOException {
		Schema schema = new Parser().parse(new File("C:/inetpub/wwwroot/user.avsc"));
		File file = new File("C:/inetpub/wwwroot/data.avr");
		DatumReader datumReader = new GenericDatumReader(schema);
		DataFileReader dataFileReader = new DataFileReader(file, datumReader);
		GenericRecord member = null;
		while (dataFileReader.hasNext()) {
			// Reuse user object by passing it to next(). This saves us from
			// allocating and garbage collecting many objects for files with
			// many items.
			member = dataFileReader.next(member);
			System.out.println(member);
		}
		
	}
}

This demonstrates that the data can be read in another language. In this case, Java is used to read the file. The Java programme just needs to know where the data is stored (in data.avr) and how the schema looks like (provided in user.avsc). After that the file can be read and its records can be accessed.

Door tom