hadoop多路径输入方法

先给一个最简单的wordCount的例子:

package cn.hx.test;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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.output.FileOutputFormat;

public class WordCountApp {
    //自定义的mapper,继承org.apache.hadoop.mapreduce.Mapper
    public static class MyMapper extends org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, LongWritable>{
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
                throws IOException, InterruptedException {
            String line = value.toString();//split  函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回,这里按照"\t"来分割text文件中字符,即一个制表符,这就是为什么我在文本中用了空格分割,导致最后的结果有很大的出入。
            String[] splited = line.split("\t");        //foreach 就是 for(元素类型t 元素变量x:遍历对象obj){引用x的java语句}
            for (String word : splited) {          
                context.write(new Text(word), new LongWritable(1));
            }
        }
    }
    
    public static class MyReducer extends org.apache.hadoop.mapreduce.Reducer<Text, LongWritable, Text, LongWritable>{
        @Override
        protected void reduce(Text k2, Iterable<LongWritable> v2s,
                Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        
            long count = 0L;
            for (LongWritable v2 : v2s) {
                count += v2.get();
            }
            LongWritable v3 = new LongWritable(count);
            context.write(k2, v3);
        }
    }
    
    //客户端代码,写完交给ResourceManager框架去执行
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, WordCountApp.class.getSimpleName());
        //打成jar执行
        job.setJarByClass(WordCountApp.class);
        
        //数据在哪里?
        FileInputFormat.setInputPaths(job, args[0]);
        //使用哪个mapper处理输入的数据?
        job.setMapperClass(MyMapper.class);
        //map输出的数据类型是什么?
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        
        //使用哪个reducer处理输入的数据?
        job.setReducerClass(MyReducer.class);
        //reduce输出的数据类型是什么?
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //数据输出到哪里?
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        
        //交给yarn去执行,直到执行结束才退出本程序
        job.waitForCompletion(true);
    }
}

可以看到,java代码中分别通过FileInputFormat和FileOutputFormat来控制输入的路径,对于多路径输入有如下方法:

1.FileInputFormat.addInputPath(job, new Path(args[0]));的方法可以添加各种路径,可以通过不断调用FileInputFormat.addInputPath函数添加各种路径;

2.多路径用逗号分隔:String Paths= strings[0]+','+strings[1],FileInputFormat.addInputPaths(job,Paths);

3.FileInputFormat.setInputPaths(job,Paths);与addInputPaths,但这里直接替换而不是添加。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值