Hadoop中的序列文件

8
这些序列文件是如何生成的?我在这里看到了一个关于序列文件的链接。
http://wiki.apache.org/hadoop/SequenceFile

这些是使用默认的Java序列化程序编写的吗?我该如何读取一个序列文件?

这里的关键类和值类是什么?它们从哪里访问?请帮我解决这个问题。提前致谢。 - user1237868
2个回答

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 的Maven依赖中:

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

5
感谢Lev Khomich的答案,我的问题已经解决。
然而,该解决方案已经过时了,新的API提供了更多功能,并且也更易于使用。
查看hadoop.io.SequenceFile的源代码,请点击这里
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 / injector生成的数据文件运行的示例输出,其中包含额外信息:

------------------------
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: 

谢谢!


实际上,您的解决方案与@khomich的解决方案非常相似:唯一的变化似乎在对Reader构造函数的调用中。指出这一点会更好。 - WestCoastProjects

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接