2011-02-23 35 views
7

如何生成的文件,這些序列文件?我在這裏看到有關序列文件的鏈接,序列中的Hadoop

http://wiki.apache.org/hadoop/SequenceFile 

這些是使用默認Java序列化器編寫的嗎?以及如何讀取序列文件?

+0

這裏的關鍵類和值類是什麼。從哪裏訪問?請幫我解決這個問題。提前致謝。 – 2012-02-28 12:34:50

回答

16

序列文件由MapReduce任務的生成,並且可以用作通用格式MapReduce作業之間傳輸數據。

您可以通過以下方式閱讀:

Configuration config = new Configuration(); 
Path path = new Path(PATH_TO_YOUR_FILE); 
SequenceFile.Reader reader = new SequenceFile.Reader(FileSystem.get(config), path, config); 
WritableComparable key = (WritableComparable) reader.getKeyClass().newInstance(); 
Writable value = (Writable) reader.getValueClass().newInstance(); 
while (reader.next(key, value)) 
    // perform some operating 
reader.close(); 

您也可以使用SequenceFile.Writer自行生成序列文件。

在示例中使用的類如下:

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.fs.FileSystem; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.SequenceFile; 
import org.apache.hadoop.io.Writable; 
import org.apache.hadoop.io.WritableComparable; 

幷包含在hadoop-core行家依賴內:

<dependency> 
    <groupId>org.apache.hadoop</groupId> 
    <artifactId>hadoop-core</artifactId> 
    <version>1.2.1</version> 
</dependency> 
3

由於列弗Khomich的回答,我的問題已經解決了。

然而,該解決方案已被棄用了一段時間,新的API提供了更多的功能,也很容易使用。

退房hadoop.io.SequenceFile的源代碼,點擊here

Configuration config = new Configuration(); 
Path path = new Path("/Users/myuser/sequencefile"); 
SequenceFile.Reader reader = new Reader(config, Reader.file(path)); 
WritableComparable key = (WritableComparable) reader.getKeyClass() 
     .newInstance(); 
Writable value = (Writable) reader.getValueClass().newInstance(); 

while (reader.next(key, value)) { 
    System.out.println(key); 
    System.out.println(value); 
    System.out.println("------------------------"); 
} 
reader.close(); 

額外的信息,這裏是對抗的Nutch /噴射器所生成的數據文件運行示例輸出:

------------------------ 
https://wiki.openoffice.org/wiki/Ru/FAQ 
Version: 7 
Status: 1 (db_unfetched) 
Fetch time: Sun Apr 13 16:12:59 MDT 2014 
Modified time: Wed Dec 31 17:00:00 MST 1969 
Retries since fetch: 0 
Retry interval: 2592000 seconds (30 days) 
Score: 1.0 
Signature: null 
Metadata: 

------------------------ 
https://www.bankhapoalim.co.il/ 
Version: 7 
Status: 1 (db_unfetched) 
Fetch time: Sun Apr 13 16:12:59 MDT 2014 
Modified time: Wed Dec 31 17:00:00 MST 1969 
Retries since fetch: 0 
Retry interval: 2592000 seconds (30 days) 
Score: 1.0 
Signature: null 
Metadata: 

謝謝!

+0

其實你的解決方案比@ khomich的更類似於不同:看起來唯一的變化就是在對Reader構造函數的調用中。本來很高興指出這一點。 – javadba 2015-02-08 02:00:09