import org.apache.hadoop.io.Text;

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.ContextFactory;
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;
import org.apache.hadoop.mapreduce.lib.output.MapFileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Progressable;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class MultiFileOut extends Configured implements Tool {
    private final static String[] Shengarry = { "北京", "天津", "山西", "内蒙古", "辽宁",
            "吉林", "黑龙江", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北",
            "湖南", "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "西藏", "陕西省", "甘肃",
            "青海", "宁夏", "新疆", "河北" };
    private final static String[] sexary = { "M", "F" };

public static String getPinYin(String src) {
        char[] srcary = null;
        srcary = src.toCharArray();
        String[] strtmp = new String[srcary.length];

// 设置汉字拼音输出的格式
        HanyuPinyinOutputFormat formatstr = new HanyuPinyinOutputFormat();
        formatstr.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        formatstr.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        formatstr.setVCharType(HanyuPinyinVCharType.WITH_V);
        String resultstr = "";
        int t0 = srcary.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断能否为汉字字符
                if (Character.toString(srcary[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    strtmp = PinyinHelper.toHanyuPinyinStringArray(srcary[i],
                            formatstr);// 将汉字的几种全拼都存到t2数组中
                    resultstr += strtmp[0];// +" ";// 取出该汉字全拼的第一种读音并连接到字符串t4后
                } else {
                    // 如果不是汉字字符,间接取出字符并连接到字符串t4后
                    resultstr += Character.toString(srcary[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return resultstr;
    }

private static class ProvinceMapper extends
            Mapper<Object, Text, Text, Text> {
        @Override
        protected void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {
            String str = value.toString();
            String outkey = "";
            Boolean isfind = false;
            if (str.indexOf("name") >= 0)
                return;

String[] strArray = str.split(",");
            if (strArray.length != 33)
                return;

String sex = strArray[5];
            String addr = strArray[7];
            for (int i = 0; i < Shengarry.length; i++) {
                for (int j = 0; j < sexary.length; j++) {
                    int index = addr.indexOf(Shengarry[i]);
                    if ((index >= 0) && (index <= 3)
                            && (sex.indexOf(sexary[j]) >= 0)) {
                        isfind = true;
                        outkey = getPinYin(Shengarry[i]) + sexary[j];
                        break;
                    }
                }

if (isfind)
                    break;
            }

if (isfind) {
                context.write(new Text(outkey), value);
            } else {
                System.out.println("Error Data" + value.toString());
            }
        }
    }

private static class ProvinceReducer extends
            Reducer<Text, Text, NullWritable, Text> {
        private MultipleOutputs mos = null;

@Override
        protected void setup(Context context) throws IOException,
                InterruptedException {
            mos = new MultipleOutputs(context);
        }

@Override
        protected void cleanup(Context context) throws IOException,
                InterruptedException {
            mos.close();
        }

@Override
        protected void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            Text value = new Text("");
            String valuetmp = "";

for (Text va : values) {
                value.set(va.toString());
                
                try {
                    mos.write(key.toString(), NullWritable.get(), value);
                } catch (Exception e) {
                    //System.out.println("Exception" + key);
                }
            }
        }
    }

public static void main(String[] args) throws Exception {
        ToolRunner.run(new Configuration(), new MultiFileOut(), args);
    }

@Override
    public int run(String[] args) throws Exception {
        int result = 0;
        Configuration conf = new Configuration();
        String[] argArray = new GenericOptionsParser(conf, args)
                .getRemainingArgs();
        if (argArray.length != 2) {
            System.err.println("Usage: MultiFileOut <in> <out>");
            System.exit(1);
        }

Job job = new Job(conf, "MultiFileOut");
        job.setJarByClass(MultiFileOut.class);
        job.setMapperClass(ProvinceMapper.class);
        job.setReducerClass(ProvinceReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);
        // job.setOutputFormatClass(WordCountOutputFormat.class);
        FileInputFormat.addInputPath(job, new Path(argArray[0]));
        FileOutputFormat.setOutputPath(job, new Path(argArray[1]));
        
        for (int i = 0; i < Shengarry.length; i++) {
            for (int j = 0; j < sexary.length; j++) {
                MultipleOutputs.addNamedOutput(job, getPinYin(Shengarry[i])
                        + sexary[j], TextOutputFormat.class, Text.class,
                        Text.class);
            }
        }
        
        try {
            result = job.waitForCompletion(true) ? 0 : 1;
        } catch (ClassNotFoundException | InterruptedException e) {
            e.printStackTrace();
        }

return result;
    }
}

hadoop分类输出的更多相关文章

  1. mysql 如何在访问某张数据表按照某个字段分类输出

    也许大家有时候会遇到需要将把数据库中的某张表的数据按照该表的某个字段分类输出,比如一张数据表area如下 我们需要将里面的area按照serialize字段进行分类输出,比如这种形式: areas   ...

  2. 微软BI 之SSIS 系列 - 在 SSIS 中将指定目录下的所有文件分类输出到不同文件夹

    开篇介绍 比如有这样的一个需求,旧的一个业务系统通常将产出的文件输出到同一个指定的目录下的不同子目录,输出的文件类型有 XML,EXCEL, TXT 这些不同后缀的文件.现在需要在 SSIS 中将它们 ...

  3. log4j 分类输出

    一个log4j的问题也是折磨了我两天了. 终于算是实现了个符合需求的小demo.然而,我必须吧log4j搞定,这个乐塞. 需求描述: 用xml配置文件,将debug.info.warn.error分类 ...

  4. hadoop MapReduce —— 输出每个单词所对应的文件

    下面是四个文件及其内容. 代码实现: Mapper: package cn.tedu.invert; import java.io.IOException; import org.apache.had ...

  5. log4net通过代码控制按分类输出

    应用场景: 比如我们系统有5个任务,每个任务都是独立的流程,按照传统的方式这些流程的数据会输出到一起,这无疑给我们排查问题增加了难度,因为我们需要的是每一个任务一个独立的输出文件,比如任务A输出到lo ...

  6. logback怎么写?分类输出日志到不同的文件

    此appender有顺序,最好不要乱调顺序,输出日志如下: drwxr-xr-x 2 root root 4096 Dec 3 00:00 2019-12-02drwxr-xr-x 2 root ro ...

  7. Hadoop概念学习系列之为什么hadoop/spark执行作业时,输出路径必须要不存在?(三十九)

    很多人只会,但没深入体会和想为什么要这样? 拿Hadoop来说,当然,spark也一样的道理. 输出路径由Hadoop自己创建,实际的结果文件遵守part-nnnn的约定. 如何指定一个已有目录作为H ...

  8. hadoop 输出中文乱码问题

    本文转载至: http://www.aboutyun.com/thread-7358-1-1.html hadoop涉及输出文本的默认输出编码统一用没有BOM的UTF-8的形式,但是对于中文的输出wi ...

  9. PHP无限级分类的实现(不使用递归)

    无限级分类在开发中经常使用,例如:部门结构.文章分类.无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式: 查找分类A下面所有分类包含的文章. 1.实现原理 在 ...

随机推荐

  1. ORACLE这门武功

    今天不是一个什么特别的日子,只是有些感触,特别是工作上的,所以就想记一变复杂的心情.有句话说:"人到中年不如狗",我不知道我现在的状况是不如狗,还是比狗好一点.但至少心情部复杂的. ...

  2. Linpack之HPL测试

    平台信息 Description: CentOS Linux release 7.6.1810 (Core) 注意事项 安装HPL之前需要配置好: GCC/Fortran77 编译器 BLAS/CBL ...

  3. Spark机器学习库(MLlib)官方指南手册中文版

    中文https://blog.csdn.net/liulingyuan6/article/details/53582300 https://yq.aliyun.com/articles/608083 ...

  4. PHP文件上传error的错误类型 - $_FILES['file']['error']

    假设文件上传字段的名称img,则: $_FILES['img']['error']有以下几种类型 1.UPLOAD_ERR_OK 其值为 0,没有错误发生,文件上传成功. 2.UPLOAD_ERR_I ...

  5. kindEditor完整认识 PHP上调用并上传图片说明

    最近又重新捣鼓了下kindeditor,之前写的一篇文章http://hi.baidu.com/yanghbmail/blog/item/c681be015755160b1d9583e7.html感觉 ...

  6. DEDE用{dede:sql}标签取出当前文档的附加表中的内容

    最近在用DEDE做项目,遇到一个需求是要在article_image.htm模板中直接取出附加表addonimages中的某一记录的imgurls字段的内容.而这条记录是不断变化的,比如我点击了< ...

  7. [转]完美的背景图全屏css代码 – background-size:cover?

    写主题样式的时候经常会碰到用背景图铺满整个背景的需求,这里分享下使用方法 需要的效果 图片以背景的形式铺满整个屏幕,不留空白区域 保持图像的纵横比(图片不变形) 图片居中 不出现滚动条 多浏览器支持 ...

  8. 微信小程序可用的第三方库

    1.wxDraw 轻量的小程序canvas动画库,专门用于处理小程序上canvas 的图形创建.图形动画,以及交互问题. 链接:http://project.ueflat.xyz/#/ 2.ZanUi ...

  9. jq学习总结之方法

    三.方法 1.length 2.index()3.get() reverse()4.not()5.filter()6.find()7.each()8.addBack()9.attr()10.toggl ...

  10. IntelliJ IDEA实时模板变量

    返回由当前方法返回的值的类型IntelliJ IDEA 实时模板中的模板变量允许用户输入.扩展模板后,变量将作为输入字段显示在编辑器中. IntelliJ IDEA 声明实时模板变量 模板中的变量以下 ...