HBase with MapReduce (Only Read)
最近在学习HBase,在看到了如何使用Mapreduce来操作Hbase,下面将几种情况介绍一下,具体的都可以参照官网上的文档说明。官网文档连接:http://hbase.apache.org/book.html 。通过学习我个人的对MapReduce操作HBase的方式可以看作的是Map过程是负责读取过程,Reduce负责的是写入的过程,一读一写可以完成对HBase的读写过程。
利用MapReduce 读取(Read)HBase中的表数据,这一过程由于只涉及到读过程,因此仅仅只需要实现Map函数即可。
(1)ReadHbaseMapper类的实现是需要继承TableMapper的,具体的实现如下:
package com.datacenter.HbaseMapReduce.Read; import java.io.IOException;
import java.util.Map.Entry;
import java.util.NavigableMap; import org.apache.hadoop.hbase.client.Result;
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.Text; public class ReadHbaseMapper extends TableMapper<Text, Text> {
public void map(ImmutableBytesWritable row, Result value, Context context)
throws InterruptedException, IOException {
// process data for the row from the Result instance.
printResult(value);
} // 按顺序输出
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)添加main函数类,来加载配置信息,是实现如下:
package com.datacenter.HbaseMapReduce.Read; 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; //通过map从hbase中读取数据 public class ReadHbase { 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 void 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();
}
} public static void main(String[] args) throws Exception { 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
ReadHbaseMapper.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 (Only Read)的更多相关文章
- HBase with MapReduce (MultiTable Read)
hbase当中没有两表联查的操作,要实现两表联查或者在查询一个表的同时也需要访问另外一张表的时候,可以通过mapreduce的方式来实现,实现方式如下:由于查询是map过程,因此这个过程不需要设计re ...
- [转帖]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 ...
随机推荐
- JSTL函数
JSTL包含了一系列标准函数. 引入:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functi ...
- NET中的类型和装箱/拆箱原理
谈到装箱拆箱,DebugLZQ相信给位园子里的博友一定可以娓娓道来,大概的意思就是值类型和引用类型的相互转换呗---值类型到引用类型叫装箱,反之则叫拆箱.这当然没有问题,可是你只知道这么多,那么Deb ...
- 第一册解说and表现
综合日语第一册第五单元到十五单元的解说表现大集会:(这么一来一本书都被我搬上来啦--) 第五课 1.始めました: 初次见面时的寒暄用语,表示“初次见面(请多多观照)”之意. 2.どうぞよろしく: 用于 ...
- StringBuilder和StringBuffer区别
一.StringBuilder 一个可变的字符序列.此类提供了一个与StringBuffer兼容的API,但不保证同步.该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程 ...
- JavaScript DOM 编程艺术(第2版)读书笔记 (9)
三位一体的网页 结构层:由HTML或XHTML之类的标记语言负责创建: 表示层:由CSS负责完成: 行为层:负责内容应该如何响应事件这一问题.这是由JavaScript语言和DOM主宰的领域. 分离 ...
- POJ 1113:Wall
原文链接:https://www.dreamwings.cn/poj1113/2832.html Wall Time Limit: 1000MS Memory Limit: 10000K Tota ...
- noi 6049 买书
题目链接: http://noi.openjudge.cn/ch0206/6049/ 6049:买书 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 小明手里有n ...
- ContentProvider总结
一.使用ContentProvider(内容提供者)共享数据 ContentProvider在android中的作用是对外共享数据,也就是说你可以通过ContentProvider把应用中的数据共享给 ...
- Oracle软件开发分析
软件开发的步骤可大致分为: 1.需求分析 2.系统设计 3.编码实现 4.系统测试 5.运行维护 student class 多对一 sno name age gender cid id c ...
- Statement returned more than one row, where no more than one was expected
Statement returned more than one row, where no more than one was expected <resultMap id="Stu ...