Package it.dtk

Source Code of it.dtk.SchemaExample

package it.dtk;

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

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;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;

import it.dtk.avro.User;

public class SchemaExample {

  /**
   * @throws IOException
   *             example using bean schema defined in it.dreamslair.avro package
   */
  public static void schemaExample() 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");
    //this print the bean as it is saved in the datuwriter
    System.out.println(user1.toString());

    // Construct via builder
    User user3 = User.newBuilder().setName("Charlie")
        .setFavoriteColor("blue").setFavoriteNumber(null).build();

    // Serialize user1 and user2 to disk
    File 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(user1);
    dataFileWriter.append(user2);
    dataFileWriter.append(user3);
    dataFileWriter.append(user1);
    dataFileWriter.append(user2);
    dataFileWriter.append(user3);
    dataFileWriter.append(user1);
    dataFileWriter.append(user2);
    dataFileWriter.append(user3);
    dataFileWriter.close();

    // Deserialize Users from disk
    DatumReader<User> userDatumReader = new SpecificDatumReader<User>(
        User.class);
    DataFileReader<User> dataFileReader = new DataFileReader<User>(file,
        userDatumReader);
    User 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);
    }
  }


  public static void main(String[] args) {
    try {
      schemaExample();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}
TOP

Related Classes of it.dtk.SchemaExample

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.