使用Partitioner实现输出到多个文件
1、需求
按学生的年龄段,将数据输出到不同的文件。这里我们分为三个年龄段:小于等于20岁、大于20岁小于等于50岁和大于50岁
2、实现
1、编写Partitioner,代码如下
public static class StudentPartitioner extends Partitioner<IntWritable, Text> {
@Override
public int getPartition(IntWritable key, Text value, int numReduceTasks) {
// 学生年龄
int ageInt = key.get(); // 默认指定分区 0
if (numReduceTasks == 0)
return 0; if (ageInt <= 20) { // 年龄小于等于20,指定分区0
return 0;
}else if (ageInt <= 50) { // 年龄大于20,小于等于50,指定分区1
return 1;
}else{ // 剩余年龄,指定分区2
return 2;
}
}
}
2、编写mapper
public static class StudentMapper extends Mapper<LongWritable, Text, IntWritable, Text>{
@Override
protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {
String[] studentArr = value.toString().split("\t"); if(StringUtils.isNotBlank(studentArr[1])){
/*
* 姓名 年龄(中间以tab分割)
* 张明明 45
*/
// 年龄
IntWritable pKey = new IntWritable(Integer.parseInt(studentArr[1].trim())); // 以年龄作为key输出
context.write(pKey, value);
}
}
}
3、编写reducer
public static class StudentReducer extends Reducer<IntWritable, Text, NullWritable, Text> {
@Override
protected void reduce(IntWritable key, Iterable<Text> values,Context context) throws IOException, InterruptedException {
for(Text value : values){
context.write(NullWritable.get(), value);
}
}
}
4、一些运行代码
@Override
public int run(String[] arg0) throws Exception {
// 读取配置文件
Configuration conf = new Configuration(); Path mypath = new Path(arg0[1]);
FileSystem hdfs = mypath.getFileSystem(conf);
if (hdfs.isDirectory(mypath)) {
hdfs.delete(mypath, true);
} // 新建一个任务
Job job = new Job(conf, "PartitionerDemo");
// 设置主类
job.setJarByClass(StudentPartitioner.class); // 输入路径
FileInputFormat.addInputPath(job, new Path(arg0[0]));
// 输出路径
FileOutputFormat.setOutputPath(job, new Path(arg0[1])); // Mapper
job.setMapperClass(StudentMapper.class);
// Reducer
job.setReducerClass(StudentReducer.class); // mapper输出格式
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class); // reducer输出格式
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class); //设置Partitioner类
job.setPartitionerClass(StudentPartitioner.class);
// reduce个数设置为3
job.setNumReduceTasks(3); //提交任务
return job.waitForCompletion(true)?0:1;
}
public static void main(String[] args0) throws Exception {
// 数据输入路径和输出路径
// String[] args0 = {
// "hdfs://ljc:9000/buaa/student/student.txt",
// "hdfs://ljc:9000/buaa/student/out/"
// };
int ec = ToolRunner.run(new Configuration(), new StudentAgePartitionerDemo(), args0);
System.exit(ec);
}
3、总结
Partitioner适用于事先知道分区数的情况下,比如像上面这个需求
缺点:
1、在作业运行之前需要知道分区数,也就是年龄段的个数,如果分区数未知,就无法操作。
2、一般来说,让应用程序来严格限定分区数并不好,因为可能导致分区数少或分区不均
使用Partitioner实现输出到多个文件的更多相关文章
- android源码环境下用mmm/mm编译模块,输出编译log到文件的方法
android源码环境下用mmm/mm编译模块,输出编译log到文件的方法 1,在android目录下直接用mmm命令编译, log信息保存在android目录下 mmm packages/apps/ ...
- Linux标准输入、输出和错误和文件重定向(转) --- good
标准输入.输出和错误 当我们在shell中执行命令的时候,每个进程都和三个打开的文件相联系,并使用文件描述符来引用这些文件.由于文件描述符不容易记忆,shell同时也给出了相应的文件名.下面就是这些文 ...
- hadoop1.2.1 MultipleOutputs将结果输出到多个文件或文件夹
hadoop1.2.1 MultipleOutputs将结果输出到多个文件或文件夹 博客分类:http://tydldd.iteye.com/blog/2053867 hadoop hadoop1 ...
- log4j.properties配置与将异常输出到Log日志文件实例
将异常输出到 log日志文件 实际项目中的使用: <dependencies> <dependency> <groupId>org.slf4j</groupI ...
- 写文件的工具类,输出有格式的文件(txt、json/csv)
import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io. ...
- Linux标准输入、输出和错误和文件重定向 专题
当我们在shell中执行命令的时候,每个进程都和三个打开的文件相联系,并使用文件描述符来引用这些文件.由于文件描述符不容易记忆,shell同时也给出了相应的文件名. 下面就是这些文件描述符及它们通常所 ...
- Log4j按级别输出日志到不同文件配置
1.自定义LogFileAppender类,继承DailyRollingFileAppender,实现Log4j按级别输出日志到不同文件. package com.liying.mango.commo ...
- Altium Designer 输出 gerber 光绘文件的详细说明
Altium Designer 输出 gerber 光绘文件的详细说明 PCB画好后,我们需要输出光绘文件交给制版厂家.由此,输出光绘文件的重要性就显出来了. 先复习一下介绍各层的定义吧,哈哈 (1) ...
- loadrunner脚本中参数化和返回值输出log到外部文件
loadrunner脚本中参数化和返回值输出log到外部文件 很多时候,我们在做性能测试之前,需要造数据,但是使用的这些参数化数据和生成的返回数据在后面的测试都会用的,所以我们需要在造数据过程中,将参 ...
随机推荐
- jsp表单提交中文乱码的解决
<%@ page language="Java" contentType="text/html; charset=utf-8 " pageEncod ...
- iOS 图片转NSData-b
iOS开发中 UIImage可能经常需要转为NSData 上传 传递等等 有两个比较常用的方法 UIImageJPEGRepresentation UIImagePNGRepresentation 第 ...
- (bug更正)利用KVC和associative特性在NSObject中存储键值
KVC 一直没仔细看过KVC的用法,想当然的认为可以在NSObject对象中存入任意键值对,结果使用时碰到问题了. 一个简单的位移动画: CAKeyframeAnimation *keyPosi=[C ...
- Uva_11762 Race to 1
题目链接 题意: 给一个数n, 每次从小于等于n的素数里选一个P, 如果能被n整除, 那么就n就变成n / P. 问: n 变成1的期望. 思路: 设小于等于n的素数有p 个, 其中是n的约数的有g个 ...
- jQuery组件写法
知识点: 什么是插件 jQuery插件的模式 jQuery插件的Lightweight Start模式(入门级插件模式) 8.1 插件(Plug-in) “插件”这个关键字,估计大家在日常生活中经常有 ...
- Application+Handle+Task
Application Application和Activity,Service一样,是android框架的一个系统组件,android系统会为每个程序运行时创建一个Application类的对象且仅 ...
- autoconf automake libtool
这是一个 autoconf / automake 的 "Hello World"gztt.ll@gmail.com 主要步骤是- 准备工程目录结构和程序- autoscan 生成 ...
- iOS获取键盘的高度
- 【poi】解决java导出excel 海量数据内存溢出问题
转自百度经验:http://jingyan.baidu.com/article/4853e1e5202c331909f72627.html 那里排版忒恶心,转来这里. 由于项目中有导出海量数据的需求, ...
- Spark:Master High Availability(HA)高可用配置的2种实现
Spark Standalone集群是Master-Slaves架构的集群模式,和大部分的Master-Slaves结构集群一样,存在着Master单点故障的问题.如何解决这个单点故障的问题,Spar ...