使用MapReduce将HDFS数据导入到HBase(二)
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(二)的更多相关文章
- 使用MapReduce将HDFS数据导入到HBase(一)
		package com.bank.service; import java.io.IOException; import org.apache.hadoop.conf.Configuration;im ... 
- 使用MapReduce将HDFS数据导入到HBase(三)
		使用MapReduce生成HFile文件,通过BulkLoader方式(跳过WAL验证)批量加载到HBase表中 package com.mengyao.bigdata.hbase; import j ... 
- 使用MapReduce将HDFS数据导入Mysql
		使用MapReduce将Mysql数据导入HDFS代码链接 将HDFS数据导入Mysql,代码示例 package com.zhen.mysqlToHDFS; import java.io.DataI ... 
- 使用MapReduce将mysql数据导入HDFS
		package com.zhen.mysqlToHDFS; import java.io.DataInput; import java.io.DataOutput; import java.io.IO ... 
- 用mapreduce读取hdfs数据到hbase上
		hdfs数据到hbase过程 将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 hbase先创建好表 cre ... 
- HBase(三): Azure HDInsigt HBase表数据导入本地HBase
		目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见 HDP2.4安装(五):集群及组件安装 , ... 
- 将Excel中数据导入数据库(二)
		在上篇文章中介绍到将Excel中数据导入到数据库中,但上篇文章例子只出现了nvachar类型,且数据量很小.今天碰到将Excel中数据导入数据库中的Excel有6419行,其中每行均有48个字段,有i ... 
- HBase结合MapReduce批量导入(HDFS中的数据导入到HBase)
		HBase结合MapReduce批量导入 package hbase; import java.text.SimpleDateFormat; import java.util.Date; import ... 
- 把hdfs数据写入到hbase表
		功能:把hdfs上的数据写入到hbase表. hadoop的mapreduce输出要导入到hbase表,最好先输出HFile格式,再导入hbase,因为HFile是hbase的内部存储格式,所以导入效 ... 
随机推荐
- Android 监控网络状态
			public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (Conn ... 
- C# string.Format谨慎使用
			string.Format string.Format在处理文本的时候很有用处,但是在使用占位符的时候一定要注意内容中的特殊字符{}. 示例 string.Format("你好{0},这是{ ... 
- Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)
			遇着这个提示的话,如果本地只安装了一个mysql,这里写的很详细的 http://www.blogjava.net/asenyifei/articles/82575.html 看这里可以解决,如果本地 ... 
- 公共Webservice
			网络上可供测试的Web Service腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService. ... 
- TCP_DEFER_ACCEPT的坑
			我实现了一个server,支持HTTP协议和内部私有协议,为了简化部署,我设计成一个端口同时兼容两种协议的客户端.根据连接后到达的消息头自动识别客户端协议.这种事情的传统做法是,accept后加入ep ... 
- ASP.NET-FineUI开发实践-9(四)
			现在是这么个问题,在开发中表格是动态出来的,就是标准板是全部字段列出,客户要根据情况列出自己想要的,在增加操作页面的同时要是能用前台自带的功能直接保存到后台就好了,现在的列显示和隐藏是不回发的. 1. ... 
- OD: File Vulnerabilities & Protocols & Fuzz
			IE.Office 等软件有个共同点,即用文件作为程序的主要输入,但攻击者往往会挑战程序员的假定和假设. 文件格式 Fuzz 就是利用畸形文件测试软件的稳健性,其流程一般包括: * 以一个正常文件作为 ... 
- WARNING OGG-01223  TCP/IP error 111 (Connection refused)
			一:问题描述 GGSCI (source_pc) 64> info all Program Status Group Lag at Chkpt Time Sinc ... 
- PHP Cookies
			PHP Cookies cookie 常用于识别用户. Cookie 是什么? cookie 常用于识别用户.cookie 是一种服务器留在用户计算机上的小文件.每当同一台计算机通过浏览器请求页面时, ... 
- 关于操作DC时的资源泄露
			首先应明确一个概念 句柄, 关于句柄的详细介绍请见这里 对于句柄的使用小结:借来的要归还,创建的要释放,选出的要选入[尤其是针对GDI的一些句柄而言,如HPEN,HBRUSH等] 1. 使用GetDC ... 
