MapReduce实现自定义分区的方法
admin
2023-01-23 11:01:28
0

简介

mapreduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算。

它通过把对数据集的大规模操作分发给网络上的每个节点实现可靠性,极大地方便了编程人员在不会分布式并行编程的情况下,将自己的程序运行在分布式系统上。

MapReduce自带的分区器是HashPartitioner
原理

先对map输出的key求hash值,再模上reduce task个数,根据结果,决定此输出kv对,被匹配的reduce任务取走。
MapReduce实现自定义分区的方法
自定义分分区需要继承Partitioner,复写getpariton()方法
自定义分区类:
MapReduce实现自定义分区的方法
注意:map的输出是键值对
其中int partitionIndex = dict.get(text.toString())partitionIndex是获取K的值

附:被计算的的文本

Dear Dear Bear Bear River Car Dear Dear  Bear Rive
Dear Dear Bear Bear River Car Dear Dear  Bear Rive

需要在main函数中设置,指定自定义分区类
MapReduce实现自定义分区的方法
自定义分区类:

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
import java.util.HashMap;
public class CustomPartitioner extends Partitioner {
    public static HashMap dict = new HashMap();
    //Text代表着map阶段输出的key,IntWritable代表着输出的值
    static{
        dict.put("Dear", 0);
        dict.put("Bear", 1);
        dict.put("River", 2);
        dict.put("Car", 3);
    }
    public int getPartition(Text text, IntWritable intWritable, int i) {
        //
        int partitionIndex = dict.get(text.toString());
        return partitionIndex;
    }
}

注意:map的输出结果是键值对,int partitionIndex = dict.get(text.toString());中的partitionIndex是map输出键值对中的键的值,也就是K的值。
Maper类:

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class WordCountMap extends Mapper {
    public void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        String[] words = value.toString().split("\t");
        for (String word : words) {
            // 每个单词出现1次,作为中间结果输出
            context.write(new Text(word), new IntWritable(1));
        }
    }
}

Reducer类:

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class WordCountMap extends Mapper {
    public void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        String[] words = value.toString().split("\t");
        for (String word : words) {
            // 每个单词出现1次,作为中间结果输出
            context.write(new Text(word), new IntWritable(1));
        }
    }
}

main函数:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
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.IOException;
public class WordCountMain {
    public static void main(String[] args) throws IOException,
            ClassNotFoundException, InterruptedException {
        if (args.length != 2 || args == null) {
            System.out.println("please input Path!");
            System.exit(0);
        }
        Configuration configuration = new Configuration();
        configuration.set("mapreduce.job.jar","/home/bruce/project/kkbhdp01/target/com.kaikeba.hadoop-1.0-SNAPSHOT.jar");
        Job job = Job.getInstance(configuration, WordCountMain.class.getSimpleName());
        // 打jar包
        job.setJarByClass(WordCountMain.class);
        // 通过job设置输入/输出格式
        //job.setInputFormatClass(TextInputFormat.class);
        //job.setOutputFormatClass(TextOutputFormat.class);
        // 设置输入/输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // 设置处理Map/Reduce阶段的类
        job.setMapperClass(WordCountMap.class);
        //map combine
        //job.setCombinerClass(WordCountReduce.class);
        job.setReducerClass(WordCountReduce.class);
        //如果map、reduce的输出的kv对类型一致,直接设置reduce的输出的kv对就行;如果不一样,需要分别设置map, reduce的输出的kv类型
        //job.setMapOutputKeyClass(.class)
        // 设置最终输出key/value的类型m
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setPartitionerClass(CustomPartitioner.class);
        job.setNumReduceTasks(4);
        // 提交作业
        job.waitForCompletion(true);

    }
}

main函数参数设置:
MapReduce实现自定义分区的方法

相关内容

热门资讯

终于明白“瓜瓜丰城棋牌.真的有... 网上科普关于“瓜瓜丰城棋牌有没有挂”话题很是火热,小编也是针对瓜瓜丰城棋牌作*弊开挂的方法以及开挂对...
终于懂了“九酷牛牛.到底是不是... 网上科普关于“九酷牛牛有没有挂”话题很是火热,小编也是针对九酷牛牛作*弊开挂的方法以及开挂对应的知识...
重磅消息“花城牌舍.真的有挂吗... 重磅消息“花城牌舍.真的有挂吗?”原来真的有挂您好,花城牌舍这个游戏其实有挂的,确实是有挂的,需要了...
最新引进“兴动茶馆.怎么开挂?... 您好:兴动茶馆这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏...
今日重大通报“边锋老友麻将.真... 网上科普关于“边锋老友麻将有没有挂”话题很是火热,小编也是针对边锋老友麻将作*弊开挂的方法以及开挂对...
玩家攻略科普“十三十三水全民比... 玩家攻略科普“十三十三水全民比鸡.辅助器?”太坑了果然有挂您好,十三十三水全民比鸡这个游戏其实有挂的...
终于了解“中至江西麻将.真的有... 您好:中至江西麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款...
终于明白“云圈丰城麻将.辅助开... 家人们!今天小编来为大家解答云圈丰城麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...
终于懂了“新皇豪炸金花.有没有... 网上科普关于“新皇豪炸金花有没有挂”话题很是火热,小编也是针对新皇豪炸金花作*弊开挂的方法以及开挂对...
【第一资讯】“西南互娱.可以开... 网上科普关于“西南互娱有没有挂”话题很是火热,小编也是针对西南互娱作*弊开挂的方法以及开挂对应的知识...