package com.bank.service;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
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.lib.input.FileInputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 使用MapReduce批量导入Hbase
 *     通过TableOutputFormat,该类内部传给指定的Put实例并调用table.put()方法。作业结束前会主动调用flushCommits()方法保存仍在写缓冲区的数据
 *
 * @author mengyao
 *
 */
public class CnyBatch extends Configured implements Tool {

static class CnyBatchMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
        protected void map(LongWritable key, Text value, Context context)
                throws java.io.IOException, InterruptedException {
            context.write(key, value);
        }
    }

static class CnyBatchReduce extends TableReducer<LongWritable, Text, NullWritable> {
        private final static String familyName = "info";
        private final static String[] qualifiers = {"gzh", "currency", "version", "valuta", "qfTime", "flag", "machineID"};
        @Override
        protected void reduce(LongWritable key,
                java.lang.Iterable<Text> value, Context context)
                throws java.io.IOException, InterruptedException {
            final String[] values = value.toString().split("\t");
            if (values.length == 7 && values.length == qualifiers.length) {
                 final String row = values[0]+"_"+values[1]+"_"+values[2]+"_"+values[3];
                 long timestamp = System.currentTimeMillis();
                 Put put = new Put(Bytes.toBytes(row));
                 for (int i = 0; i < values.length; i++) {
                     String qualifier = qualifiers[i];
                     String val = values[i];
                     put.add(Bytes.toBytes(familyName), Bytes.toBytes(qualifier), timestamp, Bytes.toBytes(val));
                 }
                 context.write(NullWritable.get(), put);
            } else {
                 System.err.println(" ERROR: value length must equale qualifier length ");
            }
        };
    }

@Override
    public int run(String[] arg0) throws Exception {
        Job job = Job.getInstance(getConf(), CnyBatch.class.getSimpleName());
        TableMapReduceUtil.addDependencyJars(job);
        job.setJarByClass(CnyBatch.class);
        
        FileInputFormat.setInputPaths(job, arg0[0]);
        job.setMapperClass(CnyBatchMapper.class);
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(Text.class);
        
        job.setReducerClass(CnyBatchReduce.class);
        job.setOutputFormatClass(TableOutputFormat.class);
        
        
        return job.waitForCompletion(true) ? 0 : 1;
    }

public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "h5:2181,h6:2181,h7:2181");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("dfs.socket.timeout", "100000");
        String[] otherArgs = new GenericOptionsParser(args).getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println(" ERROR: <dataInputDir> <tableName>");
            System.exit(2);
        }
        conf.set(TableOutputFormat.OUTPUT_TABLE, args[1]);
        int status = ToolRunner.run(conf, new CnyBatch(), args);
        System.exit(status);
    }
}

使用MapReduce将HDFS数据导入到HBase(二)的更多相关文章

  1. 使用MapReduce将HDFS数据导入到HBase(一)

    package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ...

  2. 使用MapReduce将HDFS数据导入到HBase(三)

    使用MapReduce生成HFile文件,通过BulkLoader方式(跳过WAL验证)批量加载到HBase表中 package com.mengyao.bigdata.hbase; import j ...

  3. 使用MapReduce将HDFS数据导入Mysql

    使用MapReduce将Mysql数据导入HDFS代码链接 将HDFS数据导入Mysql,代码示例 package com.zhen.mysqlToHDFS; import java.io.DataI ...

  4. 使用MapReduce将mysql数据导入HDFS

    package com.zhen.mysqlToHDFS; import java.io.DataInput; import java.io.DataOutput; import java.io.IO ...

  5. 用mapreduce读取hdfs数据到hbase上

    hdfs数据到hbase过程 将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 hbase先创建好表   cre ...

  6. HBase(三): Azure HDInsigt HBase表数据导入本地HBase

    目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见  HDP2.4安装(五):集群及组件安装 , ...

  7. 将Excel中数据导入数据库(二)

    在上篇文章中介绍到将Excel中数据导入到数据库中,但上篇文章例子只出现了nvachar类型,且数据量很小.今天碰到将Excel中数据导入数据库中的Excel有6419行,其中每行均有48个字段,有i ...

  8. HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)

    HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ...

  9. 把hdfs数据写入到hbase表

    功能:把hdfs上的数据写入到hbase表. hadoop的mapreduce输出要导入到hbase表,最好先输出HFile格式,再导入hbase,因为HFile是hbase的内部存储格式,所以导入效 ...

随机推荐

  1. 窗口 对话框 Pop Dialog 示例

    两者的区别 PopupWindow和AlertDialog最大的区别: AlertDialog是非阻塞线程的,AlertDialog弹出的时候,后台可以做其他事情(也即弹出对话框后程序会继续向下执行) ...

  2. 新浪微博开放平台OAuth授权解决方案(含代码)

    前几日一位朋友项目中需要使用新浪微博的接口,故和这位朋友一同研究了新浪微博开放平台上面所提供的资料,首先要使用这些接口是需要用户登录并且授权的,新浪微博开放平台其实是提供两种授权方式的,第一种是:OA ...

  3. ASP.NET数据绑定控件简介

    •数据绑定分为数据源和数据绑定控件两部分(①数据绑定控件通过数据源获取和修改数据②数据绑定控件通过数据源隔离数据提供者和数据使用者)数据绑定控件→数据源→数据库•数据源:SqlDataSource(连 ...

  4. Cer Crt Pem Pfx 证书格式转换

    1.从pfx格式的证书提取出密钥和证书set OPENSSL_CONF=openssl.cnfopenssl pkcs12 -in my.pfx -nodes -out server.pemopens ...

  5. java日期处理总结(二)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzUAAAG1CAIAAABPoU1KAAAgAElEQVR4nOy9e1xU1d747znP9/V9nu

  6. QML之窗口(无边框、透明及拖拽)

    1.无边框 Qt Quick 2.0 中 QQuickView代替了1.0中的QDeclarativeView. 无边框窗口代码如下: QQuickView viwer; //QQuickView继承 ...

  7. phpcms 2008和discuz X3.1实现同步登陆退出论坛(已实现)

    网络上文章很多,按步骤配置好了之后phpcms可以同步登录dz,但是dz登录后状态却无法同步到phpcms,网络上找了很多资料都大同小异,头大.只能自己调试了,废话不多说了.       以下网络上抄 ...

  8. WordPress插件制作笔记(一)---First Plugins Demo

    1->add_action  HOOK简单说明: http://codex.wordpress.org/Plugin_API/Action_Reference (参考网址) //在后台页脚位置加 ...

  9. PHP学习系列(1)——字符串处理函数(4)

    16.hebrevc() 函数把希伯来文本从右至左的流转换为左至右的流.它也会把新行 (\n) 转换为 <br />.只有 224 至 251 之间的 ASCII 字符,以及标点符号受到影 ...

  10. Xcode 如何设置 自动内存管理 转换为 手动内存管理

    建议使用自动内存管理 ARC. 如果不想自动内存管理,可以在build phases 下的compile sources 中找到不想自动管理的.m文件 ,给它加compiler flags 为 -fn ...