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. body element height id small, but the backgroud color is full screen

    http://www.cnblogs.com/xiaoyuersdch/p/9156240.html ------------------------------------------------- ...

  2. deferred shading , tile deferred, cluster forward 对tranparent支持问题的思考

    cluster对 trans的支持我大概理解了 http://efficientshading.com/wp-content/uploads/tiled_shading_siggraph_2012.p ...

  3. es高级用法之冷热分离

    背景 用户需求:近期数据查询速度快,较远历史数据运行查询速度慢? 对于开发人员而言即数据的冷热分离,实现此功能有2个前提条件: 硬件:处理速度不同的硬件,最起码有读写速度不同的硬盘,如SSD.机械硬盘 ...

  4. vs2015下载

    VS2015 专业版下载链接http://download.microsoft.com/download/B/8/9/B898E46E-CBAE-4045-A8E2-2D33DD36F3C4/vs20 ...

  5. MFC 列表控件List Control

    一般用带有表头的报表模式,所以属性:View设为Report // OnInitDialog()中 CString str[] = {TEXT("姓名"),TEXT("性 ...

  6. ubuntu配置pip3以及scrapy

    .安装支持pip3 sudo apt-get install python3-pip .安装scrapy 首先需要安装scrapy依赖项,否则scrapy安装失败,执行如下命令: sudo apt-g ...

  7. 我理解的windows中断管理

    只谈外部中断的windows内核管理,异常和trap不在此文的讨论之列. 1. windows中断总貌 在windows中,物理上的中断源被抽象为KINTERRUPT结构.一个中断源在windows中 ...

  8. Win10 + CLion + 树莓派 + QT 远程开发调用Python

    原则:能在一个机器上开发的就不在两台机器上!! 首先需要配置远程QT开发环境 配置Cmake cmake_minimum_required(VERSION 3.14) project(qt_test) ...

  9. @Value和@PropertySource实现*.properties配置文件读取过程和实现原理

    @Value和@PropertySource实现*.properties 配置文件读取过程和实现原理 1       配置使用步骤 (1)右击resource目录添加*.prooerties配置文件

  10. AngularJs 拦截器,拦截请求

    问题前述 我在项目中遇到这样一个问题: 在Angular项目中,会有很多需要用户点击操作的地方,如果用户点击过快会产生多次相同请求,会吃服务器带宽,如果这是其他涉及钱有关的操作,这会产生一个致命的问题 ...