HBase 官方文档 0.97 http://abloz.com/hbase/book.html

HBase基本操作封装类(以课堂爬虫为例)

  1. package cn.crxy.spider.utils;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.apache.hadoop.conf.Configuration;
  8. import org.apache.hadoop.hbase.HColumnDescriptor;
  9. import org.apache.hadoop.hbase.HTableDescriptor;
  10. import org.apache.hadoop.hbase.KeyValue;
  11. import org.apache.hadoop.hbase.client.Delete;
  12. import org.apache.hadoop.hbase.client.HBaseAdmin;
  13. import org.apache.hadoop.hbase.client.HTable;
  14. import org.apache.hadoop.hbase.client.HTableInterface;
  15. import org.apache.hadoop.hbase.client.HTablePool;
  16. import org.apache.hadoop.hbase.client.Put;
  17. import org.apache.hadoop.hbase.client.Result;
  18. import org.apache.hadoop.hbase.client.ResultScanner;
  19. import org.apache.hadoop.hbase.client.Scan;
  20. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
  21. import org.apache.hadoop.hbase.filter.RegexStringComparator;
  22. import org.apache.hadoop.hbase.filter.RowFilter;
  23. import org.apache.hadoop.hbase.util.Bytes;
  24.  
  25. public class HbaseUtils {
  26.  
  27. /**
  28. * HBASE 表名称
  29. */
  30. public static final String TABLE_NAME = "spider";
  31. /**
  32. * 列簇1 商品信息
  33. */
  34. public static final String COLUMNFAMILY_1 = "goodsinfo";
  35. /**
  36. * 列簇1中的列
  37. */
  38. public static final String COLUMNFAMILY_1_DATA_URL = "data_url";
  39. public static final String COLUMNFAMILY_1_PIC_URL = "pic_url";
  40. public static final String COLUMNFAMILY_1_TITLE = "title";
  41. public static final String COLUMNFAMILY_1_PRICE = "price";
  42. /**
  43. * 列簇2 商品规格
  44. */
  45. public static final String COLUMNFAMILY_2 = "spec";
  46. public static final String COLUMNFAMILY_2_PARAM = "param";
  47.  
  48. HBaseAdmin admin = null;
  49. Configuration conf = null;
  50.  
  51. /**
  52. * 构造函数加载配置
  53. */
  54. public HbaseUtils() {
  55. conf = new Configuration();
  56. conf.set("hbase.zookeeper.quorum", "192.168.1.177:2181");
  57. conf.set("hbase.rootdir", "hdfs://192.168.1.177:9000/hbase");
  58. try {
  59. admin = new HBaseAdmin(conf);
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64.  
  65. public static void main(String[] args) throws Exception {
  66. HbaseUtils hbase = new HbaseUtils();
  67. // 创建一张表
  68. // hbase.createTable("stu","cf");
  69. // //查询所有表名
  70. // hbase.getALLTable();
  71. // //往表中添加一条记录
  72. // hbase.addOneRecord("stu","key1","cf","name","zhangsan");
  73. // hbase.addOneRecord("stu","key1","cf","age","24");
  74. // //查询一条记录
  75. // hbase.getKey("stu","key1");
  76. // //获取表的所有数据
  77. // hbase.getALLData("stu");
  78. // //删除一条记录
  79. // hbase.deleteOneRecord("stu","key1");
  80. // //删除表
  81. // hbase.deleteTable("stu");
  82. // scan过滤器的使用
  83. // hbase.getScanData("stu","cf","age");
  84. // rowFilter的使用
  85. // 84138413_20130313145955
  86. }
  87.  
  88. /**
  89. * rowFilter的使用
  90. *
  91. * @param tableName
  92. * @param reg
  93. * @throws Exception
  94. */
  95. public void getRowFilter(String tableName, String reg) throws Exception {
  96. HTable hTable = new HTable(conf, tableName);
  97. Scan scan = new Scan();
  98. // Filter
  99. RowFilter rowFilter = new RowFilter(CompareOp.NOT_EQUAL,
  100. new RegexStringComparator(reg));
  101. scan.setFilter(rowFilter);
  102. ResultScanner scanner = hTable.getScanner(scan);
  103. for (Result result : scanner) {
  104. System.out.println(new String(result.getRow()));
  105. }
  106. }
  107.  
  108. public void getScanData(String tableName, String family, String qualifier)
  109. throws Exception {
  110. HTable hTable = new HTable(conf, tableName);
  111. Scan scan = new Scan();
  112. scan.addColumn(family.getBytes(), qualifier.getBytes());
  113. ResultScanner scanner = hTable.getScanner(scan);
  114. for (Result result : scanner) {
  115. if (result.raw().length == 0) {
  116. System.out.println(tableName + " 表数据为空!");
  117. } else {
  118. for (KeyValue kv : result.raw()) {
  119. System.out.println(new String(kv.getKey()) + "\t"
  120. + new String(kv.getValue()));
  121. }
  122. }
  123. }
  124. }
  125.  
  126. private void deleteTable(String tableName) {
  127. try {
  128. if (admin.tableExists(tableName)) {
  129. admin.disableTable(tableName);
  130. admin.deleteTable(tableName);
  131. System.out.println(tableName + "表删除成功!");
  132. }
  133. } catch (IOException e) {
  134. e.printStackTrace();
  135. System.out.println(tableName + "表删除失败!");
  136. }
  137.  
  138. }
  139.  
  140. /**
  141. * 删除一条记录
  142. *
  143. * @param tableName
  144. * @param rowKey
  145. */
  146. public void deleteOneRecord(String tableName, String rowKey) {
  147. HTablePool hTablePool = new HTablePool(conf, 1000);
  148. HTableInterface table = hTablePool.getTable(tableName);
  149. Delete delete = new Delete(rowKey.getBytes());
  150. try {
  151. table.delete(delete);
  152. System.out.println(rowKey + "记录删除成功!");
  153. } catch (IOException e) {
  154. e.printStackTrace();
  155. System.out.println(rowKey + "记录删除失败!");
  156. }
  157. }
  158.  
  159. /**
  160. * 获取表的所有数据
  161. *
  162. * @param tableName
  163. */
  164. public void getALLData(String tableName) {
  165. try {
  166. HTable hTable = new HTable(conf, tableName);
  167. Scan scan = new Scan();
  168. ResultScanner scanner = hTable.getScanner(scan);
  169. for (Result result : scanner) {
  170. if (result.raw().length == 0) {
  171. System.out.println(tableName + " 表数据为空!");
  172. } else {
  173. for (KeyValue kv : result.raw()) {
  174. System.out.println(new String(kv.getKey()) + "\t"
  175. + new String(kv.getValue()));
  176. }
  177. }
  178. }
  179. } catch (IOException e) {
  180. e.printStackTrace();
  181. }
  182.  
  183. }
  184.  
  185. // 读取一条记录
  186. /*
  187. * @SuppressWarnings({ "deprecation", "resource" }) public Article
  188. * get(String tableName, String row) { HTablePool hTablePool = new
  189. * HTablePool(conf, 1000); HTableInterface table =
  190. * hTablePool.getTable(tableName); Get get = new Get(row.getBytes());
  191. * Article article = null; try {
  192. *
  193. * Result result = table.get(get); KeyValue[] raw = result.raw(); if
  194. * (raw.length == 4) { article = new Article(); article.setId(row);
  195. * article.setTitle(new String(raw[3].getValue())); article.setAuthor(new
  196. * String(raw[0].getValue())); article.setContent(new
  197. * String(raw[1].getValue())); article.setDescribe(new
  198. * String(raw[2].getValue())); } } catch (IOException e) {
  199. * e.printStackTrace(); } return article; }
  200. */
  201.  
  202. // 添加一条记录
  203. public void put(String tableName, String row, String columnFamily,
  204. String column, String data) throws IOException {
  205. HTablePool hTablePool = new HTablePool(conf, 1000);
  206. HTableInterface table = hTablePool.getTable(tableName);
  207. Put p1 = new Put(Bytes.toBytes(row));
  208. p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
  209. Bytes.toBytes(data));
  210. table.put(p1);
  211. System.out.println("put'" + row + "'," + columnFamily + ":" + column
  212. + "','" + data + "'");
  213. }
  214.  
  215. /**
  216. * 查询所有表名
  217. *
  218. * @return
  219. * @throws Exception
  220. */
  221. public List<String> getALLTable() throws Exception {
  222. ArrayList<String> tables = new ArrayList<String>();
  223. if (admin != null) {
  224. HTableDescriptor[] listTables = admin.listTables();
  225. if (listTables.length > 0) {
  226. for (HTableDescriptor tableDesc : listTables) {
  227. tables.add(tableDesc.getNameAsString());
  228. System.out.println(tableDesc.getNameAsString());
  229. }
  230. }
  231. }
  232. return tables;
  233. }
  234.  
  235. /**
  236. * 创建一张表
  237. *
  238. * @param tableName
  239. * @param column
  240. * @throws Exception
  241. */
  242. public void createTable(String tableName, String column) throws Exception {
  243. if (admin.tableExists(tableName)) {
  244. System.out.println(tableName + "表已经存在!");
  245. } else {
  246. HTableDescriptor tableDesc = new HTableDescriptor(tableName);
  247. tableDesc.addFamily(new HColumnDescriptor(column.getBytes()));
  248. admin.createTable(tableDesc);
  249. System.out.println(tableName + "表创建成功!");
  250. }
  251. }
  252. }

hBase官方文档以及HBase基础操作封装类的更多相关文章

  1. hbase官方文档(转)

    FROM:http://www.just4e.com/hbase.html Apache HBase™ 参考指南  HBase 官方文档中文版 Copyright © 2012 Apache Soft ...

  2. HBase 官方文档

    HBase 官方文档 Copyright © 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision ...

  3. HBase官方文档

    HBase官方文档 目录 序 1. 入门 1.1. 介绍 1.2. 快速开始 2. Apache HBase (TM)配置 2.1. 基础条件 2.2. HBase 运行模式: 独立和分布式 2.3. ...

  4. HBase 官方文档0.90.4

    HBase 官方文档0.90.4 Copyright © 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Rev ...

  5. HBase官方文档 之 Region的相关知识

    HBase是以Region为最小的存储和负载单元(这里可不是HDFS的存储单元),因此Region的负载管理,关系到了数据读写的性能.先抛开Region如何切分不说,看看Region是如何分配到各个R ...

  6. HBase 官方文档中文版

    地址链接: http://abloz.com/hbase/book.html 里面包含基本的API和使用说明

  7. lavarel5.2官方文档阅读——架构基础

    <目录> 1.请求的生命周期 2.应用的架构 3.服务提供者 4.服务容器 5.Facades外立面(从这节起,看中文版的:https://phphub.org/topics/1783) ...

  8. gRPC官方文档(异步基础: C++)

    文章来自gRPC 官方文档中文版 异步基础: C++ 本教程介绍如何使用 C++ 的 gRPC 异步/非阻塞 API 去实现简单的服务器和客户端.假设你已经熟悉实现同步 gRPC 代码,如gRPC 基 ...

  9. 常用SQL_官方文档使用

    SQL语句基础理论 SQL是操作和检索关系型数据库的标准语言,标准SQL语句可用于操作关系型数据库. 5大主要类型: ①DQL(Data Query Language,数据查询语言)语句,主要由于se ...

随机推荐

  1. AP_建立银行信息总行、分行、账户(设定)

    2014-06-04 Created By BaoXinjian

  2. Axure多人协作

    这几天搞<材料採购系统>需求.我们组须要分模块画模型图,可是假设每一个人各自画各自的,最后整合,这样就太麻烦了.小左说Axure能够实现多人协作,于是我就研究了一下.我们组已经在用了.以下 ...

  3. GO1.6语言学习笔记3-工具篇(SublimeText 3+GoSublime组合)

    选择SublimeText作为开发工具的原因,最最主要的是它够轻巧,搭配GO开发才能有飞一般的感觉.当然作为开发工具之一,Sublime组合工具也提供足够强大的功能. 自动化提示代码 保存的时候自动格 ...

  4. 页面日期选择控件--jquery ui datepicker 插件

    日期选择插件Datepicker是一个配置灵活的插件,我们可以自定义其展示方式,包括日期格式.语言.限制选择日期范围.添加相关按钮以及其它导航等.官方地址:http://docs.jquery.com ...

  5. mod_fastcgi和mod_fcgid的区别

    mod_fcgid是一个跟mod_fastcgi二进制兼容的Apache module. 原 来的mod_fastcgi因为实现方式的限制,所以可能会创建了很多不必要的进程,而实际上只需要更少的进程就 ...

  6. Java的spi介绍和简单应用

    1.什么是java的spi SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制. 目前有不少框架用它来做服务的扩展发现, 简单来说,它就是一 ...

  7. Spring Cloud 通过代码自定义配置Ribbon

    我们还是先从官网文档开始学习,如下图所示,我们可以搞一个测试配置类,来验证是否真的可以通过代码来自定义配置Ribbon,但文档明确给出了警告:即这个测试配置类不能放在@ComponentScan所扫描 ...

  8. JBoss DataGrid的集群部署与訪问

    集群部署 JDG的缓存模式包含本地(Local)模式和集群(Clustered)模式.本项目採用多节点的Clustered模式部署.数据在多个节点的子集间进行复制.而不是同步拷贝到全部的节点. 使用子 ...

  9. ISE联合modelsim功能仿真和综合后仿真

    1.代码输入 (1).新建一个ISE工程,名字为count4. (2).新建一个verilog文件 (3).选择verilog module 输入file name为count4,单击next默认知道 ...

  10. [Linux] Ubuntu修改时区

    sudo apt-get install sysv-rc-confsudo dpkg-reconfigure tzdata