mapreduce | 自定义Partition分区(案例2)

1.需求

统计每个手机号消费总金额,按照消费金额降序排序,最终联通、电信、移动分别写入不同的文件。

130、131、132(联通) 133(电信) 135、136、137、138、139 (移动)
手机号,消费记录

13512345678,50

13512345678,90

13122345678,10

13122345678,110

13212345678,10

13212345678,90

13912345378,10

13912345378,90

13612345678,50

13612345678,55

13312345378,65

13312345378,90

2.将数据上传到hdfs

3.Idea代码

MyPartition

java 复制代码
package demo8;

import demo5.DescIntWritable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

public class MyPartition extends Partitioner<DescIntWritable, Text> {

    @Override
    public int getPartition(DescIntWritable descIntWritable, Text text, int numPartitions) {
        String textStr = text.toString();
        boolean arr1 = textStr.startsWith("130") || textStr.startsWith("131") || textStr.startsWith("132");
        boolean arr2=textStr.startsWith("133");
        if(arr1){
            return 0;
        }else if(arr2){
            return 1;
        }
        return 2;
    }
}

PhoneBillJob

java 复制代码
package demo8;

import demo5.DescIntWritable;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class PhoneBillJob {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");

        Job job = Job.getInstance(conf);
        job.setJarByClass(PhoneBillJob.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        TextInputFormat.addInputPath(job,new Path("/phtest/phone.txt"));
        TextOutputFormat.setOutputPath(job,new Path("/phtest/out"));

        job.setMapperClass(PhoneBillMapper.class);
        job.setReducerClass(PhoneBillReducer.class);
        //map输出的键与值类型
        job.setMapOutputKeyClass(DescIntWritable.class);
        job.setMapOutputValueClass(Text.class);
        //reducer输出的键与值类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DescIntWritable.class);

        //设置reduceTask的个数
        job.setNumReduceTasks(3);
        //设置自定义分区
        job.setPartitionerClass(MyPartition.class);

        boolean b = job.waitForCompletion(true);
        System.out.println(b);


    }

    static class PhoneBillMapper extends Mapper<LongWritable, Text,DescIntWritable,Text> {

        @Override
        protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {
            String[] arr = value.toString().split(",");
            context.write(new DescIntWritable(Integer.parseInt(arr[1])),new Text(arr[0]));
        }
    }
    static class PhoneBillReducer extends Reducer<DescIntWritable,Text,Text,DescIntWritable> {
        @Override
        protected void reduce(DescIntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text value : values) {
                context.write(value, key);
            }
        }
    }}

4.在hdfs上查看结果


就一直往前走吧,别回头~

相关推荐
掘金-我是哪吒1 小时前
分布式微服务系统架构第132集:Python大模型,fastapi项目-Jeskson文档-微服务分布式系统架构
分布式·python·微服务·架构·系统架构
源码技术栈1 小时前
SaaS基于云计算、大数据的Java云HIS平台信息化系统源码
java·大数据·云计算·云his·his系统·云医院·区域his
Elastic 中国社区官方博客1 小时前
Elasticsearch 索引副本数
大数据·数据库·elasticsearch·搜索引擎·全文检索
程序员学习随笔1 小时前
分布式 ID 生成的五种方法:优缺点与适用场景
分布式
Eternity......1 小时前
SparkSQL基本操作
大数据·spark
268572592 小时前
Elasticsearch 初步认识
大数据·elasticsearch·搜索引擎·全文检索·es
hello1114-2 小时前
Redis学习打卡-Day3-分布式ID生成策略、分布式锁
redis·分布式·学习
python算法(魔法师版)2 小时前
网络编程入门(一)
大数据·网络·网络协议·计算机网络
掘金-我是哪吒3 小时前
分布式微服务系统架构第129集:redis安装部署文档
redis·分布式·微服务·架构·系统架构
caihuayuan53 小时前
生产模式下react项目报错minified react error #130的问题
java·大数据·spring boot·后端·课程设计