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、一般来说,让应用程序来严格限定分区数并不好,因为可能导致分区数少或分区不均

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
如果,您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是【刘超★ljc】。

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

实现代码及数据:下载

使用Partitioner实现输出到多个文件的更多相关文章

  1. android源码环境下用mmm/mm编译模块,输出编译log到文件的方法

    android源码环境下用mmm/mm编译模块,输出编译log到文件的方法 1,在android目录下直接用mmm命令编译, log信息保存在android目录下 mmm packages/apps/ ...

  2. Linux标准输入、输出和错误和文件重定向(转) --- good

    标准输入.输出和错误 当我们在shell中执行命令的时候,每个进程都和三个打开的文件相联系,并使用文件描述符来引用这些文件.由于文件描述符不容易记忆,shell同时也给出了相应的文件名.下面就是这些文 ...

  3. hadoop1.2.1 MultipleOutputs将结果输出到多个文件或文件夹

    hadoop1.2.1 MultipleOutputs将结果输出到多个文件或文件夹 博客分类:http://tydldd.iteye.com/blog/2053867 hadoop   hadoop1 ...

  4. log4j.properties配置与将异常输出到Log日志文件实例

    将异常输出到 log日志文件 实际项目中的使用: <dependencies> <dependency> <groupId>org.slf4j</groupI ...

  5. 写文件的工具类,输出有格式的文件(txt、json/csv)

    import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io. ...

  6. Linux标准输入、输出和错误和文件重定向 专题

    当我们在shell中执行命令的时候,每个进程都和三个打开的文件相联系,并使用文件描述符来引用这些文件.由于文件描述符不容易记忆,shell同时也给出了相应的文件名. 下面就是这些文件描述符及它们通常所 ...

  7. Log4j按级别输出日志到不同文件配置

    1.自定义LogFileAppender类,继承DailyRollingFileAppender,实现Log4j按级别输出日志到不同文件. package com.liying.mango.commo ...

  8. Altium Designer 输出 gerber 光绘文件的详细说明

    Altium Designer 输出 gerber 光绘文件的详细说明 PCB画好后,我们需要输出光绘文件交给制版厂家.由此,输出光绘文件的重要性就显出来了. 先复习一下介绍各层的定义吧,哈哈 (1) ...

  9. loadrunner脚本中参数化和返回值输出log到外部文件

    loadrunner脚本中参数化和返回值输出log到外部文件 很多时候,我们在做性能测试之前,需要造数据,但是使用的这些参数化数据和生成的返回数据在后面的测试都会用的,所以我们需要在造数据过程中,将参 ...

随机推荐

  1. paramiko模块实现堡垒机

    通过SSHClient 执行命令 """通过用户名密码验证""" import paramiko # 创建 SSH 对象 ssh = par ...

  2. (转) 关于成为linux运维工程师需要掌握的技能

    曾经在一年多前写了一篇关于要成为linux运维需要掌握哪些技能和工具的贴子,然后不小心被好几个网友抄袭转发到其它网站上,当然有些是认识的,最后还很幸运地被某些热心的学习者把那段内容剪下来当作圣经般的参 ...

  3. LINQ小记

     //LINQ 根据长度查询出来            string[] strs = new string[] { "1", "22", "333& ...

  4. Objective-C property属性解析

    @interface … @property (原子性,可写性,内存管理) id name; @end 原子性:    nonatomic, atomic   默认atomic 可写性:    rea ...

  5. wampsever 数据库初体验

    Wamp就是Windos Apache Mysql PHP集成安装环境,即在window下的apache.php和mysql的服务器软件.PHP扩展.Apache模块,开启/关闭鼠标点点就搞定,再 也 ...

  6. CentOS 6.0图解网络安装全过程

    转自CentOS 6.0图解网络安装全过程 国内镜像站点(东北大学.网易) 网易镜像站点:http://mirrors.163.com/centos/6.0/isos/ 中科大镜像站点:http:// ...

  7. 如何在DJANGO里,向有外键(一对多和多对多)的DB里插入数据?

    需要插入的数据表结构如下: class UserInfo(models.Model): user_id =models.AutoField(primary_key=True) user_name=mo ...

  8. css学习笔记一

    1.在css开头用* {margin:0;padding:0;}可以清除所有样式 2.在css中table,th,td {padding:0;}效果等同于cellpadding="0″. 3 ...

  9. AlarmManager.setRepeating将不再准确

    背景: 当我们想让Android应用程序定时为做一件工作时,我们往往会在一个BroadcastReceiver中使用AlarmManager.setRepeating()方法来实现.在API 19(即 ...

  10. C 常用的输入输出 栈 哈希 文件写读 实现 字符串处理

    #include "stdafx.h"#include <stdio.h>#include <string.h>#include <stdlib.h& ...