在进行Map/Reduce时,有的业务需要在一个job中将数据写入到多个HBase的表中,下面是实现方式。

原文地址:http://lookfirst.com/2011/07/hbase-multitableoutputformat-writing-to.html

HBase MultiTableOutputFormat writing to multiple tables in one Map Reduce Job

 
Recently, I've been having a lot of fun learning about HBase and Hadoop. One esoteric thing I just learned about is the way that HBase tables are populated.

By default, HBase / Map Reduce jobs can only write to a single table because you set the output handler at the job level with the job.setOutputFormatClass(). However, if you are creating an HBase table, chances are that you are going to want to build an index related to that table so that you can do fast queries on the master table. The most optimal way to do this is to write the data to both tables at the same time when you are importing the data. The alternative is to write another M/R job to do this after the fact, but that means reading all of the data twice, which is a lot of extra load on the system for no real benefit. Thus, in order to write to both tables at the same time, in the same M/R job, you need to take advantage of the MultiTableOutputFormat class to achieve this result. The key here is that when you write to the context, you specify the name of the table you are writing to. This is some basic example code (with a lot of the meat removed) which demonstrates this.

static class TsvImporter extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
@Override
public void map(LongWritable offset, Text value, Context context) throws IOException {
// contains the line of tab separated data we are working on (needs to be parsed out).
byte[] lineBytes = value.getBytes(); // rowKey is the hbase rowKey generated from lineBytes
Put put = new Put(rowKey);
// Create your KeyValue object
put.add(kv);
context.write("actions", put); // write to the actions table // rowKey2 is the hbase rowKey
Put put = new Put(rowKey2);
// Create your KeyValue object
put.add(kv);
context.write("actions_index", put); // write to the actions table
}
} public static Job createSubmittableJob(Configuration conf, String[] args) throws IOException {
String pathStr = args[0];
Path inputDir = new Path(pathStr);
Job job = new Job(conf, "my_custom_job");
job.setJarByClass(TsvImporter.class);
FileInputFormat.setInputPaths(job, inputDir);
job.setInputFormatClass(TextInputFormat.class); // this is the key to writing to multiple tables in hbase
job.setOutputFormatClass(MultiTableOutputFormat.class);
job.setMapperClass(TsvImporter.class);
job.setNumReduceTasks(0); TableMapReduceUtil.addDependencyJars(job);
TableMapReduceUtil.addDependencyJars(job.getConfiguration());
return job;
}
 

【转】在一个Job中同时写入多个HBase的table的更多相关文章

  1. Linux如何在一个文件中写入内容

    Linux中,在一个文件中写入内容,可以vim打开编辑模式,输入我们想要的内容,此次我们使用echo命令 来在一个文件夹中写入内容. echo命令: 第一种: echo 'i love u' > ...

  2. gulp 批量添加类名 在一个任务中使用多个文件来源

    1.首先安装环境 1.安装gulp: npm install gulp 2.安装gulp-clean-css npm install gulp-clean-css 3.安装gulp-css-wrap ...

  3. 大数据学习day34---spark14------1 redis的事务(pipeline)测试 ,2. 利用redis的pipeline实现数据统计的exactlyonce ,3 SparkStreaming中数据写入Hbase实现ExactlyOnce, 4.Spark StandAlone的执行模式,5 spark on yarn

    1 redis的事务(pipeline)测试 Redis本身对数据进行操作,单条命令是原子性的,但事务不保证原子性,且没有回滚.事务中任何命令执行失败,其余的命令仍会被执行,将Redis的多个操作放到 ...

  4. SQL语句 在一个表中插入新字段

    SQL语句 在一个表中插入新字段: alter table 表名 add 字段名 字段类型 例: alter table OpenCourses add Audio varchar(50)alter ...

  5. 【编程题目】在一个字符串中找到第一个只出现一次的字符。如输入 abaccdeff,则输出 b。

    第 17 题(字符串):题目:在一个字符串中找到第一个只出现一次的字符.如输入 abaccdeff,则输出 b. 思路:此题非常容易. 最开始是想开辟一块空间存储每个字符出现的次数. 但转念一想,似乎 ...

  6. MVC中在一个视图中,怎么加载另外一个视图?

    在RazorView.cshtml视图: <!--在视图中调用无返回值的方法,视图中调用无返回值的方法,要加上大括号--> <!--在一个视图中,直接加载另外一个视图--> @ ...

  7. SharePoint Iframe 报错“此内容不能显示在一个框架中”<续>

    在之前的SharePoint站点iframe引用中,我们遇到过下面的问题,就是其它系统或者不通环境的SharePoint站点,引用SharePoint页面会报错“此内容不能显示在一个框架中”,之前我们 ...

  8. SharePoint Iframe 报错“此内容不能显示在一个框架中”

    问题描述 我们SharePoint站点用Excel Service发布的Excel,需要Iframe到其他系统中,但是,Iframe的时候发现报错“此内容不能显示在一个框架中”. 后来,尝试在其他系统 ...

  9. 为什么super()和this()调用语句不能同时在一个构造函数中出现的解释

    我想这应该是Java构造函数的一种机制吧,首先以子类和父类为例.当你创建一个子类的实例时,首先会调用父类的构造函数,然后再调用子类的构造函数,如果父类中没有缺省构造函数,则必须再子类的构造函数中显示的 ...

随机推荐

  1. JAVA设计模式——第 4 章 多例模式【Multition Pattern】(转)

    一个国家有多个皇帝这种情况有没有?还确实有,就出现在明朝,那三国期间的算不算,不算!因为各自称帝,各有各的地盘,国号不同.大家还记得那首诗<石灰吟>吗?作者是谁?于谦,他是被谁杀死的?明英 ...

  2. js跨域解决方式

    什么是跨域? 概念:仅仅要协议.域名.port有不论什么一个不同,都被当作是不同的域.(所谓同源是指,域名.协议,port同样.),对于port和协议的不同,仅仅能通过后台来解决. URL 说明 是否 ...

  3. Spring学习笔记二:注入方式

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774608.html  我们说,IOC的实现方式是依赖注入,也就是把被依赖对象赋值到依赖对象的成员属性.怎么做 ...

  4. kettle Spoon.bat运行闪退

      1.情景展示 启动kettle的Spoon.bat闪退,并没有进入kettle的启动界面. 2.原因分析 使用条件: jdk版本需>=1.6: java需配置环境变量. 如果满足了上述前提条 ...

  5. 〖Linux〗Ubuntu设定Proxy及忽略Proxy

    1. 设定代理:. ~/.proxyenv #!/bin/sh # for terminal export proxyserveraddr=123.123.123.123 export proxyse ...

  6. Spring Cloud Eureka集群 动态扩展新节点

    场景描述: Eureka的集群节点有两个,互相注册形成集群,已经支持动态刷新(不知道怎么让Eureka支持动态刷新的可以参考http://www.cnblogs.com/flying607/p/845 ...

  7. Makefile 和 CMakeLists.txt

    Makefile Makefile 的格式 target: prerequisites [tab]command 例子 #Makefile all:chap1 chap2 chap1: - - - : ...

  8. 转:3d max 2013 安装教程,凭着一种互联网精神提供给广大朋友

    看到有人在ps区咨询如何安装3d max教程,当你进行第一步之前,请先断开网络连接第一步:运行安装程序 第二步:接受安装协议,点击下一步会提示输入序列号 第三步:你会看到已经安装完成了的界面 第四部: ...

  9. jsp+easyui+DataGrid 例子

    转自:https://blog.csdn.net/l3922768721/article/details/51597977 导入js和css <%@ page language="ja ...

  10. linux解决“XXX is not in the sudoers file”错误

    问题:我想在我的Linux系统上使用sudo来运行一些特权命令,然而当我试图这么做时,我却得到了"[我的用户名] is not in the sudoers file. This incid ...