2011-02-23 34 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