`
hougbin
  • 浏览: 492627 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

HBASE

 
阅读更多

1、搭建环境

新建JAVA项目,添加的包有:

有关Hadoop的hadoop-core-0.20.204.0.jar

有关Hbase的hbase-0.90.4.jar、hbase-0.90.4-tests.jar以及Hbase资源包中lib目录下的所有jar包

 

2、主要程序

 

Java代码
  1. package com.wujintao.hbase.test;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.hbase.HBaseConfiguration;
  7. import org.apache.hadoop.hbase.HColumnDescriptor;
  8. import org.apache.hadoop.hbase.HTableDescriptor;
  9. import org.apache.hadoop.hbase.KeyValue;
  10. import org.apache.hadoop.hbase.MasterNotRunningException;
  11. import org.apache.hadoop.hbase.ZooKeeperConnectionException;
  12. import org.apache.hadoop.hbase.client.Delete;
  13. import org.apache.hadoop.hbase.client.Get;
  14. import org.apache.hadoop.hbase.client.HBaseAdmin;
  15. import org.apache.hadoop.hbase.client.HTable;
  16. import org.apache.hadoop.hbase.client.HTablePool;
  17. import org.apache.hadoop.hbase.client.Put;
  18. import org.apache.hadoop.hbase.client.Result;
  19. import org.apache.hadoop.hbase.client.ResultScanner;
  20. import org.apache.hadoop.hbase.client.Scan;
  21. import org.apache.hadoop.hbase.filter.Filter;
  22. import org.apache.hadoop.hbase.filter.FilterList;
  23. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
  24. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
  25. import org.apache.hadoop.hbase.util.Bytes;
  26. public class JinTaoTest {
  27. public static Configuration configuration;
  28. static {
  29. configuration = HBaseConfiguration.create();
  30. configuration.set("hbase.zookeeper.property.clientPort", "2181");
  31. configuration.set("hbase.zookeeper.quorum", "192.168.1.100");
  32. configuration.set("hbase.master", "192.168.1.100:600000");
  33. }
  34. public static void main(String[] args) {
  35. // createTable("wujintao");
  36. // insertData("wujintao");
  37. // QueryAll("wujintao");
  38. // QueryByCondition1("wujintao");
  39. // QueryByCondition2("wujintao");
  40. //QueryByCondition3("wujintao");
  41. //deleteRow("wujintao","abcdef");
  42. deleteByCondition("wujintao","abcdef");
  43. }
  44. /**
  45. * 创建表
  46. * @param tableName
  47. */
  48. public static void createTable(String tableName) {
  49. System.out.println("start create table ......");
  50. try {
  51. HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
  52. if (hBaseAdmin.tableExists(tableName)) {// 如果存在要创建的表,那么先删除,再创建
  53. hBaseAdmin.disableTable(tableName);
  54. hBaseAdmin.deleteTable(tableName);
  55. System.out.println(tableName + " is exist,detele....");
  56. }
  57. HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
  58. tableDescriptor.addFamily(new HColumnDescriptor("column1"));
  59. tableDescriptor.addFamily(new HColumnDescriptor("column2"));
  60. tableDescriptor.addFamily(new HColumnDescriptor("column3"));
  61. hBaseAdmin.createTable(tableDescriptor);
  62. } catch (MasterNotRunningException e) {
  63. e.printStackTrace();
  64. } catch (ZooKeeperConnectionException e) {
  65. e.printStackTrace();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. System.out.println("end create table ......");
  70. }
  71. /**
  72. * 插入数据
  73. * @param tableName
  74. */
  75. public static void insertData(String tableName) {
  76. System.out.println("start insert data ......");
  77. HTablePool pool = new HTablePool(configuration, 1000);
  78. HTable table = (HTable) pool.getTable(tableName);
  79. Put put = new Put("112233bbbcccc".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
  80. put.add("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列
  81. put.add("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列
  82. put.add("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列
  83. try {
  84. table.put(put);
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. }
  88. System.out.println("end insert data ......");
  89. }
  90. /**
  91. * 删除一张表
  92. * @param tableName
  93. */
  94. public static void dropTable(String tableName) {
  95. try {
  96. HBaseAdmin admin = new HBaseAdmin(configuration);
  97. admin.disableTable(tableName);
  98. admin.deleteTable(tableName);
  99. } catch (MasterNotRunningException e) {
  100. e.printStackTrace();
  101. } catch (ZooKeeperConnectionException e) {
  102. e.printStackTrace();
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. /**
  108. * 根据 rowkey删除一条记录
  109. * @param tablename
  110. * @param rowkey
  111. */
  112. public static void deleteRow(String tablename, String rowkey) {
  113. try {
  114. HTable table = new HTable(configuration, tablename);
  115. List list = new ArrayList();
  116. Delete d1 = new Delete(rowkey.getBytes());
  117. list.add(d1);
  118. table.delete(list);
  119. System.out.println("删除行成功!");
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. /**
  125. * 组合条件删除
  126. * @param tablename
  127. * @param rowkey
  128. */
  129. public static void deleteByCondition(String tablename, String rowkey) {
  130. //目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作
  131. }
  132. /**
  133. * 查询所有数据
  134. * @param tableName
  135. */
  136. public static void QueryAll(String tableName) {
  137. HTablePool pool = new HTablePool(configuration, 1000);
  138. HTable table = (HTable) pool.getTable(tableName);
  139. try {
  140. ResultScanner rs = table.getScanner(new Scan());
  141. for (Result r : rs) {
  142. System.out.println("获得到rowkey:" + new String(r.getRow()));
  143. for (KeyValue keyValue : r.raw()) {
  144. System.out.println("列:" + new String(keyValue.getFamily())
  145. + "====值:" + new String(keyValue.getValue()));
  146. }
  147. }
  148. } catch (IOException e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. /**
  153. * 单条件查询,根据rowkey查询唯一一条记录
  154. * @param tableName
  155. */
  156. public static void QueryByCondition1(String tableName) {
  157. HTablePool pool = new HTablePool(configuration, 1000);
  158. HTable table = (HTable) pool.getTable(tableName);
  159. try {
  160. Get scan = new Get("abcdef".getBytes());// 根据rowkey查询
  161. Result r = table.get(scan);
  162. System.out.println("获得到rowkey:" + new String(r.getRow()));
  163. for (KeyValue keyValue : r.raw()) {
  164. System.out.println("列:" + new String(keyValue.getFamily())
  165. + "====值:" + new String(keyValue.getValue()));
  166. }
  167. } catch (IOException e) {
  168. e.printStackTrace();
  169. }
  170. }
  171. /**
  172. * 单条件按查询,查询多条记录
  173. * @param tableName
  174. */
  175. public static void QueryByCondition2(String tableName) {
  176. try {
  177. HTablePool pool = new HTablePool(configuration, 1000);
  178. HTable table = (HTable) pool.getTable(tableName);
  179. Filter filter = new SingleColumnValueFilter(Bytes
  180. .toBytes("column1"), null, CompareOp.EQUAL, Bytes
  181. .toBytes("aaa")); // 当列column1的值为aaa时进行查询
  182. Scan s = new Scan();
  183. s.setFilter(filter);
  184. ResultScanner rs = table.getScanner(s);
  185. for (Result r : rs) {
  186. System.out.println("获得到rowkey:" + new String(r.getRow()));
  187. for (KeyValue keyValue : r.raw()) {
  188. System.out.println("列:" + new String(keyValue.getFamily())
  189. + "====值:" + new String(keyValue.getValue()));
  190. }
  191. }
  192. } catch (Exception e) {
  193. e.printStackTrace();
  194. }
  195. }
  196. /**
  197. * 组合条件查询
  198. * @param tableName
  199. */
  200. public static void QueryByCondition3(String tableName) {
  201. try {
  202. HTablePool pool = new HTablePool(configuration, 1000);
  203. HTable table = (HTable) pool.getTable(tableName);
  204. List<Filter> filters = new ArrayList<Filter>();
  205. Filter filter1 = new SingleColumnValueFilter(Bytes
  206. .toBytes("column1"), null, CompareOp.EQUAL, Bytes
  207. .toBytes("aaa"));
  208. filters.add(filter1);
  209. Filter filter2 = new SingleColumnValueFilter(Bytes
  210. .toBytes("column2"), null, CompareOp.EQUAL, Bytes
  211. .toBytes("bbb"));
  212. filters.add(filter2);
  213. Filter filter3 = new SingleColumnValueFilter(Bytes
  214. .toBytes("column3"), null, CompareOp.EQUAL, Bytes
  215. .toBytes("ccc"));
  216. filters.add(filter3);
  217. FilterList filterList1 = new FilterList(filters);
  218. Scan scan = new Scan();
  219. scan.setFilter(filterList1);
  220. ResultScanner rs = table.getScanner(scan);
  221. for (Result r : rs) {
  222. System.out.println("获得到rowkey:" + new String(r.getRow()));
  223. for (KeyValue keyValue : r.raw()) {
  224. System.out.println("列:" + new String(keyValue.getFamily())
  225. + "====值:" + new String(keyValue.getValue()));
  226. }
  227. }
  228. rs.close();
  229. } catch (Exception e) {
  230. e.printStackTrace();
  231. }
  232. }
  233. }

注意:可能大家没看到更新数据的操作,其实更新的操作跟添加完全一致,只不过是添加呢rowkey不存在,更新呢rowkey已经存在,并且timstamp相同的情况下,还有就是目前好像还没办法实现hbase数据的分页查询,不知道有没有人知道怎么做

 

HBase性能优化建议:

针对前面的代码,有很多不足之处,在此我就不修改上面的代码了,只是提出建议的地方,大家自己加上

1)配置

当你调用create方法时将会加载两个配置文件:hbase-default.xml and hbase-site.xml,利用的是当前的java类路径, 代码中configuration设置的这些配置将会覆盖hbase-default.xml和hbase-site.xml中相同的配置,如果两个配置文件都存在并且都设置好了相应参上面的属性下面的属性即可

 

2)关于建表

 

public void createTable(HTableDescriptor desc)

 

HTableDescriptor 代表的是表的schema, 提供的方法中比较有用的有

setMaxFileSize,指定最大的region size

setMemStoreFlushSize 指定memstore flush到HDFS上的文件大小

增加family通过 addFamily方法

 

public void addFamily(final HColumnDescriptor family)

 

HColumnDescriptor 代表的是column的schema,提供的方法比较常用的有

setTimeToLive:指定最大的TTL,单位是ms,过期数据会被自动删除。

setInMemory:指定是否放在内存中,对小表有用,可用于提高效率。默认关闭

setBloomFilter:指定是否使用BloomFilter,可提高随机查询效率。默认关闭

setCompressionType:设定数据压缩类型。默认无压缩。

setMaxVersions:指定数据最大保存的版本个数。默认为3。

 

注意的是,一般我们不去setInMemory为true,默认是关闭的

 

3)关于入库

官方建议

table.setAutoFlush(false); //数据入库之前先设置此项为false

table.setflushCommits();//入库完成后,手动刷入数据

注意:

在入库过程中,put.setWriteToWAL(true/flase);

关于这一项如果不希望大量数据在存储过程中丢失,建议设置为true,如果仅是在测试演练阶段,为了节省入库时间建议设置为false

 

4)关于获取表实例

HTablePool pool = new HTablePool(configuration, Integer.MAX_VALUE);

HTable table = (HTable) pool.getTable(tableName);

建议用表连接池的方式获取表,具体池有什么作用,我想用过数据库连接池的同学都知道,我就不再重复

不建议使用new HTable(configuration,tableName);的方式获取表

 

5)关于查询

建议每个查询语句都放入try catch语句块,并且finally中要进行关闭ResultScanner实例以及将不使用的表重新放入到HTablePool中的操作,具体做法如下

 

Java代码
  1. public static void QueryAll(String tableName) {
  2. HTablePool pool = new HTablePool(configuration, Integer.MAX_VALUE);
  3. HTable table = null;
  4. ResultScanner rs = null;
  5. try {
  6. Scan scan = new Scan();
  7. table = (HTable) pool.getTable(tableName);
  8. rs = table.getScanner(scan);
  9. for (Result r : rs) {
  10. System.out.println("获得到rowkey:" + new String(r.getRow()));
  11. for (KeyValue keyValue : r.raw()) {
  12. System.out.println("列:" + new String(keyValue.getFamily())
  13. + "====值:" + new String(keyValue.getValue()));
  14. }
  15. }
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }finally{
  19. rs.close();// 最后还得关闭
  20. pool.putTable(table); //实际应用过程中,pool获取实例的方式应该抽取为单例模式的,不应在每个方法都重新获取一次(单例明白?就是抽取到专门获取pool的逻辑类中,具体逻辑为如果pool存在着直接使用,如果不存在则new)
  21. }
  22. }
分享到:
评论

相关推荐

    HBase数据库设计.doc

    1. HBase有哪些基本的特征? 1 HBase特征: 1 2. HBase相对于关系数据库能解决的问题是什么? 2 HBase与关系数据的区别? 2 HBase与RDBMS的区别? 2 3. HBase的数据模式是怎么样的?即有哪些元素?如何存储?等 3 1...

    HBase(hbase-2.4.9-bin.tar.gz)

    HBase(hbase-2.4.9-bin.tar.gz)是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”。就像Bigtable利用了Google文件系统(File System...

    hbase-2.3.5单机一键部署工具

    注意:zookeeper3.4.13和hbase2.3.5都是采用docker-compose方式部署 原文链接:https://blog.csdn.net/m0_37814112/article/details/120915194 说明:使用外部zookeeper3.4.13之hbase2.3.5一键部署工具,支持部署、...

    java大数据作业_3HBase

    1. 请用java集合的代码描述HBase的表结构 2. 请简述HBase中数据写入最后导致Region分裂的全过程 3. 如果设计一个笔记的表,表中要求有笔记的属性和笔记的内容,怎么做 4. HBase部署时如何指定多个zookeeper 5. HBase...

    实验三:熟悉常用的HBase操作

    A.3实验三:熟悉常用的HBase操作 本实验对应第5章的内容。 A.3.1 实验目的 (1)理解HBase在Hadoop体系结构中的角色。(2)熟练使用HBase操作常用的 Shell命令。(3)熟悉HBase操作常用的 Java API。 A.3.2 实验平台 (1...

    hbase-shell批量命令执行脚本的方法

    批量执行hbase shell 命令 #!/bin/bash source /etc/profile exec $HBASE_HOME/bin/hbase shell &lt;&lt;EOF truncate 'tracker_total_apk_fact_zyt' major_compact('t_abc') disable 't_abc' drop 't_abc' create...

    HBase海量数据存储实战视频教程

    从HBase的集群搭建、HBaseshell操作、java编程、架构、原理、涉及的数据结构,并且结合陌陌海量消息存储案例来讲解实战HBase 课程亮点 1,知识体系完备,从小白到大神各阶段读者均能学有所获。 2,生动形象,化繁为...

    基于Hbase的大数据查询优化

    Hbase有着先天的优势和先天的劣势,而劣势就是其较差的数据定位能力,也就是数据查询能力。因为面向列的特点,Hbase只能单单地以rowkey为主键作查询,而无法对表进行多维查询和join操作,并且查询通常都是全表扫描,耗费...

    大数据开发之Hbase基本使用及存储设计实战教程(视频+笔记+代码)

    │ Day15[Hbase 基本使用及存储设计].pdf │ ├─02_视频 │ Day1501_Hbase的介绍及其发展.mp4 │ Day1502_Hbase中的特殊概念.mp4 │ Day1503_Hbase与MYSQL的存储比较.mp4 │ Day1504_Hbase部署环境准备.mp4 │ Day...

    HbaseTemplate 操作hbase

    java 利用 sping-data-hadoop HbaseTemplate 操作hbase find get execute 等方法 可以直接运行

    FlinkSQL实战:HBase的结合应用

    HBase作为Google发表BigTable论文的开源实现版本,是一种分布式列式存储的数据库,构建在HDFS之上的NoSQL数据库,非常适合大规模实时查询,因此HBase在实时计算领域使用非常广泛。可以实时写HBase,也可以利用...

    hbase-2.2.6-bin.tar.gz

    hbase-2.2.6-bin.tar.gz HBase是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”。就像Bigtable利用了Google文件系统(File System)所...

    Hbase权威指南(HBase: The Definitive Guide)

    如果你正在寻找一种具备可伸缩性的存储解决方案来适应几乎没有穷尽的数据的话,这本书将可以向你表明apache hbase完全能够满足你的需求。作为google bigtable架构的开源实现,hbase能够支持数以十亿计的记录数和数以...

    hbase-1.2.4-bin.tar.gz.zip

    HBase是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”。就像Bigtable利用了Google文件系统(File System)所提供的分布式数据存储...

    hive和hbase整合

    hadoop jar /usr/lib/hbase/hbase-0.90.4-cdh3u3.jar importtsv -Dimporttsv.columns=HBASE_ROW_KEY, catgyname,catgyid1,catgyname1,catgyid2,catgyname2,catgyid3,catgyname3,catgyid4,catgyname4,catgyid5,...

    hbase-1.4.9-bin.tar.gz

    hbase官方推荐稳定版1.4.9 HBase是建立在Hadoop文件系统之上的分布式面向列的数据库。它是一个开源项目,是横向扩展的。 HBase是一个数据模型,类似于谷歌的大表设计,可以提供快速随机访问海量结构化数据。它利用了...

    Hbase JAVA编程开发实验

    Hbase JAVA编程开发实验报告以及代码,1 按照“Hbase应用开发实验1.pdf”中给出的说明,完成相关相关实验及JAVA程序的编写、调试和运行,提交JAVA源程序(AccessObject.java, HBaseTester.java, User.java)及运行...

    pinpoint的hbase初始化脚本hbase-create.hbase

    搭建pinpoint需要的hbase初始化脚本hbase-create.hbase

    hbase-sdk是基于hbase-client和hbase-thrift的原生API封装的一款轻量级的HBase ORM框架

    hbase-sdk是基于hbase-client和hbase-thrift的原生API封装的一款轻量级的HBase ORM框架。 针对HBase各版本API(1.x~2.x)间的差异,在其上剥离出了一层统一的抽象。并提供了以类SQL的方式来读写HBase表中的数据。对...

    HBase3.0参考指南

    HBase3.0参考指南 This is the official reference guide for the HBase version it ships with. Herein you will find either the definitive documentation on an HBase topic as of its standing when the ...

Global site tag (gtag.js) - Google Analytics