package org.lukey.hadoop.muloutput;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.util.GenericOptionsParser; public class TestMultipleOutput { static String baseOutputPath = "/user/hadoop/test_out"; private static MultipleOutputs<Text, IntWritable> mos; // Mapper
static class WordsOfClassCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1);
private Text className = new Text(); @Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
FileSplit fileSplit = (FileSplit) context.getInputSplit(); // 文件名
String fileName = fileSplit.getPath().getName(); // 文件夹名
String dirName = fileSplit.getPath().getParent().getName(); className.set(dirName + "/" + fileName); // Country:ABDBI 1
mos.write(value, one, className.toString());
// context.write(className, one); } @Override
protected void cleanup(Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
mos.close();
} @Override
protected void setup(Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
mos = new MultipleOutputs<Text, IntWritable>(context);
} } // Reducer
static class WordsOfClassCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { // result 表示每个文件里面单词个数
IntWritable result = new IntWritable(); @Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
result.set(sum); context.write(key, result);
} } // Client
public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) {
System.out.println("Usage <in> <out>");
System.exit(-1);
} Job job = new Job(conf, "file count"); job.setJarByClass(TestMultipleOutput.class); job.setMapperClass(WordsOfClassCountMapper.class);
job.setReducerClass(WordsOfClassCountReducer.class); FileSystem fileSystem = FileSystem.get(conf); Path path = new Path(otherArgs[0]); FileStatus[] fileStatus = fileSystem.listStatus(path); for (FileStatus fs : fileStatus) {
if (fs.isDir()) {
Path p = new Path(fs.getPath().toString());
FileInputFormat.addInputPath(job, p);
}else{
FileInputFormat.addInputPath(job, fs.getPath());
}
} FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1); }
}

multipleOutputs Hadoop的更多相关文章

  1. 使用hadoop multipleOutputs对输出结果进行不一样的组织

    MapReduce job中,可以使用FileInputFormat和FileOutputFormat来对输入路径和输出路径来进行设置.在输出目录中,框架自己会自动对输出文件进行命名和组织,如:par ...

  2. hadoop多文件输出MultipleOutputFormat和MultipleOutputs

    1.MultipleOutputFormat可以将相似的记录输出到相同的数据集.在写每条记录之前,MultipleOutputFormat将调用generateFileNameForKeyValue方 ...

  3. Hadoop MultipleOutputs 结果输出到多个文件夹 出现数据不全,部分文件为空

    如题:出现下图中的情况(设置reduceNum=5) 感觉很奇怪,排除了很久,终于发现是一个第二次犯的错误:丢了这句 this.mOutputs.close(); 加上这句,一切恢复正常!

  4. hadoop multipleoutputs

    http://grepalex.com/2013/05/20/multipleoutputs-part1/ http://grepalex.com/2013/07/16/multipleoutputs ...

  5. [Hadoop in Action] 第7章 细则手册

    向任务传递定制参数 获取任务待定的信息 生成多个输出 与关系数据库交互 让输出做全局排序   1.向任务传递作业定制的参数        在编写Mapper和Reducer时,通常会想让一些地方可以配 ...

  6. hadoop MapReduce 笔记

    1.        MapReduce程序开发步骤 编写map 和 reduce 程序–> 单元测试 -> 编写驱动程序进行验证-> 本地数据集调试 ->  部署到集群运行 用 ...

  7. hadoop拾遗(五)---- mapreduce 输出到多个文件 / 文件夹

    今天要把HBase中的部分数据转移到HDFS上,想根据时间戳来自动输出到以时间戳来命名的每个文件夹下.虽然以前也做过相似工作,但有些细节还是忘记了,所以这次写个随笔记录一下. package com. ...

  8. [BigData]关于Hadoop学习笔记第三天(PPT总结)(一)

     课程安排 MapReduce原理*** MapReduce执行过程** 数据类型与格式*** Writable接口与序列化机制*** ---------------------------加深拓展- ...

  9. 通过MultipleOutputs写到多个文件

    MultipleOutputs 类可以将数据写到多个文件,这些文件的名称源于输出的键和值或者任意字符串.这允许每个 reducer(或者只有 map 作业的 mapper)创建多个文件. 采用name ...

随机推荐

  1. php curl调用相关api

    一.基本步骤 1.本次模拟是php的相关post请求,可通过CURLOPT_CUSTOMREQUEST设定相关POST.GET.PUT.DELETE相关适应REST API 2.相关重要的是curl_ ...

  2. MT4 做指标模版

    //+------------------------------------------------------------------+ //| guo.mq4 | //| Copyright 2 ...

  3. viewpager处理(一):让viewpager不能滑动

    1.实现原理: 自定义viewpager,重写onTouchEvent方法,什么触摸事件都不响应即可让viewpager不能滑动. 2.代码如下 public class NoScrollViewPa ...

  4. 安卓获取线程id

    错误的做法: @Override public void onCreate() { mContext = getApplicationContext(); mHandler = new Handler ...

  5. 删除sql计划 调用的目标发生了异常。 (mscorlib) 其他信息: 用户 'sa' 登录失败。

    在删除以前创建的sql的计划任务时,弹出如题错误提示,发现错误原因在于,sa密码更改过,导致在删除时因为sa的密码和当前的密码不正确出现此错误. 解决办法: 1.在计划任务的编辑窗口,找到管理连接 2 ...

  6. Android PopupWindow菜单

    初学Android,引用了这篇文章的代码 http://www.cnblogs.com/jiezzy/archive/2012/08/15/2640584.html 使用PopupWindow制作自定 ...

  7. 直接用request.setAttribute()会报错,在这之前应该先让request获取ServletActionContext.getRequest();方法 // request.getAttribute同理

    正确流程应该是 import javax.servlet.http.HttpServletRequest; HttpServletRequst request = ServletActionConte ...

  8. sql server统计字段的值在某些范围内中的个数

    有一张表test如下: create table test ( id ,) primary key, num int ) 插入数据: ); ); ); ); ); ); ); ); ); ); ); ...

  9. 35W行数据的文本文件,每500行添加4个换行符。代码实现思路

  10. awstats + tomcat + windows

    下载: 1.apache-tomcat-7.0.67 2.ActivePerl-5.22.1.2201-MSWin32-x86-64int-299574.msi 3.awstats-7.4.zip 修 ...