avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simone Leo <simone....@crs4.it>
Subject Re: Deserialize Avro Object Without Schema
Date Wed, 25 Mar 2015 10:36:25 GMT
Hello,

here is a full working example of deserialization without schema (same 
as the one at http://avro.apache.org/docs/1.7.7/gettingstartedjava.html, 
but using the 0-arg constructor for GenericDatumReader, i.e., no schema):

import java.io.File;

import org.apache.avro.generic.GenericRecord;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.io.DatumReader;
import org.apache.avro.file.DataFileReader;


public class TryRead {

   public static void main(String[] args) throws Exception {
     String fName = null;
     try {
       fName = args[0];
     } catch (ArrayIndexOutOfBoundsException e) {
       System.err.println("Usage: TryRead AVRO_FILE");
       System.exit(1);
     }
     File file = new File(fName);
     DatumReader<GenericRecord> datumReader =
         new GenericDatumReader<GenericRecord>();
     DataFileReader<GenericRecord> dataFileReader =
         new DataFileReader<GenericRecord>(file, datumReader);
     GenericRecord user = null;
     while (dataFileReader.hasNext()) {
       user = dataFileReader.next(user);
       System.out.println(user);
     }
   }
}

On 03/25/2015 10:08 AM, Rendy Bambang Junior wrote:
> Hi,
>
> I'm new in Avro. One of Avro's benefit is dynamic typing which code need
> not to be generated to deserialize Avro schema.
>
> I'm trying to find an example to deserialize Avro without specify the
> schema but all of them require me to pass schema to GenericDatumReader
> constructor or set the schema after construct the reader.
>
> So, I'm wondering, *is it possible to deserialize Avro without defining
> it's schema?*
>
> I mean, it's like getting value from Map<String, Object> and cast to
> desired data type without knowing the schema.
>
> It should be possible right? Since the schema itself is embedded in the
> data.
>
> Attached my sample code, it get null pointer exception because I don't
> specify the schema.
>
> Thanks!
>
> Rendy

-- 
Simone Leo
Data Fusion - Distributed Computing
CRS4
POLARIS - Building #1
Piscina Manna
I-09010 Pula (CA) - Italy
e-mail: simone.leo@crs4.it
http://www.crs4.it

-- 
Simone Leo
Data Fusion - Distributed Computing
CRS4
POLARIS - Building #1
Piscina Manna
I-09010 Pula (CA) - Italy
e-mail: simone.leo@crs4.it
http://www.crs4.it

Mime
View raw message