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的更多相关文章

  1. write & read a sequence file(基于全新2.2.0API)

    write & read a sequence file write & read a sequence file import java.io.IOException; import ...

  2. MapReduce库类

    Hadoop除了可以让开发人员自行编写map函数和reduce函数,还提供一些常用函数(mapper.reducer和partitioner)的类库,这些类位于 org.apache.hadoop.m ...

  3. 手动创建binary log files和手动编辑binary log index file会有什么影响

    基本环境:官方社区版MySQL 5.7.19 一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括b ...

  4. Files and Directories

    Files and Directories Introduction     In the previous chapter we coveredthe basic functions that pe ...

  5. 609. Find Duplicate File in System

    Given a list of directory info including directory path, and all the files with contents in this dir ...

  6. mvc file控件无刷新异步上传操作

    前言 上传文件应该是很常见必不可少的一个操作,网上也有很多提供的上传控件.今天遇到一个问题:input控件file无法进行异步无刷新上传.真真的感到别扭.所以就尝试这去处理了一下.主要分三个部分:上传 ...

  7. 小型文件数据库 (a file database for small apps) SharpFileDB

    小型文件数据库 (a file database for small apps) SharpFileDB For english version of this article, please cli ...

  8. SharpFileDB - a file database for small apps

    SharpFileDB - a file database for small apps 本文中文版在此处. I'm not an expert of database. Please feel fr ...

  9. 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 ...

随机推荐

  1. DataSet与DataTable对象

    DataSet与DataTable对象 摘自:http://www.cnblogs.com/fttbfttb/articles/1509662.html DataSet对象 DataSet是ADO.N ...

  2. 参数化命令相关知识点(防止Sql注入)

    一: 使用参数化命令查询DAL类: public DataTable StudentDAL(string name,string gender) { string str="连接字符串&qu ...

  3. 关于 hangfire 的权限问题

    hangfire 是一个分布式后台执行服务. 官网:http://hangfire.io/ 我看中hangfire的地方是 1:使用简单 2:多种持久化保存方案.支持sqlserver ,msmq等 ...

  4. 泛函编程(9)-异常处理-Option

    Option是一种新的数据类型.形象的来描述:Option就是一种特殊的List,都是把数据放在一个管子里:然后在管子内部对数据进行各种操作.所以Option的数据操作与List很相似.不同的是Opt ...

  5. JavaScript来实现打开链接页面(转载)

    在页面中的链接除了常规的方式以外,如果使用javascript,还有很多种方式,下面是一些使用javascript,打开链接的几种方式: 1.使用window的open方法打开链接,这里可是在制定页面 ...

  6. rabbitmq队列中消息过期配置

    最近公司某个行情推送的rabbitmq服务器由于客户端异常导致rabbitmq队列中消息快速堆积,还曾导致过内存积压导致rabbitmq客户端被block的情况.考虑到行情信息从业务上来说可以丢失部分 ...

  7. 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)

    即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...

  8. oGrid 初探

    oGrid 是个还蛮有趣的 pure JavaScript grid 控件 code 并不多而且是纯 JavaScript 写成,一条小龙觉得还算蛮好理解,不像其他几乎都是用 Jquery 为 bas ...

  9. 趣味题:恺撒Caesar密码(c++实现)

    描述:Julius Caesar 生活在充满危险和阴谋的年代.为了生存,他首次发明了密码,用于军队的消息传递.假设你是Caesar 军团中的一名军官,需要把Caesar 发送的消息破译出来.并提供给你 ...

  10. PHP读取Excel文件内容

    PHP读取Excel文件内容   项目需要读取Excel的内容,从百度搜索了下,主要有两个选择,第一个是PHPExcelReader,另外一个是PHPExcel.   PHPExcelReader比较 ...