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,路径参数可用多个args[0],args[1]...

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(strs[1]),new IntWritable(1)); }

else

{ context.write(new Text(strs[1]),new IntWritable(1)); }

相关推荐
运维行者_7 分钟前
OpManager MSP NetFlow Analyzer集成解决方案,应对多客户端网络流量监控挑战
大数据·运维·服务器·网络·数据库·自动化·运维开发
极创信息5 小时前
信创系统认证服务怎么做?从适配到验收全流程指南
java·大数据·运维·tomcat·健康医疗
大大大大晴天️6 小时前
Flink技术实践-Flink SQL 开发中的隐蔽陷阱
大数据·sql·flink
Gofarlic_OMS7 小时前
Windchill的license合规使用报告自动化生成与审计追踪系统
大数据·运维·人工智能·云原生·自动化·云计算
xcbrand7 小时前
文旅行业品牌策划公司找哪家
大数据·运维·人工智能·python
zxsz_com_cn8 小时前
设备预测性维护故障预警规则与原理解析
大数据·人工智能
hughnz9 小时前
AI和自动化让油田钻工慢慢消失
大数据·人工智能
juniperhan9 小时前
Flink 系列第8篇:Flink Checkpoint 全解析(原理+流程+配置+优化)
大数据·分布式·flink
GIS数据转换器9 小时前
延凡低成本低空无人机AI巡检方案
大数据·人工智能·信息可视化·数据挖掘·无人机
七夜zippoe10 小时前
OpenClaw 子代理(Subagent)机制详解
大数据·人工智能·subagent·openclaw·子代理