SequenceFile可以处理hdfs上大量小文件,它可以作为大量小文件的容器。HDFS和MapReduce是针对大文件优化的,所以通过SequenceFile类型将小文件包装起来可以获得更高效的存储和处理。存储
在SequenceFile中的键和值并不一定是Writable类型,只要能被Serialization序列化和反序列化,任何类型都可以。
SequenceFile的优点是:以键值对方式存储、支持压缩、合并大量小文件。
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://single32:9000"), conf);
Path targetPath = new Path("/sfs");
//在HDFS上创建不使用压缩的SequenceFile
final Option optPath = SequenceFile.Writer.file(targetPath);
final Option optKeyClass = SequenceFile.Writer.keyClass(Text.class);
final Option optValueClass = SequenceFile.Writer.valueClass(BytesWritable.class);
final SequenceFile.Writer writer = SequenceFile.createWriter(conf, optPath, optKeyClass, optValueClass);
final Collection<File> listFiles = FileUtils.listFiles(new File("/usr/local/"), new String[]{"txt"}, false);
Text key = null;
BytesWritable value = null;
for (File file : listFiles) {
key = new Text(file.getPath());
value = new BytesWritable(FileUtils.readFileToByteArray(file));
writer.append(key, value);
}
IOUtils.closeStream(writer);
//读取HDFS上指定目录下的SequenceFile文件
final SequenceFile.Reader reader = new SequenceFile.Reader(fs, targetPath, conf);
final Text outputKey = new Text();
final BytesWritable outputValue = new BytesWritable();
while(reader.next(outputKey, outputValue)){
final File file = new File("/usr/"+outputKey.toString());
FileUtils.writeByteArrayToFile(file, outputValue.getBytes());
}
IOUtils.closeStream(reader);
原文:http://www.cnblogs.com/mengyao/p/4456148.html