Spark-Avro学习8之Java Avro使用(不生成code方式)


1.解释 

  Avro与thrift,protocol buffer区别之一是:Dynamic typing: 不必需生成代码(生成代码只是优化选项)



2.代码:

/**
 * @author xubo
 * time 20160502
 * ref http://avro.apache.org/docs/1.7.7/gettingstartedjava.html#Defining+a+schema
 */
package example.avro;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.avro.Schema;
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 AvroTestNoCode {

	public static void main(String[] args) throws IOException {

		// Creating users
		// First, we use a Parser to read our schema definition and create a
		// Schema object.
		Schema schema = new Schema.Parser().parse(new File(
				"file/avro/user.avsc"));

		// Using this schema, let's create some users.
		GenericRecord user1 = new GenericData.Record(schema);
		user1.put("name", "Alyssa");
		user1.put("favorite_number", 256);
		// Leave favorite color null

		GenericRecord user2 = new GenericData.Record(schema);
		user2.put("name", "Ben");
		user2.put("favorite_number", 7);
		user2.put("favorite_color", "red");

		System.out.println(user1);
		System.out.println(user2);

		// Serialize user1 and user2 to disk
		String iString = new SimpleDateFormat("yyyyMMddHHmmssSSS")
				.format(new Date());
		File file = new File("file/avro/output/users" + iString + ".avro");
		DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(
				schema);
		DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(
				datumWriter);
		dataFileWriter.create(schema, file);
		dataFileWriter.append(user1);
		dataFileWriter.append(user2);
		dataFileWriter.close();

		// Deserialize users from disk
		DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(
				schema);
		DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(
				file, datumReader);
		GenericRecord user = 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.
			user = dataFileReader.next(user);
			System.out.println(user);
		}
	}
}

user.avsc:

{
   "namespace": "example.avro",
   "type": "record",
   "name": "User",
   "fields": [
      {
         "name": "name",
         "type": "string"
      },
      {
         "name": "favorite_number",
         "type": [
            "int",
            "null"
         ]
      },
      {
         "name": "favorite_color",
         "type": [
            "string",
            "null"
         ]
      }
   ]
}

3.结果:

{"name": "Alyssa", "favorite_number": 256, "favorite_color": null}
{"name": "Ben", "favorite_number": 7, "favorite_color": "red"}
{"name": "Alyssa", "favorite_number": 256, "favorite_color": null}
{"name": "Ben", "favorite_number": 7, "favorite_color": "red"}

user.avro:

Objavro.schema�{"type":"record","name":"User","namespace":"example.avro","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}


参考:

【1】 Avro语言规范 http://avro.apache.org/docs/current/spec.html

【2】 教程:http://avro.apache.org/docs/1.7.7/gettingstartedjava.html#Defining+a+schema

你可能感兴趣的:(a)