mapreduce分区
本次分区是采用项目垃圾分类的csv文件,按照小于4的分为一个文件,大于等于4的分为一个文件
源代码:
PartitionMapper.java:
package cn.idcast.partition; import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException;
/*
K1:行偏移量 LongWritable
v1:行文本数据 Text k2:行文本数据 Text
v2:NullWritable
*/ public class PartitionMapper extends Mapper<LongWritable,Text, Text, NullWritable> {
//map方法将v1和k1转为k2和v2
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
context.write(value,NullWritable.get());
}
}
PartitionerReducer.java:
package cn.idcast.partition; import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException; /*
k2: Text
v2: NullWritable k3: Text
v3: NullWritable
*/
public class PartitionerReducer extends Reducer<Text, NullWritable,Text, NullWritable> {
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
context.write(key,NullWritable.get());
}
}
MyPartitioner.java:
package cn.idcast.partition; import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner; public class MyPartitioner extends Partitioner<Text, NullWritable> {
/*
1:定义分区规则
2:返回对应的分区编号 */
@Override
public int getPartition(Text text, NullWritable nullWritable, int numPartitions) {
//1:拆分行文本数据(k2),获取垃圾分类数据的值
String[] split = text.toString().split(",");
String numStr=split[1]; //2:判断字段与15的关系,然后返回对应的分区编号
if(Integer.parseInt(numStr)>=4){
return 1;
}
else{
return 0;
}
}
}
JobMain.java:
package cn.idcast.partition; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; import java.net.URI; public class JobMain extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
//1:创建Job任务对象
Job job = Job.getInstance(super.getConf(), "partition_mapreduce");
//如果打包运行出错,则需要加该配置
job.setJarByClass(cn.idcast.mapreduce.JobMain.class);
//2:对Job任务进行配置(八个步骤)
//第一步:设置输入类和输入的路径
job.setInputFormatClass(TextInputFormat.class);
TextInputFormat.addInputPath(job,new Path("hdfs://node1:8020/input"));
//第二部:设置Mapper类和数据类型(k2和v2)
job.setMapperClass(PartitionMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
//第三步:指定分区类
job.setPartitionerClass(MyPartitioner.class);
//第四、五、六步
//第七步:指定Reducer类和数据类型(k3和v3)
job.setReducerClass(PartitionerReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//设置ReduceTask的个数
job.setNumReduceTasks(2);
//第八步:指定输出类和输出路径
job.setOutputFormatClass(TextOutputFormat.class);
Path path=new Path("hdfs://node1:8020/out/partition_out");
TextOutputFormat.setOutputPath(job,path);
//获取FileSystem
FileSystem fs = FileSystem.get(new URI("hdfs://node1:8020/partition_out"),new Configuration());
//判断目录是否存在
if (fs.exists(path)) {
fs.delete(path, true);
System.out.println("存在此输出路径,已删除!!!");
}
//3:等待任务结束
boolean bl = job.waitForCompletion(true);
return bl?0:1;
} public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
//启动一个job任务
int run = ToolRunner.run(configuration, new JobMain(), args);
System.exit(run);
}
}
在hadoop或者本地运行结果:
1.均为4-16的文件

2.均为1-3的文件

mapreduce分区的更多相关文章
- Hadoop Mapreduce分区、分组、二次排序过程详解[转]
原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动 (1)最简单的过程: map - reduce (2) ...
- hadoop2.2.0 MapReduce分区
package com.my.hadoop.mapreduce.partition; import java.util.HashMap;import java.util.Map; import org ...
- Hadoop Mapreduce分区、分组、二次排序
1.MapReduce中数据流动 (1)最简单的过程: map - reduce (2)定制了partitioner以将map的结果送往指定reducer的过程: map - partiti ...
- MapReduce分区和排序
一.排序 排序: 需求:根据用户每月使用的流量按照使用的流量多少排序 接口-->WritableCompareable 排序操作在hadoop中属于默认的行为.默认按照字典殊勋排序. 排序的分类 ...
- MapReduce分区的使用(Partition)
MapReduce中的分区默认是哈希分区,根据map输出key的哈希值做模运算,如下 int result = key.hashCode()%numReduceTask; 如果我们需要根据业务需求来将 ...
- Hadoop Mapreduce分区、分组、二次排序过程详解
转载:http://blog.tianya.cn/m/post.jsp?postId=53271442 1.MapReduce中数据流动 (1)最简单的过程: map - reduce (2)定制了 ...
- MapReduce分区数据倾斜
什么是数据倾斜? 数据不可避免的出现离群值,并导致数据倾斜,数据倾斜会显著的拖慢MR的执行速度 常见数据倾斜有以下几类 1.数据频率倾斜 某一个区域的数据量要远远大于其他区域 2.数据大小倾斜 ...
- YARN集群的mapreduce测试(五)
将user表计算后的结果分区存储 测试准备: 首先同步时间,然后master先开启hdfs集群,再开启yarn集群:用jps查看: master上: 先有NameNode.SecondaryNameN ...
- spark shuffle:分区原理及相关的疑问
一.分区原理 1.为什么要分区?(这个借用别人的一段话来阐述.) 为了减少网络传输,需要增加cpu计算负载.数据分区,在分布式集群里,网络通信的代价很大,减少网络传输可以极大提升性能.mapreduc ...
随机推荐
- Web安全学习
项目地址(参考):https://websec.readthedocs.io/zh/latest/basic/history.html 本文只能充当目录简介,具体还要自己深入学习. 序章 Web技术演 ...
- linux 文件查找 find
find 是实时查找工具,通过遍历指定路径完成文件查找 特点: 查找速度略慢 精确查找 实时查找 查找条件丰富 1.对每个目录先处理目录内的文件,再处理目录本身 find /data/test -de ...
- EasySwoole-延迟队列-取消订单
场景: 在用户要支付订单的时候,如果超过30分钟未支付,会把订单关掉.当然我们可以做一个定时任务,每个一段时间来扫描未支付的订单, 如果该订单超过支付时间就关闭,但是在数据量小的时候并没有什么大的问题 ...
- 理解并手写 bind() 函数
有了对call().apply()的前提分析,相信bind()我们也可以手到擒来. 参考前两篇:'对call()函数的分析' 和 '对apply()函数的分析',我们可以先得到以下代码: Functi ...
- Java子类继承父类的执行顺序
父类的静态代码块(static) 子类的静态代码块(static) 父类的非静态代码块(父类成员初始化) 父类的构造方法 子类的非静态代码块(子类成员初始化) 子类的构造方法
- CentOS7.5环境下安装配置GitLab
1. 安装依赖软件 yum -y install policycoreutils openssh-server openssh-clients postfix 2.设置postfix开机自启,并启动, ...
- 使用阿里云镜像站NTP服务搭建NTP服务器(基于CentOS 7系统)
镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 一.NTP服务器介绍 网络时间协议(Network Time Protocol,NTP)服务器,也就是日常所说的NTP服务器,用来提供同步时间服务 ...
- Python GUI tkinter 学习笔记(二)
第二个程序 # -*- coding: utf-8 -*- from Tkinter import * class App: def __init__(self, master): # frame 创 ...
- sublime settings
{ "font_face": "Monaco", // 编辑器的字体 "font_size": 13, // 字号 "highli ...
- C++ md5 函数
转 http://www.zedwood.com/article/cpp-md5-function MD5 is no longer considered cryptographically safe ...