java hadoop(四) hadoop mapreduce 分区案例

8 篇文章 0 订阅
5 篇文章 0 订阅

我们在编码步骤那章简单介绍了一下分区。

其实通俗来说,分区就是根绝K的某个特征属性(可能是大小、长度以及尾号等等),将K的数据写到不同的文件里。

比如我们根据ID来分区,我们可以根据尾号来分10个文件。

有几个分区就有几个Reduce Task。

 

 

实现分区案例:

我们根据hdfs文件中的这个字段大小进行分区,大于15的一个区,小于15的一个区,最终有两个out文件。

目录结构:

Mapper:

package partitiondemo;

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;


/**
 * 传入四个参数,第一个参数是这一行的起始点在文件中的偏移量 第二个就是读取到的内容
 * 第三个 第四个 是我们输出的K2 V2类型
 * 思路:将文本内容作为K2。 V2直接为空
 */
public class MyMapper extends Mapper<LongWritable, Text,Text, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //value 直接写入空值
        context.write(value, NullWritable.get());
    }
}

Partitioner:

package partitiondemo;

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

/**
 * 这里的两个参数就是map的K2 V2
 */
public class MyPartitioner  extends Partitioner<Text, NullWritable> {

    @Override
    public int getPartition(Text text, NullWritable nullWritable, int i) {
        //取出根据数值分区的那个值
        String num = text.toString().split("\t")[5];

        //分区
        if(Integer.parseInt(num)>15){
            return 1;
        }else {
            return 0;
        }
    }
}

Reducer:

package partitiondemo;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 没有处理
 */
public class MyReducer 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());
    }
}

Main:

package partitiondemo;

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

public class ReducerMain {

    public static void main(String[] args) throws Exception{
        //创建job
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration, "my-partition");
        //指定job所在jar包
        job.setJarByClass(ReducerMain.class);
        //指定文件读取方式 路径
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, new Path("hdfs://192.168.40.150:9000/test.txt"));
        //指定mapper
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        //指定自定义分区类
        job.setPartitionerClass(MyPartitioner.class);

        //指定reducer
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //设置分区个数 我们是两个reducer
        job.setNumReduceTasks(2);
        //指定输出方式类以及输出路径 目录必须不存在
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job, new Path("hdfs://192.168.40.150:9000/lgy_test/res"));
        //将job提交到yarn集群
        boolean bl = job.waitForCompletion(true);
        System.exit(bl?0:1);

    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值