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输出各运营商的手机号码的更多相关文章

  1. Hadoop mapreduce自定义分区HashPartitioner

    本文发表于本人博客. 在上一篇文章我写了个简单的WordCount程序,也大致了解了下关于mapreduce运行原来,其中说到还可以自定义分区.排序.分组这些,那今天我就接上一次的代码继续完善实现自定 ...

  2. Hadoop mapreduce自定义分组RawComparator

    本文发表于本人博客. 今天接着上次[Hadoop mapreduce自定义排序WritableComparable]文章写,按照顺序那么这次应该是讲解自定义分组如何实现,关于操作顺序在这里不多说了,需 ...

  3. Hadoop学习之路(6)MapReduce自定义分区实现

    MapReduce自带的分区器是HashPartitioner 原理:先对map输出的key求hash值,再模上reduce task个数,根据结果,决定此输出kv对,被匹配的reduce任务取走. ...

  4. [Hadoop] - Mapreduce自定义Counter

    在Hadoop的MR程序开发中,经常需要统计一些map/reduce的运行状态信息,这个时候我们可以通过自定义Counter来实现,这个实现的方式是不是通过配置信息完成的,而是通过代码运行时检查完成的 ...

  5. 【Hadoop】Hadoop MR 自定义分组 Partition机制

    1.概念 2.Hadoop默认分组机制--所有的Key分到一个组,一个Reduce任务处理 3.代码示例 FlowBean package com.ares.hadoop.mr.flowgroup; ...

  6. Hadoop mapreduce自定义排序WritableComparable

    本文发表于本人博客. 今天继续写练习题,上次对分区稍微理解了一下,那根据那个步骤分区.排序.分组.规约来的话,今天应该是要写个排序有关的例子了,那好现在就开始! 说到排序我们可以查看下hadoop源码 ...

  7. Hadoop MapReduce自定义数据类型

    一 自定义数据类型的实现 1.继承接口Writable,实现其方法write()和readFields(), 以便该数据能被序列化后完成网络传输或文件输入/输出: 2.如果该数据需要作为主键key使用 ...

  8. 一起学Hadoop——使用自定义Partition实现hadoop部分排序

    排序在很多业务场景都要用到,今天本文介绍如何借助于自定义Partition类实现hadoop部分排序.本文还是使用java和python实现排序代码. 1.部分排序. 部分排序就是在每个文件中都是有序 ...

  9. [MapReduce_8] MapReduce 中的自定义分区实现

    0. 说明 设置分区数量 && 编写自定义分区代码 1. 设置分区数量 分区(Partition) 分区决定了指定的 Key 进入到哪个 Reduce 中 分区目的:把相同的 Key ...

随机推荐

  1. Series和Dataframe分组时使用groupby函数的区别

    1. Dataframe分组用groupby("列名")或者groupby(["列名1","列名2"]) import pandas as ...

  2. python--openCV--其它

    t1=cv2.getTickCount() # 记录当前时间,以时钟周期计算 t2=cv2.getTickFrequency() #返回时钟周期,返回CPU的频率,返回CPU一秒中所走的时钟周期数

  3. yarn是什么?

    yarn是个包管理器.你可以通过它使用全世界开发者的代码, 或者分享自己的代码. 从 npm 安装软件包并保持相同的包管理流程. 优点:         1.速度超快. Yarn 缓存了每个下载过的包 ...

  4. (vue.js)Vue element tab 每个tab用一个路由来管理?

    (vue.js)Vue element tab 每个tab用一个路由来管理? 来源:网络整理     时间:2017/5/13 0:24:01     关键词:   关于网友提出的“ (vue.js) ...

  5. vue-cli3构建多页面应用

    创建一个项目hello-world vue create hello-worldcd hello-worldnpm run serve 在src目录下新建pages目录,在pages下新建页面 App ...

  6. AGC032F One Third

    很奇怪的一个题.看见了无从下手.概率期望好题. 给一个面积为 \(1\) 的圆,经过圆心随机幅角切直径 \(n\) 次,定义 \(f(x) = \min |S - \frac{1}{3}|\),其中 ...

  7. 树莓派安装QT(全部库包括)

    在网上现有的资料中大部分只有前两个命令,少量有三个命令,因此写下该博客 在树莓派上安装QT5的全部库,包括QtQuick.QtMultimedia库. sudo apt-get install qt5 ...

  8. 理解Cookie和Session

    HTTP 无状态协议 HTTP 本身是一个无状态的连接协议,无状态的意思是:每条请求/响应都是独立进行的,服务端每处理完一个客户端的请求之后就会断开连接,并且每条请求/响应与其之前(或之后)的请求/响 ...

  9. Configuring Windows for Certificate Logon

    Setting up a Windows Domain ( on AD) Installing Domain Controller Roles,During installing Active Dir ...

  10. 树及其衍生算法(Trees and tree algorithms)

    1,二叉树(Binary tree) 二叉树:每一个节点最多两个子节点,如下图所示: 相关概念:节点Node,路径path,根节点root,边edge,子节点 children,父节点parent,兄 ...