【Hadoop】MapReduce自定义分区Partition输出各运营商的手机号码
MapReduce和自定义Partition
MobileDriver主类
package Partition;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
public class MobileDriver {
public static void main(String[] args) {
String[] paths = {"F:\\mobile.txt", "F:\\output"};
JobUtils.commit(paths, true, 3, MobileDriver.class,
MobileMapper.class, Text.class, NullWritable.class, MobilePartition.class,
MobileReduce.class, Text.class, NullWritable.class);
}
}
JobUtils工具类
package Partition;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.File;
import java.io.IOException;
public class JobUtils {
private static Configuration conf;
static {
conf = new Configuration();
}
/**
* 提交job
*
* @param paths 输入输出路径数组
* @param isPartition 是否包含自定义分区类
* @param reduceNumber reduce数量(若自定义分区为true,则此项必须>=自定义分区数)
* @param params 可变参数
*/
public static void commit(String[] paths, boolean isPartition, int reduceNumber, Class... params) {
try {
Job job = Job.getInstance(conf);
job.setJarByClass(params[0]);
job.setMapperClass(params[1]);
job.setOutputKeyClass(params[2]);
job.setOutputValueClass(params[3]);
if (isPartition) {
job.setPartitionerClass(params[4]);//设置自定义分区;
}
if (reduceNumber > 0) {
job.setNumReduceTasks(reduceNumber);
job.setReducerClass(params[5]);
job.setOutputKeyClass(params[6]);
job.setOutputValueClass(params[7]);
} else {
job.setNumReduceTasks(0);
}
deleteDirectory(paths[1]);
FileInputFormat.setInputPaths(job, new Path(paths[0]));
FileOutputFormat.setOutputPath(job, new Path(paths[1]));
job.waitForCompletion(true);
} catch (InterruptedException | ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
//输出目录存在则删除
public static void deleteDirectory(String path) {
File pFile = new File(path);
if (!pFile.exists()) {
return;
}
if ((pFile.isDirectory() && pFile.listFiles().length == 0) || pFile.isFile()) {
pFile.delete();
} else {
for (File file : pFile.listFiles()) {
if (file.isDirectory()) {
deleteDirectory(file.getAbsolutePath());
} else {
file.delete();
}
}
}
pFile.delete();
}
}
Map自定义类
package 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;
public class MobileMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] mobiles = line.split("\t");
for (String mobile : mobiles) {
//不满足11位手机号进行过滤
if (mobile.length() == 11) {
context.write(new Text(mobile), NullWritable.get());
}
}
}
}
Reduce自定义类
package Partition;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class MobileReduce 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());
}
}
Partition自定义分区类
package Partition;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
import java.util.Arrays;
public class MobilePartition extends Partitioner<Text, NullWritable> {
@Override
public int getPartition(Text text, NullWritable nullWritable, int i) {
String line = text.toString();
String flag = line.substring(0, 3);
if (Arrays.asList(Mobile.CHINA_MOBILE).contains(flag)) {
return 0;//移动
} else if (Arrays.asList(Mobile.CHINA_UNICOM).contains(flag)) {
return 1;//联通
} else {
return 2;//电信
}
}
}
【Hadoop】MapReduce自定义分区Partition输出各运营商的手机号码的更多相关文章
- Hadoop mapreduce自定义分区HashPartitioner
本文发表于本人博客. 在上一篇文章我写了个简单的WordCount程序,也大致了解了下关于mapreduce运行原来,其中说到还可以自定义分区.排序.分组这些,那今天我就接上一次的代码继续完善实现自定 ...
- Hadoop mapreduce自定义分组RawComparator
本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...
- Hadoop学习之路(6)MapReduce自定义分区实现
MapReduce自带的分区器是HashPartitioner 原理:先对map输出的key求hash值,再模上reduce task个数,根据结果,决定此输出kv对,被匹配的reduce任务取走. ...
- [Hadoop] - Mapreduce自定义Counter
在Hadoop的MR程序开发中,经常需要统计一些map/reduce的运行状态信息,这个时候我们可以通过自定义Counter来实现,这个实现的方式是不是通过配置信息完成的,而是通过代码运行时检查完成的 ...
- 【Hadoop】Hadoop MR 自定义分组 Partition机制
1.概念 2.Hadoop默认分组机制--所有的Key分到一个组,一个Reduce任务处理 3.代码示例 FlowBean package com.ares.hadoop.mr.flowgroup; ...
- Hadoop mapreduce自定义排序WritableComparable
本文发表于本人博客. 今天继续写练习题,上次对分区稍微理解了一下,那根据那个步骤分区.排序.分组.规约来的话,今天应该是要写个排序有关的例子了,那好现在就开始! 说到排序我们可以查看下hadoop源码 ...
- Hadoop MapReduce自定义数据类型
一 自定义数据类型的实现 1.继承接口Writable,实现其方法write()和readFields(), 以便该数据能被序列化后完成网络传输或文件输入/输出: 2.如果该数据需要作为主键key使用 ...
- 一起学Hadoop——使用自定义Partition实现hadoop部分排序
排序在很多业务场景都要用到,今天本文介绍如何借助于自定义Partition类实现hadoop部分排序.本文还是使用java和python实现排序代码. 1.部分排序. 部分排序就是在每个文件中都是有序 ...
- [MapReduce_8] MapReduce 中的自定义分区实现
0. 说明 设置分区数量 && 编写自定义分区代码 1. 设置分区数量 分区(Partition) 分区决定了指定的 Key 进入到哪个 Reduce 中 分区目的:把相同的 Key ...
随机推荐
- python_tkinter基本属性
1.外形尺寸 尺寸单位:只用默认的像素或者其他字符类的值!,不要用英寸毫米之类的内容. btn = tkinter.Button(root,text = '按钮') # 设置按钮尺寸,绝大多数默认单位 ...
- BZOJ5206 [Jsoi2017]原力[根号分治]
这是一个三元环计数的裸题,只是多了一个颜色的区分和权值的计算罢了. 有一种根号分治的做法(by gxz) 这种复杂度的证明特别显然,思路非常简单,不过带一个log,可以用unordered_map或者 ...
- invalid argument "type=bind,source=/tmp/tfserving/serving/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two_cpu," for "--mount" flag: invalid field '' must be a key=value pair
--mount参数后面不要有空格 空格去掉即可
- C# 异步编程 (12)
异步编程重要性 C# 5.0 提供了更强大的异步编程.添加两个新的关键字 async 和 await . 使用异步编程,方法调用是在后台运行(通常在线程或任务的帮助下),并且不会阻塞调用线程. 3种不 ...
- [Cypress] Use the Most Robust Selector for Cypress Tests
Which selectors your choose for your tests matter, a lot. In this lesson, we'll see the recommended ...
- 题解 [SDOI2010] 大陆争霸
题面 解析 这题似乎不是那么难啊 首先,显而易见, 如果要摧毁一个城市,必须要满足两个条件: 机器人摧毁了保护它的城市. 机器人到达了这个城市. 而这两个条件可以同时进行(毕竟有无数机器人) 那么显然 ...
- 创建节点--DOM树
创建节点 快捷键:innerhtml outerhtml innertext outertext ==============创建节点方法有两种:============== <script s ...
- 步骤五 · 4-9 解决getElementsByClassName()兼容性 未解决
前端零基础入门 2019版 / 步骤五 · 4-9 解决getElementsByClassName()兼容性
- mybatis sql语句中转义字符
问题: 在mapper ***.xml中的sql语句中,不能直接用大于号.小于号要用转义字符 解决方法: 1.转义字符串 小于号 < < 大于号 > & ...
- AtCoder Beginner Contest 137 D题【贪心】
[题意]一共有N个任务和M天,一个人一天只能做一个任务,做完任务之后可以在这一天之后的(Ai-1)天拿到Bi的工资,问M天内最多可以拿到多少工资. 链接:https://atcoder.jp/cont ...