package com.bank.service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 将非结构化的数据处理为结构化数据
 * @author mengyao
 *
 */
import com.bank.entity.CNY;

public class CnyDataFormat extends Configured implements Tool {

static class CnyDataFormatMapper extends Mapper<LongWritable, Text, NullWritable, CNY>{
        
        CNY cny = new CNY();
        
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] fields = line.split("\t");
            if (fields.length == 42) {
                String gzh = fields[12] ;
                String currency = fields[9];
                String version = fields[10];
                String valuta = fields[11];
                long qfTime;
                try {
                    qfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(fields[3]+" "+fields[4]).getTime();
                } catch (ParseException e) {
                    qfTime = System.currentTimeMillis();
                }
                int flag = Integer.parseInt(fields[5]);
                String machineID = fields[13];
                cny.set(gzh, currency, version, valuta, qfTime, flag, machineID);
                context.write(NullWritable.get(), cny);
            } else {
                System.err.println(" ERROR: data format failed!");
            }
        }
    }
    
    static class CnyDataFormatReduce extends Reducer<NullWritable, CNY, NullWritable, CNY>{
        @Override
        protected void reduce(NullWritable key, Iterable<CNY> value, Context context) throws IOException, InterruptedException {
            for (CNY cny : value) {
                context.write(NullWritable.get(), cny);
            }
        }
    }

@Override
    public int run(String[] arg0) throws Exception {
        Job job = Job.getInstance(getConf(), CnyDataFormat.class.getSimpleName());
        job.setJarByClass(CnyDataFormat.class);                                //设置main函数所在的类
        
        FileInputFormat.setInputPaths(job, new Path(arg0[0]));
        job.setMapperClass(CnyDataFormatMapper.class);
        job.setMapOutputKeyClass(NullWritable.class);
        job.setMapOutputValueClass(CNY.class);
        
        job.setReducerClass(CnyDataFormatReduce.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(CNY.class);
        FileOutputFormat.setOutputPath(job, new Path(arg0[1]));
        
        return job.waitForCompletion(true) ? 0 : 1;                                //等待MapReduce执行完成并打印作业进度详情
    }

public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] paths = new GenericOptionsParser(conf, args).getRemainingArgs();
        if (paths.length != 2) {
            System.err.println("Usage: " + CnyDataFormat.class.getName() + " <in> <out>");
            System.exit(2);
        }
        int status = ToolRunner.run(new CnyDataFormat(), args);
        System.exit(status);
        
    }
}

package com.bank.entity;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Writable;

/**
 * 实现Hadoop的序列化接口
 * @author mengyao
 *
 */
public class CNY implements Writable {

private String gzh;
    private String currency;
    private String version;
    private String valuta;
    private long qfTime;
    private int flag;
    private String machineID;
    
    @Override
    public void readFields(DataInput in) throws IOException {
        this.gzh = in.readUTF();
        this.currency = in.readUTF();
        this.version = in.readUTF();
        this.valuta = in.readUTF();
        this.qfTime = in.readLong();
        this.flag = in.readInt();
        this.machineID = in.readUTF();
    }
    
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(this.gzh);
        out.writeUTF(this.currency);
        out.writeUTF(this.version);
        out.writeUTF(this.valuta);
        out.writeLong(this.qfTime);
        out.writeInt(this.flag);
        out.writeUTF(this.machineID);
    }

public void set(String gzh, String currency, String version,
            String valuta, long qfTime, int flag, String machineID) {
        this.gzh = gzh;
        this.currency = currency;
        this.version = version;
        this.valuta = valuta;
        this.qfTime = qfTime;
        this.flag = flag;
        this.machineID = machineID;
    }

@Override
    public String toString() {
        return this.gzh +"\t"+ this.currency +"\t"+ this.version +"\t"+ this.valuta +"\t"+ this.qfTime +"\t"+ this.flag +"\t"+ this.machineID;
    }

public String getGzh() {
        return gzh;
    }

public void setGzh(String gzh) {
        this.gzh = gzh;
    }

public String getCurrency() {
        return currency;
    }

public void setCurrency(String currnecy) {
        this.currency = "cny";
    }

public String getVersion() {
        return version;
    }

public void setVersion(String version) {
        this.version = version;
    }

public String getValuta() {
        return valuta;
    }

public void setValuta(String valuta) {
        this.valuta = valuta;
    }

public long getQfTime() {
        return qfTime;
    }

public void setQfTime(long qfTime) {
        this.qfTime = qfTime;
    }

public int getFlag() {
        return flag;
    }

public void setFlag(int flag) {
        this.flag = flag;
    }

public String getMachineID() {
        return machineID;
    }

public void setMachineID(String machineID) {
        this.machineID = machineID;
    }
   
    
}

Hadoop2.4.1 使用MapReduce简单的数据清洗的更多相关文章

  1. MapReduce 简单的全文搜索2

    上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找mapreduce is simple那么他就只查找有这个句子的文章,而不是查找有这三个单词的文章. 这个版本需要重写反向索引,因为 ...

  2. oozie与mapreduce简单案例

    准备工作  拷贝原来的模板 mkdir oozie-apps cd oozie-apps/ cp -r ../examples/apps/mar-reduce . mv map-reduce mr-w ...

  3. Hadoop2 使用 YARN 运行 MapReduce 的过程源码分析

    Hadoop 使用 YARN 运行 MapReduce 的过程如下图所示: 总共分为11步. 这里以 WordCount 为例, 我们在客户端终端提交作业: # 把本地的 /home/hadoop/t ...

  4. MapReduce 简单数据统计

    1. 准备数据源 摘录了一片散文,保存格式为utf-8 2. 准备环境 2.1 搭建伪分布式环境 https://www.cnblogs.com/cjq10029/p/12336446.html 上传 ...

  5. MapReduce简单执行过程及Wordcount案例

    MapReducer运行过程 以单词统计为案例. 假如现在文件中存在如下内容: aa bb aa cc dd aa 当然,这是小文件,如果文件大小较大时会将文件进行 "切片" ,此 ...

  6. 【hadoop2.6.0】MapReduce原理

    看了几篇博文,感觉还是云里雾里的. http://blog.csdn.net/opennaive/article/details/7514146 http://www.aboutyun.com/thr ...

  7. hadoop2.2编程:mapreduce编程之二次排序

    mr自带的例子中的源码SecondarySort,我重新写了一下,基本没变. 这个例子中定义的map和reduce如下,关键是它对输入输出类型的定义:(java泛型编程) public static ...

  8. MapReduce 简单的全文搜索

    上一个已经实现了反向索引,那么为什么不尝试下全文搜索呢.例如有了 Hello     file3.txt:1; MapReduce     file3.txt:2;fil1.txt:1;fil2.tx ...

  9. hadoop mapreduce 简单例子

    本例子统计 用空格分开的单词出现数量(  这个Main.mian 启动方式是hadoop 2.0 的写法.1.0 不一样 ) 目录结构: 使用的 maven : 下面是maven 依赖. <de ...

随机推荐

  1. ubuntu 下操作文件夹,出现Permission denied的解决的方法

    今天遇到个诡异问题,向一个文件夹(myResources)粘贴文件的时候,出现这样一个提示 Permission denied 是权限没设好,仅仅是拷贝粘贴一个文件,怎么会这样? 解决的办法: $ s ...

  2. Json序列反序列类型处理帮助类

    Json序列反序列类型处理帮助类. JSON反序列化 JSON序列化 将Json序列化的时间由/Date(1294499956278+0800)转为字符串 将时间字符串转为Json时间 using S ...

  3. 【网络流#6】POJ 3041 Asteroids 二分图最大匹配 - 《挑战程序设计竞赛》例题

    学习网络流中ing...作为初学者练习是不可少的~~~构图方法因为书上很详细了,所以就简单说一说 把光束作为图的顶点,小行星当做连接顶点的边,建图,由于 最小顶点覆盖 等于 二分图最大匹配 ,因此求二 ...

  4. SpringMVC04controller中定义多个方法

    public class MyController extends MultiActionController { // 新增 方法修饰符要是public public ModelAndView ad ...

  5. C#之Linq学习笔记【转】

    写在前面 其实在09年就已经学习过Linq了,并被她那优美的语法所吸引,只是现在所在的公司还在使用VS2005在.Net2.0的框架下面的开发,所以Linq也很久没有用过了,最近看部门的同事对这个有些 ...

  6. php的mq客户端获取队列方法改造

    获取mq中消息然后处理失败重试机制: 下面的代码是php连接mq客户端的获取queue队列中的消息代码: public function createDurableSubscriber($queue, ...

  7. Wcf序列化的循环引用问题1

    1.Wcf数据契约序列化,使用的类DataContractSerializer 默认如果类不指定[DataContract],则序列化类的所有字段,并且在出现循环引用的时候回抛出异常,服务终止 msd ...

  8. js获取当前日期时间同时显示星期

    JavaScript获取当前日期时间同时显示星期几,具体代码如下: <html> <head> <meta http-equiv="Content-Type&q ...

  9. xxx is not in the sudoers file. This incident will be reported的解决方法

    1>.进入超级用户模式.也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式.(当然,你也可以直接用root用户登录,因为红旗安装过后默认的登录 ...

  10. Android 5.1 Camera 架构学习之Camera初始化

    Android Camera 采用C/S架构,client 与server两个独立的线程之间(CameraService)使用Binder通信. 一 CameraService的注册. 1.手机开机后 ...