2012-07-24 15 views
6

私はHDFSに保存されているAvroファイルを簡単に読み込もうとしています。ローカルファイルシステム上にあるときにそれを読む方法を見つけました....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ファイルを読むことができますか?

EDIT:SeekableInputを実装するカスタムクラス(SeekableHadoopInput)を作成することで、これを機能させることができます。私はgithubの "ganglion"からこれを "盗んだ"。それでも、このためのHadoop/Avro統合パスがあるようです。

おかげ

答えて

21

(それはHadoopのに依存するためアブロ・mapredサブモジュールでは、)FsInputクラスこれを行うことができます。 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 
関連する問題