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)); }

相关推荐
大大大大晴天3 天前
Hudi Metadata Table 与 Hive Sync (HMS)怎么选?
大数据
手可摘星辰7773 天前
一次线上FlinkCDC异常排查复盘
大数据·flink
大大大大晴天3 天前
Hudi技术内幕:Metadata Table原理与实践
大数据
大大大大晴天4 天前
Hudi技术内幕:深入解析Index索引机制
大数据
阿里云大数据AI技术4 天前
Flink Forward Asia 2026 深圳启幕:Agentic Streaming for AI,开启实时智能新范式
大数据·flink
SelectDB5 天前
阶跃星辰基于 SelectDB 构建 PB 级 Agent 可观测平台
大数据·数据库·aigc
大大大大晴天8 天前
Hudi技术内幕:RecordPayload到RecordMerger
大数据
SelectDB9 天前
秒级弹性、最高降本 70%:SelectDB Serverless 如何重塑云数仓资源效率
大数据·后端·云原生
WhoAmI9 天前
MapReduce框架原理解析一:InputFormat
大数据·hadoop
WhoAmI9 天前
MapReduce框架原理解析三:OutputFormat
大数据·hadoop