import java.io.IOException; import java.io.InputStream; import org.apache.avro.file.DataFileStream; import org.apache.avro.generic.GenericDatumReader; import org.apache.avro.generic.GenericRecord; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; public class HdfsReadAvro { public static void readFromAvro(InputStream is) throws IOException { DataFileStream<Object> reader = new DataFileStream<Object>(is, new GenericDatumReader<Object>()); for (Object o : reader) { GenericRecord r = (GenericRecord) o; System.out.println(r.get("username")+ ":"+r.get("password")); } IOUtils.cleanup(null, is); IOUtils.cleanup(null, reader); } public static void main(String[] args) throws Exception { Configuration config = new Configuration(); FileSystem hdfs = FileSystem.get(config); Path destFile = new Path(args[0]); InputStream is = hdfs.open(destFile); readFromAvro(is); } }