使用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到外部文件 很多时候,我们在做性能测试之前,需要造数据,但是使用的这些参数化数据和生成的返回数据在后面的测试都会用的,所以我们需要在造数据过程中,将参 ...
随机推荐
- C语言的编译过程和GCC编译参数
C语言的编译一般有三个步骤: 预编译: gcc -E -o a.e a.c 预编译a.c文件,生成的目标文件名为a.e 预编译就是将include包含的头文件内容替换到C文件中,同时删除代码中没用的注 ...
- Newtonsoft.Json工具类
这个类用于序列化和反序列化类. 效果是当前最好的.微软都推荐使用.在建立MVC的里面已经引用了这个dll. 上面一篇文章要用到 SerializeHelper工具类 public class Seri ...
- nodejs发展
http://www.infoq.com/cn/news/2012/11/netease-nodejs-framework http://www.jlmonteagudo.com/2013/06/ja ...
- IPCS资源
ipcrm用法 ipcrm -M shmkey 移除用shmkey创建的共享内存段 ipcrm -m shmid 移除用shmid标识的共享内存段 ipcrm -Q msgkey 移除用ms ...
- Linux数据备份
今天需要用到备份网站,写了个脚本,备份了mysql和redis数据. #!/bin/bash BACKDIR="/home/tan/getll_backup" if [ ! -d ...
- 【VirtualDOM】
前沿技术解密——VirtualDOM miniflycn/qvd Matt-Esch/virtual-dom Facebook React 和 Web Components(Polymer)对比优势和 ...
- Myeclipse 保存jsp异常Save FailedCompilation unit name must end with .java, or one of the registered Java-like extensions
如图 解决方法:去掉jsp页面的调试断点
- 10.8 OSInstall破解
解决 无法完成您的购买,不兼容的问题 http://pan.baidu.com/share/link?shareid=2396730945&uk=1764629622
- NGU-学习笔记(1)-动态添加删除图集
现在 正在做unity的方向 不得不说我选的是UI方向 Unity中很有名的就是NGUI插件了.今天做了个ngui的简单背包系统.非常简陋..初学着 自己mark下 (1)预览 主要就是个 simpl ...
- 使用AppDelegate单例,解决子视图无法给父视图发送消息的问题
关于单例模式,我会在实验过后再开一个博客重点讲单例的使用,这里只是介绍我在PhotoForBingyan的照片滤镜的项目中使用AppDelegate单例的情况. 碰到的问题: 由于这个项目是个多视图的 ...