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

相关推荐
Coder_Boy_8 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_944934738 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
九河云9 小时前
5秒开服,你的应用部署还卡在“加载中”吗?
大数据·人工智能·安全·机器学习·华为云
Gain_chance10 小时前
36-学习笔记尚硅谷数仓搭建-DWS层数据装载脚本
大数据·数据仓库·笔记·学习
每日新鲜事10 小时前
热销复盘:招商林屿缦岛203套售罄背后的客户逻辑分析
大数据·人工智能
AI架构全栈开发实战笔记11 小时前
Eureka 在大数据环境中的性能优化技巧
大数据·ai·eureka·性能优化
AI架构全栈开发实战笔记11 小时前
Eureka 对大数据领域服务依赖关系的梳理
大数据·ai·云原生·eureka
自挂东南枝�12 小时前
政企舆情大数据服务平台的“全域洞察中枢”
大数据
weisian15112 小时前
Elasticsearch-1--什么是ES?
大数据·elasticsearch·搜索引擎
LaughingZhu12 小时前
Product Hunt 每日热榜 | 2026-02-08
大数据·人工智能·经验分享·搜索引擎·产品运营