Combine small files to Sequence file
Combine small files to sequence file or avro files are a good method to feed hadoop.
Small files in hadoop will take more namenode memory resource.
SequenceFileInputFormat 是一种Key value 格式的文件格式。
Key和Value的类型可以自己实现其序列化和反序列化内容。
SequenceFile示例内容:

其默认的key,value之间的分隔符 是\001,这个与hive文件的存储格式是匹配的,这样也方便直接把这种文件加载到hive里面。
以下的代码仅供参考作用,真实的项目中使用的时候,可以做适当的调整,以更高地节约资源和满足项目的需要。
示例代码如下:
package myexamples; import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.io.FileUtils;
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.Text; public class localf2seqfile {
/*
* Local folder has a lot of txt file
* we need handle it in map reduce
* so we want to load these files to one sequence file
* key: source file name
* value: file content
* */
static void write2Seqfile(FileSystem fs,Path hdfspath,HashMap<Text,Text> hm)
{
SequenceFile.Writer writer=null; try {
writer=SequenceFile.createWriter(fs, fs.getConf(), hdfspath, Text.class, Text.class); for(Map.Entry<Text,Text> entry:hm.entrySet())
writer.append(entry.getKey(),entry.getValue()); } catch (IOException e) {
e.printStackTrace();
}finally{
try{writer.close();}catch(IOException ioe){}
}
}
static HashMap<Text,Text> collectFiles(String localpath) throws IOException
{
HashMap<Text,Text> hm = new HashMap<Text,Text>();
File f = new File(localpath);
if(!f.isDirectory()) return hm;
for(File file:f.listFiles())
hm.put(new Text(file.getName()), new Text(FileUtils.readFileToString(file))); return hm;
}
static void readSeqFile(FileSystem fs, Path hdfspath) throws IOException
{
SequenceFile.Reader reader = new SequenceFile.Reader(fs,hdfspath,fs.getConf());
Text key = new Text();
Text value = new Text();
while (reader.next(key, value)) {
System.out.print(key +" ");
System.out.println(value);
}
reader.close(); }
public static void main(String[] args) throws IOException {
args = "/home/hadoop/test/sub".split(" "); Configuration conf = new Configuration();
conf.set("fs.default.name", "hdfs://namenode:9000"); FileSystem fs = FileSystem.get(conf);
System.out.println(fs.getUri());
Path file = new Path("/user/hadoop/seqfiles/seqdemo.seq");
if (fs.exists(file)) fs.delete(file,false);
HashMap<Text,Text> hm = collectFiles(args[0]);
write2Seqfile(fs,file,hm);
readSeqFile(fs,file); fs.close();
}
}
Combine small files to Sequence file的更多相关文章
- write & read a sequence file(基于全新2.2.0API)
write & read a sequence file write & read a sequence file import java.io.IOException; import ...
- MapReduce库类
Hadoop除了可以让开发人员自行编写map函数和reduce函数,还提供一些常用函数(mapper.reducer和partitioner)的类库,这些类位于 org.apache.hadoop.m ...
- 手动创建binary log files和手动编辑binary log index file会有什么影响
基本环境:官方社区版MySQL 5.7.19 一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括b ...
- Files and Directories
Files and Directories Introduction In the previous chapter we coveredthe basic functions that pe ...
- 609. Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- mvc file控件无刷新异步上传操作
前言 上传文件应该是很常见必不可少的一个操作,网上也有很多提供的上传控件.今天遇到一个问题:input控件file无法进行异步无刷新上传.真真的感到别扭.所以就尝试这去处理了一下.主要分三个部分:上传 ...
- 小型文件数据库 (a file database for small apps) SharpFileDB
小型文件数据库 (a file database for small apps) SharpFileDB For english version of this article, please cli ...
- SharpFileDB - a file database for small apps
SharpFileDB - a file database for small apps 本文中文版在此处. I'm not an expert of database. Please feel fr ...
- The Kernel Newbie Corner: Kernel Debugging with proc "Sequence" Files--Part 3
转载:https://www.linux.com/learn/linux-career-center/44184-the-kernel-newbie-corner-kernel-debugging-w ...
随机推荐
- PHP功能齐全的发送邮件类
下面这个类的功能则很强大,不但能发html格式的邮件,还可以发附件 <?php class Email { //---设置全局变量 var $mailTo = ""; // ...
- Microsecond and Millisecond C# Timer[转]
文章转至:http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer IntroductionAny ...
- *nix下传统编程入门之GCC
准备工作 注意:本文可能会让你失望,如果你有下列疑问的话:为什么要在终端输命令啊? GCC 是什么东西,怎么在菜单中找不到? GCC 不能有像 VC 那样的窗口吗?…… 那么你真正想要了解的可能是 a ...
- [译]PrestaShop开发者指南 第一篇 基础
# 第一篇 基础 PS(PrestaShop简称)一开始就设定了能够在它的基础上很简单的构建第三方模块的机制,让它成为一款具有极高定制性的电子商务软件. PS的可以在三个方面进行定制: * 主题 * ...
- 【Asphyre引擎】学习笔记(二)
转一篇火人论坛那边的一份学习文档,我简单排一下版,希望对入门者有帮助. 感谢China Yang,这份文档也帮助我快速入了门. 和我一起学 Asphyre Sphinx Framework v1.0. ...
- Android开发中的问题及相应解决(持续更新)
最近博客写的少了,以后还得经常更新才行. ------------------------------------------------------------ 1.特定业务需求下try cath ...
- Hosts文件小结
今天又遇到Hosts,小结一下: Hosts文件是什么? Hosts文件从表象上来看是一个没有扩展名的系统文件.其基本作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库&qu ...
- 树莓派版的家用NAS服务器
家里的文件越来越多,每个人的文件放得到处都是,需要的时候又找不到... 买个NAS服务器?太贵!太吵!太费电!... 好在我们有树莓派,自己动手,丰衣足食! 说做就做,主要分成以下三部分 加载双USB ...
- ubuntu下nagios配置
参考文献: http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html http://my.oschina.net/duangr/blo ...
- 安卓开发_浅谈OptionsMenus(选项菜单)
Android平台下所提供的菜单大体上可分为三类:选项菜单.上下文菜单和子菜单. 当Activity在前台运行时,如果用户按下手机上的Menu键,此时就会在屏幕低端弹出相应的选项菜单.但这个功能需要开 ...