import java.io.DataOutput;
import java.io.IOException;
 
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.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader;
import org.apache.hadoop.mapreduce.lib.input.CombineFileSplit;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.LineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileRecordReader;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
 
public class TestCombine extends Configured implements Tool {
    private static class ProvinceMapper extends
            Mapper<Object, Text, Text, Text> {
        @Override
        protected void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {
            System.out.println("value : " + value + " Context " + context);
            context.write(value, value);
        }
    }
 
    private static class ProvinceReducer extends
            Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            for (Text va : values) {
                System.out.println("reduce " + key);
                context.write(key, key);
            }
        }
    }
     
    public static class CombineSequenceFileInputFormat<K, V> extends CombineFileInputFormat<K, V> { 
        @SuppressWarnings({ "unchecked", "rawtypes" }) 
        @Override 
        public RecordReader<K, V> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException { 
            return new CombineFileRecordReader((CombineFileSplit)split, context, CombineLineRecordReader.class); 
        } 
    } 
     
    public static class CombineLineRecordReader<K, V> extends RecordReader<K, V> { 
        private CombineFileSplit split; 
        private TaskAttemptContext context; 
        private int index; 
        private RecordReader<K, V> rr; 
       
        @SuppressWarnings("unchecked") 
        public CombineLineRecordReader(CombineFileSplit split, TaskAttemptContext context, Integer index) throws IOException, InterruptedException { 
            this.index = index;
            this.split = (CombineFileSplit) split; 
            this.context = context; 
       
            this.rr = (RecordReader<K, V>) ReflectionUtils.newInstance(LineRecordReader.class, context.getConfiguration()); 
        } 
       
        @SuppressWarnings("unchecked") 
        @Override 
        public void initialize(InputSplit curSplit, TaskAttemptContext curContext) throws IOException, InterruptedException { 
            this.split = (CombineFileSplit) curSplit; 
            this.context = curContext; 
       
            if (null == rr) { 
                rr = ReflectionUtils.newInstance(SequenceFileRecordReader.class, context.getConfiguration()); 
            } 
       
            FileSplit fileSplit = new FileSplit(this.split.getPath(index), 
                    this.split.getOffset(index), this.split.getLength(index), 
                    this.split.getLocations()); 
               
            this.rr.initialize(fileSplit, this.context); 
        } 
       
        @Override 
        public float getProgress() throws IOException, InterruptedException { 
            return rr.getProgress(); 
        } 
       
        @Override 
        public void close() throws IOException { 
            if (null != rr) { 
                rr.close(); 
                rr = null; 
            } 
        } 
       
        @Override 
        public K getCurrentKey() 
        throws IOException, InterruptedException { 
            return rr.getCurrentKey(); 
        } 
       
        @Override 
        public V getCurrentValue() 
        throws IOException, InterruptedException { 
            return rr.getCurrentValue(); 
        } 
       
        @Override 
        public boolean nextKeyValue() throws IOException, InterruptedException { 
            return rr.nextKeyValue(); 
        } 
    } 
 
     
    public int run(String[] args) throws Exception {
        Configuration conf = new Configuration();
         
        Job job = new Job(conf);
        job.setJobName("TestCombine");
        job.setJarByClass(TestCombine.class);
 
        job.setMapperClass(ProvinceMapper.class);
        job.setReducerClass(ProvinceReducer.class);
         
        job.setInputFormatClass(CombineSequenceFileInputFormat.class);
         
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
         
        String inpath = "/home/hadoop/tmp/combine";
        String outpath = "/home/hadoop/tmp/combineout";
        Path p = new Path(outpath);
         
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(p)){
            fs.delete(p);
        }
        FileInputFormat.addInputPaths(job, inpath);
        FileOutputFormat.setOutputPath(job, p);
 
        return job.waitForCompletion(true) ? 0 : 1;
    }
 
    public static void main(String[] args) throws Exception {
        int ret = ToolRunner.run(new TestCombine(), args);
        System.exit(ret);
    }
}

简单 实现CombineFileInputFormat的更多相关文章

  1. 简单实现CombineFileInputFormat

    import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.conf.Configuration; ...

  2. Hadoop CombineFileInputFormat实现原理及源码分析

    Hadoop适用于少量的大文件场景,而不是大量的小文件场景(这里的小文件通常指文件大小显著小于HDFS Block Size的文件),其主要原因是因为FileInputFormat在为这些小文件生成切 ...

  3. hadoop old API CombineFileInputFormat

    来自:http://f.dataguru.cn/thread-271645-1-1.html 简介 本文主要介绍下面4个方面 1.为什么要使用CombineFileInputFormat 2.Comb ...

  4. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  5. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  6. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  7. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  8. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

  9. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

随机推荐

  1. git基本命令--远程

    git clone: # clone到 <本地目录名> $ git clone <版本库的网址> <本地目录名> # 克隆版本库的时候,所使用的远程主机自动被Git ...

  2. debian下安装repo

    1.去google网站上下载repo脚本(用php语言写成的脚本) https://gerrit.googlesource.com/git-repo/+/stable/repo 可以将脚本复制下来并保 ...

  3. python2.6.6在centos6.4下安装

    1.wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tar.bz2 2. tar xvjf Python-2.6.6.tar.bz2 ...

  4. Concepts and Tricks In CNN

    转自:http://blog.cvmarcher.com/posts/2015/05/17/cnn-trick/ 这篇文章主要讲一下Convolutional Neural Network(CNN)里 ...

  5. android studio布局文件/XML怎么代码补全

    android studio中的布局文件代码补全方式是打第一个字母就提示了,而java代码有时候要按快捷键. 布局文件的话呢,要写在标签开始处才提示,在标签闭合处有时候不提示,有时候在内容里也会有不提 ...

  6. const、volatile、mutable的用法

    http://blog.csdn.net/wuliming_sc/article/details/3717017 const.volatile.mutable的用法 const修饰普通变量和指针 co ...

  7. phonegap的照相机API

    1. Camera Api简单介绍 2. 拍照 3. 预览照片 一. Camera Api简单介绍 Camera选择使用摄像头拍照,或从设备相册中获取一张照片.图片以base64编码的 字符串或图片U ...

  8. lepus bug

    ------------------------------------------------BUG fix-------------------------------------------- ...

  9. Goods transportation

    Goods transportation time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. LightOJ 1370 Bi-shoe and Phi-shoe 数论

    题目大意:f(x)=n 代表1-x中与x互质的数字的个数.给出n个数字a[i],要求f(x)=a[i],求x的和. 思路:每个素数x 有x-1个不大于x的互质数.则f(x)=a[i],若a[i]+1为 ...