Reading a simple Avro file from HDFS

后端 未结 1 924
遥遥无期
遥遥无期 2021-02-06 12:05

I am trying to do a simple read of an Avro file stored in HDFS. I found out how to read it when it is on the local file system....

FileReader reader = DataFileR         


        
1条回答
  •  南旧
    南旧 (楼主)
    2021-02-06 12:32

    The FsInput class (in the avro-mapred submodule, since it depends on Hadoop) can do this. It provides the seekable input stream that is needed for Avro data files.

    Path path = new Path("/path/on/hdfs");
    Configuration config = new Configuration(); // make this your Hadoop env config
    SeekableInput input = new FsInput(path, config);
    DatumReader reader = new GenericDatumReader();
    FileReader fileReader = DataFileReader.openReader(input, reader);
    
    for (GenericRecord datum : fileReader) {
        System.out.println("value = " + datum);
    }
    
    fileReader.close(); // also closes underlying FsInput
    

    0 讨论(0)
提交回复
热议问题