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 ...
随机推荐
- document.documentElement.clientWidth
document.documentElement.clientWidth 摘自:http://blog.sina.com.cn/s/blog_6f1f9ead0100n1f6.html 关于获取各种浏 ...
- 浅谈ImageList
ImageList组件用了很久,但是一直不太清楚它的实现原理,今天专门特意花了时间倒腾了下,终于弄明白了!于是在这里和大家分享下! 在设计页面中打卡工具箱-组件 找到ImageList组件,将它直接拖 ...
- 运行QQ出现initialization failure 0x0000000c错误和浏览器上不了网
出现QQ出现initialization failure 0x0000000c错误和浏览器上不了网的问题,原因是关机的时候没有正常关闭导致的. 解决方法: 1.我们在开始菜单栏中的附件中找到“命令提示 ...
- javascript中怎样验证密码是否含有特殊符号、数字、大小写字母,长度是否大于6小于12
今天在温习了一下以前学过的知识,一般常用验证密码是通过正则表达式或是通过ASCII 一.用AscII码来验证
- 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel)
[源码下载] 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel) 作者:webabcd 介绍重新想象 Windows 8 Store ...
- dapper的增、删、查改的CodeSmith模板
<%@ Template Language="C#" TargetLanguage="Text" %> <%@ Property Name=& ...
- Android图像处理之Bitmap类
Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图像剪切.旋转.缩放等操作,并可以指定格式保存图像文件.本文从应用的角度,着重介绍怎么用Bitmap来实现 ...
- 利用Handler访问网络数据
废话不多白吃,代码如下: 1.MainActivity package com.yz.day11_22_handler;import android.app.Activity;import andro ...
- 最简MacOs10.8安装
虚拟机中安装Mac Os X的方法网上很多很多,但是对刚接触的朋友来讲肯定不是一件容易的事,这个自己深有体会,包括去年已经装好过,今年再找教程安装都装不起来,期间还出现了各种问题,幸好去年装好之后备份 ...
- Objective-C 代码规范(Code Style)
我们写出来的代码会给很多人看,为了使代码清晰简洁,方便阅读理解,都会统一遵从一定的代码规范,Objective-C同样如此. 主要参考规范: 1.Google Objective-C Style Gu ...