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上查看结果


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

相关推荐
分布式存储与RustFS12 分钟前
Helm在Kubernetes上部署RustFS生产环境指南
分布式·零基础·picgo·对象存储·minio·rustfs
rchmin14 分钟前
Nacos配置中心避坑指南:灵活配置 server-addr 的坑
分布式·nacos·动态配置
永霖光电_UVLED33 分钟前
1.6T 光模块的能效革命
大数据·人工智能·汽车·娱乐
talen_hx29640 分钟前
《零基础入门Spark》学习笔记 Day 17
大数据·笔记·学习·spark
hf2000121 小时前
深入分析:Iceberg v3「删除向量(Deletion Vectors, DV)」如何缓解 CDC 场景写放大
大数据·spark·数据湖·湖仓一体·lakehouse
Elastic 中国社区官方博客2 小时前
使用 Remote Write 将 Prometheus 指标发送到 Elasticsearch
大数据·运维·elasticsearch·搜索引擎·全文检索·prometheus
nashane2 小时前
HarmonyOS 6.0 分布式相机实战:调用远端设备摄像头与AI场景识别(API 11+)
分布式·数码相机·harmonyos·harmonyos 5
小t说说2 小时前
2026年PPT生成工具评测及使用体验
大数据·前端·人工智能
IT观测2 小时前
数字化转型浪潮下的西安样本:从“摩高互动”看企业级技术服务的破局之道
大数据·人工智能
想你依然心痛2 小时前
大数据时代时序数据库选型指南:Apache IoTDB 的实战进阶与避坑法则
大数据·apache·时序数据库