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

相关推荐
古法老登瞎折腾8 分钟前
大数据智能运维实战(一):机器和任务数据都有了,如何用 MCP 搭建运维助手?
大数据·运维·spring boot·spring ai·mcp
墨子夕38 分钟前
创新的潮汐——从科学发现到资本周期的百年规律(第二卷)
大数据·数据库·人工智能
盟接之桥1 小时前
盟接之桥谈线束数字化-按单入按单出:线束企业库存管理的破局之道
大数据·前端·网络·数据库·人工智能·制造
大大大大晴天2 小时前
从 Kafka 到 Flink,如何让实时数仓既快、又准、还可追
大数据
顧棟2 小时前
Elasticsearch Scroll :分页进度到底记在哪?
大数据·elasticsearch·搜索引擎
用户3610588626122 小时前
Flink SQL 之 Flink SQL 企业级案例分析:从实时大屏到用户画像与风控告警的生产实践
大数据·flink
probably1213 小时前
基于水利部技术标准的山洪灾害科学防治体系建设探析
大数据·人工智能
用户3610588626123 小时前
Flink SQL 之 Table Window 原理及代码实现:从滚动窗口到窗口 TopN 与窗口 Join 实战
大数据·flink
ha_lydms3 小时前
St Functions(空间函数)
大数据·hadoop·阿里云·spark·数据处理·dataworks·maxcompute
W***25923 小时前
2026企业AI办公工具选型指南:适合团队使用的AI办公产品有哪些
大数据·人工智能