一个mapreduce同时加载读取多个文件的代码部分

方法一:

 

  a.第一步:在job中加载两个文件所在的位置

 

         FileInputFormat.setInputPaths(job, new Path[] { new Path("hdfs://192.168.9.13:8020/gradeMarking"),
      new Path("hdfs://192.168.9.13:8020/implyCount") });

 

  b.第二步:在Mapper类中重写setup方法,使用context对象获取该文件所在的文件名(如果是经过处理后的数据文件,因为文件名一样part-r-00000,所以要获取其所在的文件夹名)
        @Override
    protected void setup(Mapper<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
    FileSplit fs = (FileSplit) context.getInputSplit();
    parentName = fs.getPath().getParent().getName();
    }

方法二:

  a.第一步:在job中将文件加载到本地

    job.addCacheFile(new URI("hdfs://192.168.9.13:8020/meanwhileFind(同现)_data/part-r-00000"));
  b.第二步:在Mapper函数中重写setup函数,用字符缓冲流进行读取
   

 1 @Override
 2         protected void setup(Mapper<LongWritable, Text, Text, Sort>.Context context)
 3                 throws IOException, InterruptedException {
 4             @SuppressWarnings("resource")
 5             BufferedReader br = new BufferedReader(new FileReader("part-r-00000"));
 6             String str = null;
 7             while ((str = br.readLine()) != null) {
 8                 String[] datas = str.split("\t");
 9                 String[] sp = datas[0].split("-");
10                 if (!map.containsKey(sp[0])) {
11                     HashMap<String, Double> mapInner = new HashMap<>();
12                     mapInner.put(sp[1], Double.parseDouble(datas[1]));
13                     map.put(sp[0], mapInner);
14                 } else {
15                     @SuppressWarnings("rawtypes")
16                     HashMap mapInner = map.get(sp[0]);
17                     mapInner.put(sp[1], Double.parseDouble(datas[1]));
18                 }
19             }
20         }

 

转载于:https://www.cnblogs.com/le-ping/p/7788935.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值