mapreduce多文件的处理手法

复制代码
package org.three;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

public class MyThreeDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conn = new Configuration();

        Job job = Job.getInstance();

        job.setJarByClass(MyThreeDriver.class);
        job.setMapperClass(MyThreeMapper.class);
        job.setReducerClass(MyThreeReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileInputFormat.addInputPath(job,new Path(args[1]));
        FileOutputFormat.setOutputPath(job,new Path(args[2]));

        System.exit(job.waitForCompletion(true)?0:1);



    }


    private static class MyThreeMapper extends Mapper<LongWritable,Text,Text,IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            if (key.get()==0){
                return;
            }
            String[] strs = value.toString().split(",");
            FileSplit fs = (FileSplit) context.getInputSplit();
            String names = fs.getPath().getName();
            if(names.equals("t1.csv")){
                context.write(new Text(strs[1]),new IntWritable(1));
            }else {
                context.write(new Text(strs[1]),new IntWritable(1));
            }
        }
    }

    private static class MyThreeReduce extends Reducer<Text,IntWritable,Text,IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum=0;
            for (IntWritable value : values) {
                sum++;
            }
            context.write(key,new IntWritable(sum));
        }
    }
}

1、多输入文本。多个fileinputform,路径参数可用多个args0,args1...

2、运行时数据文件存放在hdfs中。

3、多文件数据的获取并判断。

String\[\] strs = value.toString().split(",");

FileSplit fs = (FileSplit) context.getInputSplit();

String names = fs.getPath().getName();

if(names.equals("t1.csv"))

{ context.write(new Text(strs1),new IntWritable(1)); }

else

{ context.write(new Text(strs1),new IntWritable(1)); }

相关推荐
姜穆澜5 小时前
OneID 从 0 到 1 完整生产案例(二)
大数据
思录Echo5 小时前
光学轮廓仪质检设备厂家怎么选?优可测国产替代方案深度解析
大数据·人工智能
半摆烂日常6 小时前
自建WMS和买成品:三年成本对比
大数据·服务器·数据库·python·深度学习·低代码·numpy
runshui276 小时前
openssl 3.5安装
大数据
临沂GEO6 小时前
芝麻开门GEO|AI数字化新趋势,助力企业线上长效增长
大数据·人工智能·python
invicinble7 小时前
做数字产品的核心内容--数据的设计与展示
大数据·前端
财迅通Ai7 小时前
光智科技的护城河:稀散金属的资源禀赋与产业纵深
大数据·科技·光智科技
hrrrrxeeeee7 小时前
告别功能验收!AI 项目要用效果指标说话
大数据·人工智能
dh2711987798 小时前
AI幻觉与信任赤字:南京GEO服务商如何帮企业重建AI时代的品牌信用
大数据·人工智能
玖石书8 小时前
Git Submodule 完全指南:从添加到日常维护的常规操作全流程
大数据·git·elasticsearch