SequenceFile文件是Hadoop用来存储二进制形式的key-value对而设计的一种平面文件(Flat File)。在SequenceFile文件中,每一个key-value对被看做是一条记录(Record),基于Record的压缩策略,SequenceFile文件支持三种压缩类型:

NONE: 对records不进行压缩; (组合1)

RECORD: 仅压缩每一个record中的value值(不包括key); (组合2)

BLOCK: 将一个block中的所有records(包括key)压缩在一起;(组合3)

package test0820;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.VLongWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat; public class Test0829 { public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(Test0829.class); job.setMapperClass(WCMapper.class);
job.setReducerClass(WCReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(VLongWritable.class); // 设置输出类
job.setOutputFormatClass(SequenceFileOutputFormat.class); /**
* 设置sequecnfile的格式,对于sequencefile的输出格式,有多种组合方式,
* 从下面的模式中选择一种,并将其余的注释掉
*/

// 组合方式1:不压缩模式
SequenceFileOutputFormat.setOutputCompressionType(job, CompressionType.NONE); //组合方式2:record压缩模式,并指定采用的压缩方式 :默认、gzip压缩等
// SequenceFileOutputFormat.setOutputCompressionType(job,
// CompressionType.RECORD);
// SequenceFileOutputFormat.setOutputCompressorClass(job,
// DefaultCodec.class); //组合方式3:block压缩模式,并指定采用的压缩方式 :默认、gzip压缩等
// SequenceFileOutputFormat.setOutputCompressionType(job,
// CompressionType.BLOCK);
// SequenceFileOutputFormat.setOutputCompressorClass(job,
// DefaultCodec.class); FileInputFormat.addInputPaths(job, args[0]);
SequenceFileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true);
}
//map
public static class WCMapper extends
Mapper<LongWritable, Text, Text, VLongWritable> {
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] split = value.toString().split(":",2);
if(split.length!=1){
String[] splited = split[1].split(",");
for(String s : splited){
context.write(new Text(s), new VLongWritable(1L));
}
}
}
}
//reduce
public static class WCReducer extends Reducer<Text, VLongWritable, Text, VLongWritable>{
@Override
protected void reduce(Text key, Iterable<VLongWritable> v2s, Context context)
throws IOException, InterruptedException { long sum=0; for(VLongWritable vl : v2s){
sum += vl.get();
}
context.write(key, new VLongWritable(sum));
}
}
}

MR输入SequenceFile

当输入文件格式是SequenceFile的时候,要使用SequenceFileInputformat类。由于SequenceFile都是以key和value的二进制形式存放的(注意hadoop类型的二进制的解释方式和原始二进制不一样,会多一些维护信息),所以在读取SequenceFile文件时必须预先知道key和value对应的hadoop类型。

对于上面代码产生的SequenceFile结果文件,以SequenceFileInputformat类进行读取。其中key为Text类型,value为VLongWritable类型。

package test0820;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.VLongWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class SFInput02 {
public static void main(String[] args) throws Exception {
Job job = Job.getInstance(new Configuration());
job.setJarByClass(SFinput.class); job.setMapperClass(SFMapper.class);
job.setReducerClass(SFReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(VLongWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(VLongWritable.class); job.setInputFormatClass(SequenceFileInputFormat.class); SequenceFileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true);
}
public static class SFMapper extends Mapper<Text, VLongWritable,Text, VLongWritable> {
public void map(Text key, VLongWritable value, Context context)
throws IOException, InterruptedException {
context.write(key, value);
} }
//reduce
public static class SFReducer extends Reducer<Text, VLongWritable,Text, VLongWritable>{
@Override
protected void reduce(Text key, Iterable<VLongWritable> v2s,Context context)
throws IOException, InterruptedException {
for(VLongWritable vl : v2s){
context.write(key, vl);
}
}
}
}

如若不清楚SequenceFile文件中key和value的类型,可以使用SequenceFileAsTextInputFormat类。它将SequenceFile的key和value都转化成Text对象传入map中。

package test0820;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileAsTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class SFinput {
public static void main(String[] args) throws Exception {
Job job = Job.getInstance(new Configuration());
job.setJarByClass(SFinput.class); job.setMapperClass(SFMapper.class);
job.setReducerClass(SFReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); job.setInputFormatClass(SequenceFileAsTextInputFormat.class); SequenceFileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true);
}
public static class SFMapper extends Mapper<Text, Text,Text, Text> {
public void map(Text key, Text value, Context context)
throws IOException, InterruptedException {
context.write(key, value);
} }
//reduce
public static class SFReducer extends Reducer<Text, Text,Text,Text>{
@Override
protected void reduce(Text key, Iterable<Text> v2s,Context context)
throws IOException, InterruptedException {
for(Text text : v2s){
context.write(key, text);
}
}
}
}

最后还有一种sequencefileAsBinaryInputFormat 类,它将SequenceFile中的key和value都以原始二进制的形式封装在byteswritable对象中传给map,如何对二进制数据进行解释是map函数编写者的工作。

MR案例:输出/输入SequenceFile的更多相关文章

  1. MR案例:倒排索引 && MultipleInputs

    本案例采用 MultipleInputs类 实现多路径输入的倒排索引.解读:MR多路径输入 package test0820; import java.io.IOException; import j ...

  2. MR案例:Reduce-Join

    问题描述:两种类型输入文件:address(地址)和company(公司)进行一对多的关联查询,得到地址名(例如:Beijing)与公司名(例如:Beijing JD.Beijing Red Star ...

  3. MR案例:倒排索引

    1.map阶段:将单词和URI组成Key值(如“MapReduce :1.txt”),将词频作为value. 利用MR框架自带的Map端排序,将同一文档的相同单词的词频组成列表,传递给Combine过 ...

  4. MR案例:小文件处理方案

    HDFS被设计来存储大文件,而有时候会有大量的小文件生成,造成NameNode资源的浪费,同时也影响MapReduce的处理效率.有哪些方案可以合并这些小文件,或者提高处理小文件的效率呢? 1). 所 ...

  5. MR案例:CombineFileInputFormat

    CombineFileInputFormat是一个抽象类.Hadoop提供了两个实现类CombineTextInputFormat和CombineSequenceFileInputFormat. 此案 ...

  6. 解读:MR多路径输入

    对于在一个MR-Job中使用多路径作为输入文件,一般有三种方法: 1).多次调用,加载不同路径: import org.apache.hadoop.mapreduce.lib.input.FileIn ...

  7. MR案例:定制InputFormat

    数据输入格式 InputFormat类用于描述MR作业的输入规范,主要功能:输入规范检查(比如输入文件目录的检查).对数据文件进行输入切分和从输入分块中将数据记录逐一读取出来.并转化为Map的输入键值 ...

  8. 【VB超简单入门】五、基本输出输入

    之前讲了VB IDE的基本操作和概念,接下来要开始将VB语言的编程了. 程序最重要的部分是输出和输入,输入数据,经过计算机处理,再输出结果.本文将介绍两种最基本的输出输入方法,分别是Print.Msg ...

  9. Java基础(5)- 输出输入

    输出输入 public class Input { public static void main (String[] args){ try { /** * 打开文件流进行读取 */ Scanner ...

随机推荐

  1. java前后台开发之后台自动上传下载

    package downloadTest; import java.io.BufferedReader; import java.io.File; import java.io.FileInputSt ...

  2. 巨蟒python全栈开发-第11阶段 devops-git&&openpyxl2

    大纲 1.git分支 2.git tag 3.git 忽略文件 4.正则表达式 5.openpyxl写数据 6.openpyxl读数据 1.git分支 2.git tag 3.git 忽略文件 4.正 ...

  3. 分享:宽恕的艺术 Forgive

    宽恕的艺术 To forgive may be divine, but no one ever said it was easy. 宽恕是神圣的,但是没有人说很容易做到宽恕别人. When someo ...

  4. HDU_5538_House Building

    House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  5. 百度jQuery库

    <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.js"></script>

  6. hook Extending the Framework Core

    w Task Scheduling - Laravel - The PHP Framework For Web Artisanshttps://laravel.com/docs/5.4/schedul ...

  7. HTTP Transaction Delays

    w客户端.服务器超载 HTTP The Definitive Guide 与建立TCP连接以及传输请求和相应报文的时间相比,事务处理的时间是很短的.除非客户端或服务器超载或正在处理复杂的动态资源,否则 ...

  8. Django系列

    1.Django框架 2.Django restframework 其他 django之contenttype

  9. 10张Gif动图让你弄懂递归等概念

    图像(包括动图)是传递信息的一种高效方式,往往能增强表象.记忆与思维等方面的反应强度.所谓一图胜千言,说的就是这个道理. 今天为大家整理了十张动图GIFS,有助于认识循环.递归.二分检索等概念的具体运 ...

  10. 【opencv入门篇】 10个程序快速上手opencv【上】

    导言:本系列博客目的在于能够在vs快速上手opencv,理论知识涉及较少,大家有兴趣可以查阅其他博客深入了解相关的理论知识,本博客后续也会对图像方向的理论进一步分析,敬请期待:) PS:官方文档永远是 ...