簡體   English   中英

從HDFS讀取一個簡單的Avro文件

[英]Reading a simple Avro file from HDFS

我試圖簡單讀取存儲在HDFS中的Avro文件。 我發現當它在本地文件系統上時如何閱讀....

FileReader reader = DataFileReader.openReader(new File(filename), new GenericDatumReader());

for (GenericRecord datum : fileReader) {
   String value = datum.get(1).toString();
   System.out.println("value = " value);
}

reader.close();

但是,我的文件是HDFS。 我不能給openReader一個Path或一個FSDataInputStream。 如何在HDFS中讀取Avro文件?

編輯:我通過創建實現SeekableInput的自定義類(SeekableHadoopInput)來實現此目的。 我在github上“偷走”了這個“Ganglion”。 似乎仍然會有一個Hadoop / Avro集成路徑。

謝謝

FsInput類(在avro-mapred子模塊中,因為它依賴於Hadoop)可以做到這一點。 它提供Avro數據文件所需的可搜索輸入流。

Path path = new Path("/path/on/hdfs");
Configuration config = new Configuration(); // make this your Hadoop env config
SeekableInput input = new FsInput(path, config);
DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>();
FileReader<GenericRecord> fileReader = DataFileReader.openReader(input, reader);

for (GenericRecord datum : fileReader) {
    System.out.println("value = " + datum);
}

fileReader.close(); // also closes underlying FsInput

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM