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 ...
随机推荐
- Oracle数据库导入导出总结(dmp文件)
Oracle 10G 管理页面(Oracle Enterprise Manager 10g): http://localhost:1158/em http://localhost:1158/em/co ...
- TCMalloc优化MySQL、Nginx内存管理
TCMalloc的全称为Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员. 与标准的glibc库的Malloc相比,TCMalloc库在内存 ...
- php学习笔记:文件的上传(包含设置文件的上传大小限制)
今天写的是文件上传,前几天自学的正规则又忘记了,用了很笨的方法去判断格式,直接上代码: <?php /** * Created by PhpStorm. * User: Administrato ...
- 窗体==>>初始Windows程序
初识Windows程序 01.创建Windows程序(VS) 01.打开Visual Studio开发工具 02.选择"文件"→"新建"→"项目&qu ...
- 各个平台的mysql重启命令
linux平台及windows平台mysql重启方法 Linux下重启MySQL的正确方法: 1.通过rpm包安装的MySQL service mysqld restart 2.从源码包安装的MySQ ...
- iOS 线程相关-----绝对de干货
平时用线程总是知其然,而不知所以然,现在针对涉及到的有关线程的知识体系做了一个系统的整理,由于GCD平时用的也比较多,所以用了大量的空间来讲述这一块,其他的涉及的不是很多,也做了说明,真真切切的是一个 ...
- 对CSS进行wxss思路学习,display属性。
先来概要一下学习思路: 本系列内容,将针对微信小程序中的WXSS学习,所以在学习CSS时每一个知识点都在小程序IDE中进行实践,达到最好的学习效果. 由于wxss与CSS有些许不同,在学习CSS过程中 ...
- javascript --- 只继承于原型
正如上次所述,,出于效率考虑,我们应该尽可能的将一些可重用的属性和方法添加到原型中去. 如果养成了这个好习惯,我们仅仅依靠原型就能顺利的完成继承关系的构建了. 毕竟采用new her()方法将her的 ...
- 分形几何算法和实现(C语言)
初识分形 1.分形的含义: 英文单词Fractal,它是由美籍法国数学家曼德勃罗(Benoit Mandelbrot)创造出来的.其含义是不规则的.破碎的.分数的.曼德勃罗是想用此词来描述自然界中传统 ...
- IOS客户端Coding项目记录(四)
1:打开Xcode,然后闪退,报加载某库出现异常 如/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolc ...