package com.bank.service;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 使用MapReduce批量导入Hbase(没有Reduce函数的MapReduce)
 * @author mengyao
 *
 */
public class DataImportToHbase extends Configured implements Tool {

static class DataImportToHbaseMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
        private static String familyName = "info";
        private static String[] qualifiers = {"gzh", "currency", "version", "valuta", "qfTime", "flag", "machineID"};
        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String line = value.toString();
            String[] values = line.split("\t");
            if (values.length == 7 && values.length == qualifiers.length) {
                String row = values[0]+"_"+values[1]+"_"+values[2]+"_"+values[3];
                long timestamp = System.currentTimeMillis();
                ImmutableBytesWritable immutable = new ImmutableBytesWritable(Bytes.toBytes(row));
                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(immutable, 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(), DataImportToHbase.class.getSimpleName());
        job.setJarByClass(DataImportToHbase.class);
        
        job.setInputFormatClass(TextInputFormat.class);
        FileInputFormat.setInputPaths(job, new Path(arg0[0]));
        
        job.setMapperClass(DataImportToHbaseMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(Put.class);
        
        TableMapReduceUtil.initTableReducerJob(arg0[1], null, job);        
        job.setNumReduceTasks(0);
        TableMapReduceUtil.addDependencyJars(job);
        
        return job.waitForCompletion(true) ? 0 : 1;
    }
    
    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "h5:2181,h6:2181,h7:2181");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("dfs.socket.timeout", "3600000");
        String[] otherArgs = new GenericOptionsParser(args).getRemainingArgs();
        if (otherArgs.length != 2) {
            System.err.println(" ERROR: <dataInputDir> <tableName>");
            System.exit(2);
        }
        int status = ToolRunner.run(conf, new DataImportToHbase(), otherArgs);
        System.exit(status);
    }
}

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

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

    package com.bank.service; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf. ...

  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. HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)

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

  8. 把hdfs数据写入到hbase表

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

  9. sqoop将关系型的数据库得数据导入到hbase中

    1.sqoop将关系数据库导入到hbase的参数说明

随机推荐

  1. 阻塞机制下的recv小结

    recv是socket编程中最常用的函数之一,在阻塞状态的recv有时候会返回不同的值,而对于错误值也有相应的错误码,分别对应不同的状态,下面是我针对常见的几种网络状态的简单总结.      首先阻塞 ...

  2. linux系统用户锁定与解锁

    1.使用passwd命令锁定与解锁账号 [root@rhel7 ~]# passwd -l lxj --- -l 锁定 Locking password for user lxj. passwd: S ...

  3. ASP.NET-FineUI开发实践-1

    用.Net开发3年,主要接触资产管理,生产流程,质量追溯,.Net在这方面还是很靠谱的.2月低接触FineUI,那时版本是3.0+,第一眼让我想起了ExtJs,其实就是用.Net封装了ExtJs让AS ...

  4. 修改登录linux之后显示的默认文件夹目录

    命令如下: ll -a vim .bash_profile 最后一行加上cd 需要显示的文件夹

  5. WinAPI——谐振动的合成

    #include<Windows.h> #include<math.h> #define NUM 1000 #define PI 3.14159 LRESULT CALLBAC ...

  6. “The SQL Server license agreenment cannot be located for the selected edition.”MSSQL安装问题

    今天老邹又来吐槽了.今天不和IE7较劲了.说点别的吧. 我呢什么软件都喜欢装最新版的,这部刚出来windows 8.1就赶紧装上了,随后就用上了vs2013.前天看到新闻说微软已经发布了sql ser ...

  7. js 定义函数的几种方法 以及如何调用

    /*1.方法调用模式: 先定义一个对象,然后在对象的属性中定义方法,通过myobject.property来执行方法,this即指当前的myobject 对象.*/ var car = { carId ...

  8. 1201.1——Vim编辑器的相关操作

    一 vi的操作模式 vi提供两种操作模式:输入模式(insert mode)和指令模式(command mode).在输入模式下,用户可输入文本资料.在指令模式下,可进行删除.修改等各种编辑动作. 在 ...

  9. iOS中枚举定义的三种方式

    最简单的方式 typedef enum{ num1 = 0, num2 = 1, num3 = 2 }num; 同时我们还可以使用NS_ENUM的方式定义枚举 typedef NS_ENUM (NSI ...

  10. 3D dungeon

    算法:广搜: 描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is comp ...