HBase with MapReduce (MultiTable Read)
hbase当中没有两表联查的操作,要实现两表联查或者在查询一个表的同时也需要访问另外一张表的时候,可以通过mapreduce的方式来实现,实现方式如下:由于查询是map过程,因此这个过程不需要设计reduce过程。
(1)map的实现
package com.datacenter.HbaseMapReduce.MultiReadTable; import java.io.IOException;
import java.util.NavigableMap;
import java.util.Map.Entry; import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; import com.datacenter.HbaseMapReduce.Read.ReadHbase; public class MuliTableReadmapper extends TableMapper<Text, LongWritable> { private ResultScanner rs=null; @Override
protected void map(ImmutableBytesWritable key, Result value, Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
printResult(value); //输出第二张表的内容
Result temp=rs.next();//这个结果只是一个单元的结果,所谓一个单元可以理解成是一行的数据
while(temp!=null){
printResult(temp);
temp=rs.next();
} } @Override
protected void setup(Context context) throws IOException,
InterruptedException {
// TODO Auto-generated method stub
HConnection hconn = MultiReadTableMain.HbaseUtil(
MultiReadTableMain.rootdir, MultiReadTableMain.zkServer,
MultiReadTableMain.port); HTableInterface ht = hconn.getTable("test"); Scan scan = new Scan();
scan.setCaching(500); // 1 is the default in Scan, which will be bad for
// MapReduce jobs
scan.setCacheBlocks(false); // don't set to true for MR jobs rs = ht.getScanner(scan); } // 按顺序输出
public void printResult(Result rs) { if (rs.isEmpty()) {
System.out.println("result is empty!");
return;
} NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> temps = rs
.getMap();
String rowkey = Bytes.toString(rs.getRow()); // actain rowkey
System.out.println("rowkey->" + rowkey);
for (Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> temp : temps
.entrySet()) {
System.out.print("\tfamily->" + Bytes.toString(temp.getKey()));
for (Entry<byte[], NavigableMap<Long, byte[]>> value : temp
.getValue().entrySet()) {
System.out.print("\tcol->" + Bytes.toString(value.getKey()));
for (Entry<Long, byte[]> va : value.getValue().entrySet()) {
System.out.print("\tvesion->" + va.getKey());
System.out.print("\tvalue->"
+ Bytes.toString(va.getValue()));
System.out.println();
}
}
}
} }
(2)主类的实现
package com.datacenter.HbaseMapReduce.MultiReadTable; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat; import com.datacenter.HbaseMapReduce.Read.ReadHbase;
import com.datacenter.HbaseMapReduce.Read.ReadHbaseMapper; public class MultiReadTableMain {
static public String rootdir = "hdfs://hadoop3:8020/hbase";
static public String zkServer = "hadoop3";
static public String port = "2181"; private static Configuration conf;
private static HConnection hConn = null; public static HConnection HbaseUtil(String rootDir, String zkServer, String port) { conf = HBaseConfiguration.create();// 获取默认配置信息
conf.set("hbase.rootdir", rootDir);
conf.set("hbase.zookeeper.quorum", zkServer);
conf.set("hbase.zookeeper.property.clientPort", port); try {
hConn = HConnectionManager.createConnection(conf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hConn;
} public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
HbaseUtil(rootdir, zkServer, port); // Configuration config = HBaseConfiguration.create(); Job job = new Job(conf, "ExampleRead");
job.setJarByClass(ReadHbase.class); // class that contains mapper Scan scan = new Scan();
scan.setCaching(500); // 1 is the default in Scan, which will be bad for
// MapReduce jobs
scan.setCacheBlocks(false); // don't set to true for MR jobs
// set other scan attrs TableMapReduceUtil.initTableMapperJob("score", // input HBase table name
scan, // Scan instance to control CF and attribute selection
MuliTableReadmapper.class, // mapper
null, // mapper output key
null, // mapper output value
job);
job.setOutputFormatClass(NullOutputFormat.class); // because we aren't
// emitting anything
// from mapper boolean b = job.waitForCompletion(true);
if (!b) {
throw new IOException("error with job!");
}
} }
HBase with MapReduce (MultiTable Read)的更多相关文章
- HBase with MapReduce (Only Read)
		最近在学习HBase,在看到了如何使用Mapreduce来操作Hbase,下面将几种情况介绍一下,具体的都可以参照官网上的文档说明.官网文档连接:http://hbase.apache.org/boo ... 
- [转帖]HBase详解(很全面)
		HBase详解(很全面) very long story 简单看了一遍 很多不明白的地方.. 2018-06-08 16:12:32 卢子墨 阅读数 34857更多 分类专栏: HBase [转自 ... 
- HBase Block Cache(块缓存)
		Block Cache HBase提供了两种不同的BlockCache实现,用于缓存从HDFS读出的数据.这两种分别为: 默认的,存在于堆内存的(on-heap)LruBlockCache 存在堆外内 ... 
- HBase笔记4(调优)
		Master/Region Server调优 JVM调优 默认的RegionServer内存是1G,而Memstore默认占40%,即400M,实在是太小了,可以通过HBASE_HEAPSIZE参数修 ... 
- HBase with MapReduce (SummaryToFile)
		上一篇文章是实现统计hbase单元值出现的个数,并将结果存放到hbase的表中,本文是将结果存放到hdfs上.其中的map实现与前文一直,连接:http://www.cnblogs.com/ljy20 ... 
- HBase with MapReduce (Summary)
		我们知道,hbase没有像关系型的数据库拥有强大的查询功能和统计功能,本文实现了如何利用mapreduce来统计hbase中单元值出现的个数,并将结果携带目标的表中, (1)mapper的实现 pac ... 
- HBase with MapReduce (Read and Write)
		上面一篇文章仅仅是介绍如何通过mapReduce来对HBase进行读的过程,下面将要介绍的是利用mapreduce进行读写的过程,前面我们已经知道map实际上是读过程,reduce是写的过程,然而ma ... 
- Hadoop学习笔记—15.HBase框架学习(基础实践篇)
		一.HBase的安装配置 1.1 伪分布模式安装 伪分布模式安装即在一台计算机上部署HBase的各个角色,HMaster.HRegionServer以及ZooKeeper都在一台计算机上来模拟. 首先 ... 
- hbase 集群(完全分布式)方式安装
		一,环境 1, 主节点一台: ubuntu desktop 16.04 zhoujun 172.16.12.1 从节点(slave)两台:ubuntu server 16.04 hadoo ... 
随机推荐
- postgresql命令行
			原文链接 PostgreSQL 8.1 中文文档 连接数据库, 默认的用户和数据库是postgrespsql -U user -d dbname \c dbname 切换数据库,相当于mysql的us ... 
- tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080
			1.INFO: Maximum number of threads (200) created for connector with address null and port 8091 说明:最大线 ... 
- eclipse编辑struts.xml 代码提示
			先确定xml文件 window-preferences-查询catalog 点击add 关于这个Location 先找到你下载的struts压缩包 然后找到 解压这个jar包 你会得到一些dtd文件 ... 
- [OGRE]最小ogre程序的流程
			总结一下最小ogre程序的流程: 1 创建Ogre::Root 2 用Ogre::Root加载插件,必须载入的是场景管理器和渲染器 3 调用Ogre::ResourceGroupManager::ge ... 
- LeetCode算法题解
			1.给定两个正整数(二进制形式表示)A和B,问把A变为B需要改变多少位(bit)?也就是说,整数A和B的二进制表示中有多少位是不同的?(181) 解法一:举例说明,为了减少复杂度,就使用八位二进制吧. ... 
- HTML5正则表单式验证电子邮件
			<input type="text" title="email" required pattern="[^@]+@[^@]+\.[a-zA-Z] ... 
- Count Color(线段树+位运算 POJ2777)
			Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ... 
- laravel框架总结(三) -- 路径分析
			1.直接写绝对路径,这样会用在/goods/show前面加上域名 <a href="/goods/show?id=<?php echo $item['id']; ?>&qu ... 
- ubuntu如何以删除文件夹?
			rm [选项] 文件 -f, --force 强力删除,不要求确认 -i 每删除一个文件或进入一个子目录都要求确认 -I 在删除超过三个文件或者递归删除前要求确认 -r, -R 递归删除子目录 -d, ... 
- CentOS7 基础配置
			Centos 7 部分>>>>>>>>>>>>>>>>>>>>>>& ... 
