使用MapReduce在集群上进行分区操作

在mapreduce道路上的同志,或许以下代码能带来启发
1、需要将数据上传到hdfs(集群上面)
2、将idea中的代码打包上传到集群
3、在集群上执行hadoop jar 名字.jar DemoParter.PartitionMapper /in /out

代码如下:

package DemoParter;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

//继承Configured类和接口Tool
public class PartitionMain extends Configured implements Tool{

    public int run(String[] args) throws Exception {

        //创建一个JOB任务,并且调用Configuration类,给任务起一个名字
        Job job = Job.getInstance(super.getConf(), "job");
     //如果想要传到集群上这是必备的
        job.setJarByClass(PartitionMain.class);
        //需要读取文件类读取文件的类setInputFormatClass
        job.setInputFormatClass(TextInputFormat.class);
        //TextInputFormat调用集群的传输路径
        TextInputFormat.addInputPath(job,new Path(args[0]));

        //确定mapper类
        job.setMapperClass(PartitionMapper.class);
        //确定mapper类的Key2的数据类型
        job.setMapOutputKeyClass(Text.class);
        //确定mapper类的values的数据类型
        job.setMapOutputValueClass(NullWritable.class);


        //设置分区setPartitionerClass
        //创建一个分区类PartitionOwn
        job.setPartitionerClass(PartitionOwn.class);

        job.setReducerClass(PartitionReducer.class);
        //确定Reducer类的Key3的数据类型
        job.setOutputKeyClass(Text.class);
        //确定Reducer类的v3的数据类型
        job.setMapOutputValueClass(NullWritable.class);
        //设置reduce的数量
        job.setNumReduceTasks(2);
        //需要创建一个文件的输出路径
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean b = job.waitForCompletion(true);

        return b?0:1;
    }

    public static void main(String[] args) throws Exception {
        //调用run()方法,
        int run = ToolRunner.run(new Configuration(), new PartitionMain(), args);
        //退出run()方法
        System.exit(run);
    }
}

package DemoParter;

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;

//创建map方法 设置map 中key1 v1  ke2 v2的参数
public class PartitionMapper extends Mapper<LongWritable,Text,Text,NullWritable> {

    //重写map方法
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
       //因为后续需要进行分区,所以此时不需要进行splits分割只要context输出即可
        context.write(value,NullWritable.get());
    }
}
package DemoParter;

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

import java.io.IOException;


public class PartitionReducer 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());
    }
}

package DemoParter;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
//继承 Partitioner类此时对v2,key2进行处理
public class PartitionOwn extends Partitioner<Text,NullWritable> {
    @Override
    public int getPartition(Text text, NullWritable nullWritable, int i) {
        //进行Split分割
        String[] split = text.toString().split("\t");
        //分割以后选出第列进行分区的条件
        String gameResult = split[5];
        if(gameResult != null && gameResult != ""){

                if(Integer.parseInt(gameResult) > 13){
                    return 0;
                }else
                {
                    return 1;
                }
        }


        return 0;
    }
}

相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页