Hadoop编程学习3--数据排序

package org.apache.hadoop.examples;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Sort 
{
    public static class Map extends Mapper<Object,Text,IntWritable,IntWritable>
    {
        private IntWritable num=new IntWritable(1); //序号
        private IntWritable val=new IntWritable();  //数值

        public void map(Object key,Text value,Mapper<Object,Text,IntWritable,IntWritable>.Context context) throws IOException, InterruptedException
        {
            val.set(Integer.parseInt(value.toString()));

            //将一行行读取到的数值写入key中,num是序号1
            context.write(val, num);
        }
    }

    public static class Reduce extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>
    {
        private IntWritable num=new IntWritable(1);

        public void reduce(IntWritable key,Iterable<IntWritable> value,Reducer<IntWritable,IntWritable,IntWritable,IntWritable>.Context context) throws IOException, InterruptedException
        {
            //Map传入Reduce节点中间又一个Shuffle过程,会自动对数字升序排序
            for(IntWritable val:value)
            {
                //将序号 数字 写入output目录中
                context.write(num,key);

                //每写入一个数字,序号num+1
                num.set(num.get()+1);
            }

        }
    }

    public static void main(String[] args) throws Exception
    {

        String[] otherArgs = new String[]{"input2","output2"};

        final String OUTPUT_PATH = otherArgs[1];     
        Configuration conf = new Configuration();

        Path path = new Path(OUTPUT_PATH);  

        //加载配置文件
        FileSystem fileSystem = path.getFileSystem(conf);

        //输出目录若存在则删除
        if (fileSystem.exists(new Path(OUTPUT_PATH))) 
        {  
           fileSystem.delete(new Path(OUTPUT_PATH),true);  
        }  

        //指定输入输出目录

        if (otherArgs.length != 2) 
        {
            System.err.println("路径出错");
            System.exit(2);
        }

        //一些初始化
        Job job = Job.getInstance();
        job.setJarByClass(Sort.class);
        job.setMapperClass(Map.class);  //初始化为自定义Map类
        job.setReducerClass(Reduce.class);  //初始化为自定义Reduce类
        job.setOutputKeyClass(IntWritable.class);  //指定输出的key的类型
        job.setOutputValueClass(IntWritable.class);  //指定输出的Value的类型

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  //FileInputFormat指将输入的文件(若大于64M)进行切片划分,每个split切片对应一个Mapper任务
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
     }

} 

代码有很清晰的注释,看不懂的话可以评论给我,input目录文件及运行结果output目录如下:

DFS文件目录:

DFS文件目录

/input/1.txt 2.txt 3. txt 4.txt

其中某一个文件

每个文件中都有一些测试数字,不一一截图了。

/output2/part-r-00000

/output2/part-r-00000

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值