HBase Shell 操作

3.1 基本操作
1.进入 HBase 客户端命令行,在hbase-2.1.3目录下


bin/hbase shell


2.查看帮助命令


hbase(main):001:0> help



3.查看当前数据库中有哪些表


hbase(main):002:0> list


3.2 表的操作

1.创建表


hbase(main):002:0> create 'student','info'


注意:删除字符的按键BackSpace的删除顺序是反过来的,即从左往右删。
2.插入数据到表


hbase(main):003:0> put 'student','1001','info:sex','male'
hbase(main):004:0> put 'student','1001','info:age','18'
hbase(main):005:0> put 'student','1002','info:name','Janna'
hbase(main):006:0> put 'student','1002','info:sex','female'
hbase(main):007:0> put 'student','1002','info:age','20'


3.扫描查看表数据


hbase(main):008:0> scan 'student'
hbase(main):009:0> scan 'student',{STARTROW => '1001', STOPROW => '1001'} 前闭后开
hbase(main):010:0> scan 'student',{STARTROW => '1001'}


4.查看表结构


hbase(main):0011:0> describe 'student'


5.更新指定字段的数据


hbase(main):012:0> put 'student','1001','info:name','Nick'
hbase(main):013:0> put 'student','1001','info:age','100'


6.查看【指定行】或【指定列族:列】的数据


hbase(main):014:0> get 'student','1001'
hbase(main):015:0> get 'student','1002','info:name'


7.统计表数据行数


hbase(main):021:0> count 'student'


8.删除数据
删除某 rowkey 的全部数据:


hbase(main):016:0> deleteall 'student','1001'


注意:删除操作默认的时间戳是当前时间。在 HBase 中,增删改数据都是打时间戳!!!
删除某rowkey的某一列数据:


hbase(main):017:0> delete 'student','1002','info:sex'


注意:shell删除操作会将数据的所有版本都删除掉。但是在 HBase 的 API 操作中可以细粒度的控制删除哪一个版本。

9.清空表数据


hbase(main):018:0> truncate 'student'


提示:清空表的操作顺序为先 disable,然后再 truncate。

10.删除表
首先需要先让该表为 disable 状态:


hbase(main):019:0> disable 'student'


然后才能 drop 这个表:


hbase(main):020:0> drop 'student'


提示:如果直接 drop 表,会报错:ERROR: Table student is enabled. Disable it first.

11.变更表信息
设置将info列族中的数据存放3个版本:


hbase(main):022:0> alter 'student',{NAME => 'info', VERSIONS => 3}
hbase(main):022:0> get 'student','1001',{COLUMN => 'info:name', VERSIONS => 3}


Hbase API操作

1.环境准备:

创建maven项目,更改pom.xml文件配置

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.hbase</groupId>
  4. <artifactId>hbase-client</artifactId>
  5. <version>1.1.3</version>
  6. </dependency>
  7.  
  8. <dependency>
  9. <groupId>junit</groupId>
  10. <artifactId>junit</artifactId>
  11. <version>4.11</version>
  12. <scope>test</scope>
  13. </dependency>
  14.  
  15. <dependency>
  16. <groupId>org.apache.logging.log4j</groupId>
  17. <artifactId>log4j-core</artifactId>
  18. <version>2.8.2</version>
  19. </dependency>
  20.  
  21. <dependency>
  22. <groupId>org.apache.hadoop</groupId>
  23. <artifactId>hadoop-common</artifactId>
  24. <version>2.7.2</version>
  25. </dependency>
  26.  
  27. <dependency>
  28. <groupId>org.apache.hadoop</groupId>
  29. <artifactId>hadoop-hdfs</artifactId>
  30. <version>${hadoop.version}</version>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.apache.hadoop</groupId>
  35. <artifactId>hadoop-client</artifactId>
  36. <version>${hadoop.version}</version>
  37. </dependency>
  38. </dependencies>

2.API

一系列API操作集合如下:

  1. package com.gec.demo;
  2.  
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.*;
  5. import org.apache.hadoop.hbase.client.*;
  6.  
  7. import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
  8. import org.apache.hadoop.hbase.util.Bytes;
  9. import sun.java2d.pipe.OutlineTextRenderer;
  10.  
  11. import java.io.IOException;
  12.  
  13. import static com.gec.demo.utli.getTable.getHTable;
  14.  
  15. public class HbaseOperation {
  16. static Admin admin = null;
  17.  
  18. static Connection conn = null;
  19.  
  20. static Configuration conf = null;
  21.  
  22. static {
  23. // HBase配置文件
  24. conf = HBaseConfiguration.create();
  25.  
  26. // 设置zookeeper地址
  27. conf.set("hbase.zookeeper.quorum", "hadoop-001");
  28. conf.set("hbase.zookeeper.property.clientPort", "2181");
  29.  
  30. try {
  31. // 获取连接对象,执行
  32. conn = ConnectionFactory.createConnection(conf);
  33. admin = conn.getAdmin();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38.  
  39. private static void close(Connection conn, Admin admin) {
  40. if (conn != null) {
  41. try {
  42. conn.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47.  
  48. if (admin != null) {
  49. try {
  50. admin.close();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. public static boolean isTableExistNewAPI(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
  57.  
  58. // HBase配置文件
  59. Configuration conf = HBaseConfiguration.create();
  60.  
  61. // 设置zookeeper地址
  62. conf.set("hbase.zookeeper.quorum", "hadoop-001");
  63. conf.set("hbase.zookeeper.property.clientPort", "2181");
  64.  
  65. // 获取连接对象,执行
  66. Connection connection = ConnectionFactory.createConnection(conf);
  67. Admin admin = connection.getAdmin();
  68. boolean tableExists = admin.tableExists(TableName.valueOf(tableName));
  69.  
  70. // 关闭资源
  71. admin.close();
  72.  
  73. return tableExists;
  74. }
  75. // 创建表
  76. public static void createTable(String tableName, String... columnFamily) throws IOException {
  77.  
  78. if (isTableExistNewAPI(tableName)) {
  79. System.out.println("表" + tableName + "已存在!");
  80. return;
  81. }
  82.  
  83. // 创建表描述器
  84. HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
  85.  
  86. // 添加列族
  87. for (String cf : columnFamily) {
  88. // 创建列描述器
  89. HColumnDescriptor HColumnDescriptor = new HColumnDescriptor(cf);
  90. // 指定列族的版本个数,默认个数是一个
  91. // HColumnDescriptor.setMaxVersions(5);
  92. hTableDescriptor.addFamily(HColumnDescriptor);
  93. }
  94.  
  95. // 创建表操作
  96. admin.createTable(hTableDescriptor);
  97. System.out.println("表" + tableName + "创建成功!");
  98. }
  99. // 删除表
  100. public static void deleteTable(String tableName) throws IOException {
  101.  
  102. if (isTableExistNewAPI(tableName)) {
  103. // 删除表之前先使表不可用(下线)
  104. admin.disableTable(TableName.valueOf(tableName));
  105. // 执行删除操作
  106. admin.deleteTable(TableName.valueOf(tableName));
  107. System.out.println("表" + tableName + "删除成功!");
  108. } else {
  109. System.out.println("表" + tableName + "不存在!");
  110. }
  111. }
  112.  
  113. // 向表中插入数据(或修改)
  114. public static void putRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
  115.  
  116. // 创建HTable对象
  117. // 旧API
  118. // HTable hTable = new HTable(conf, TableName.valueOf(tableName));
  119. // 获取Table对象
  120. // 新API
  121. Table table = conn.getTable(TableName.valueOf(tableName));
  122.  
  123. Put put = new Put(Bytes.toBytes(rowKey));
  124. // 向Put对象中组装数据
  125. put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
  126.  
  127. // 执行向表中插入数据的操作
  128. table.put(put);
  129.  
  130. System.out.println("插入数据成功");
  131.  
  132. table.close();
  133.  
  134. // 批量插入数据提示:1、同一个RowKey下添加不同的列;2、不同的RowKey,可以将RowKey(Put)放到List集合。
  135. }
  136.  
  137. // 删除多行数据
  138. public static void deleteData(String tableName, String rowKey, String columnFamily, String column) throws IOException {
  139.  
  140. // 获取Table对象
  141. // 新API
  142. Table table = conn.getTable(TableName.valueOf(tableName));
  143.  
  144. // 创建Delete对象
  145. Delete delete = new Delete(Bytes.toBytes(rowKey));
  146.  
  147. // 向Delete对象中组装数据,如果不组装,则删除的是行键的数据(多行数据)
  148. // delete.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); // 慎用这个方法,删除某个版本(默认最新版本),保留旧的版本
  149. // delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); // 公司推荐使用这个方法,删除所有版本
  150.  
  151. // 执行删除操作
  152. table.delete(delete);
  153.  
  154. System.out.println("删除多行数据成功");
  155.  
  156. table.close();
  157. }
  158. // 获取所有数据(全表扫描)
  159. public static void scanTable(String tableName) throws IOException {
  160.  
  161. // 获取Table对象
  162. // 新API
  163. Table table = conn.getTable(TableName.valueOf(tableName));
  164.  
  165. // 构建扫描器,指定扫描的起始行和结束行,不指定的话,表示扫描全表,还可以指定其他限定
  166. Scan scan = new Scan();
  167. // scan.setStartRow(startRow);
  168. // scan.setStopRow(stopRow);
  169.  
  170. // 执行扫描全表操作
  171. ResultScanner resultScanner = table.getScanner(scan);
  172.  
  173. for (Result result : resultScanner) {
  174. Cell[] cells = result.rawCells();
  175. for (Cell cell : cells) {
  176. System.out.println("行键:" + Bytes.toString(result.getRow())
  177. + " 列族:" + Bytes.toString(CellUtil.cloneFamily(cell))
  178. + " 列:" + Bytes.toString(CellUtil.cloneQualifier(cell))
  179. + " 值:" + Bytes.toString(CellUtil.cloneValue(cell)));
  180. }
  181. }
  182.  
  183. table.close();
  184. }
  185. // 获取某一行数据
  186. public static void getRowData(String tableName, String rowKey) throws IOException {
  187.  
  188. // 获取Table对象
  189. // 新API
  190. Table table = conn.getTable(TableName.valueOf(tableName));
  191.  
  192. // 新建一个Get对象
  193. Get get = new Get(Bytes.toBytes(rowKey));
  194. // 显示所有版本
  195. // get.setMaxVersions();
  196. // 显示指定版本
  197. // get.setMaxVersions(maxVersions);
  198. // 显示指定时间戳的版本
  199. // get.setTimeStamp();
  200.  
  201. // 执行获取某一行数据的操作
  202. Result result = table.get(get);
  203. Cell[] cells = result.rawCells();
  204. for (Cell cell : cells) {
  205. System.out.println("行键:" + Bytes.toString(result.getRow())
  206. + " 列族:" + Bytes.toString(CellUtil.cloneFamily(cell))
  207. + " 列:" + Bytes.toString(CellUtil.cloneQualifier(cell))
  208. + " 值:" + Bytes.toString(CellUtil.cloneValue(cell))
  209. + " 时间戳:" + cell.getTimestamp());
  210. }
  211.  
  212. table.close();
  213. }
  214. // 获取某一行指定“列族:列”的数据
  215. public static void getRowQualifierData(String tableName, String rowKey, String columnFamily, String column) throws IOException {
  216.  
  217. // 获取Table对象
  218. // 新API
  219. Table table = conn.getTable(TableName.valueOf(tableName));
  220.  
  221. // 新建一个Get对象
  222. Get get = new Get(Bytes.toBytes(rowKey));
  223. // 指定要获取某一行的“列族:列”
  224. get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
  225.  
  226. // 执行获取某一行指定“列族:列”数据的操作
  227. Result result = table.get(get);
  228. Cell[] cells = result.rawCells();
  229. for (Cell cell : cells) {
  230. System.out.println("行键:" + Bytes.toString(result.getRow())
  231. + " 列族:" + Bytes.toString(CellUtil.cloneFamily(cell))
  232. + " 列:" + Bytes.toString(CellUtil.cloneQualifier(cell))
  233. + " 值:" + Bytes.toString(CellUtil.cloneValue(cell))
  234. + " 时间戳:" + cell.getTimestamp());
  235. }
  236.  
  237. table.close();
  238. }
  239. }

Hbase Shell命令详解+API操作的更多相关文章

  1. 【Devops】【docker】【CI/CD】关于jenkins构建成功后一步,执行的shell命令详解+jenkins容器运行宿主机shell命令的实现方法

    1.展示这段shell命令 +详解 #================================================================================= ...

  2. Linux主要shell命令详解(上)

    [摘自网络] kill -9 -1即实现用kill命令退出系统 Linux主要shell命令详解 [上篇] shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的 ...

  3. adb shell 命令详解,android

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  4. adb shell 命令详解,android, adb logcat

    http://www.miui.com/article-275-1.html http://noobjava.iteye.com/blog/1914348 adb shell 命令详解,android ...

  5. linux iostat命令详解 磁盘操作监控工具

    Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视. 它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况. ...

  6. hadoop Shell命令详解

    调用文件系统(FS)Shell命令应使用bin/hadoop fs <args>的形式.所有的的FS shell命令使用URI路径作为参数.URI路径详解点击这里. 1.cat说明:将路径 ...

  7. Hive Shell 命令详解

    Hive服务介绍 Hive默认提供的cli(shell)服务,如果需要启动其他服务,那么需要service参数来启动其他服务,比如thrift服务.metastore服务等.可以通过命令hive -- ...

  8. adb shell 命令详解(转)

    adb介绍 SDK的Tools文件夹下包含着Android模拟器操作的重要命令adb,adb的全称为(Android Debug Bridge就是调试桥的作用.通过adb我们可以在Eclipse中方面 ...

  9. adb shell 命令详解

    adb介绍 SDK的Tools文件夹下包含着Android模拟器操作的重要命令adb,adb的全称为(Android Debug Bridge就是调试桥的作用.通过adb我们可以在Eclipse中方面 ...

随机推荐

  1. Linux下的shell与make

    Linux下的shell与make 一.shell 1.1 什么是shell ● 用户与Linux的接口 ● 命令解释器 ● 支持多用户 ● 支持复杂的编程语言 ● Shell有很多种,如:csh,t ...

  2. js实现瀑布流以及加载效果

    一.瀑布流是个啥? 瀑布流,是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部. 最早采用瀑布流布局的网站是Pinteres ...

  3. 【转载】Vue项目自动转换 px 为 rem,高保真还原设计图

    前端开发中还原设计图的重要性毋庸置疑,目前来说应用最多的应该也还是使用rem.然而很多人依然还是处于刀耕火种的时代,要么自己去计算rem值,要么依靠编辑器安装插件转换. 而本文的目标就是通过一系列的配 ...

  4. 【Python】unittest-5

    #练习9: import unittest from selenium import webdriver import time class GloryRoad(unittest.TestCase): ...

  5. JavaScript 是如何工作的: 事件循环和异步编程的崛起 + 5个如何更好的使用 async/await 编码的技巧 - 学习笔记

    那么,谁会告诉 JS 引擎去执行你的程序?事实上,JS 引擎不是单独运行的 —— 它运行在一个宿主环境中,对于大多数开发者来说就是典型的浏览器和 Node.js.实际上,如今,JavaScript 被 ...

  6. 前端笔记 (1.HTML)

    近来一直在学习一些web的知识,主要是包括html,css,js和php,记录一下笔记,希望向和我一样刚学的朋友能提供帮助 这些笔记知识主要来源于菜鸟教程和w3school.我搭建了一个wampSer ...

  7. WordPress无插件实现SMTP给评论用户发送邮件提醒

    wordpress中集成PHPMalier给评论用户发送邮件提醒 首先你得去下载PHPMalier.  注:PHPMailer需PHP的socket扩展支持.如果PHPMailer连接邮箱需要ssl加 ...

  8. git get submodule after clone

    /********************************************************************************* * git get submodu ...

  9. 在django中进行MySQL入库

    在django中进行mysql 入库 需要导入 : from django.db import models   在添加主键时,需要使用:  primary_key=True id = models. ...

  10. 组合数的求法 (n<=1e8 可以过来看)

    C(n,m) =n!/(m!* (n-m)!  ); o(n) 求 1-m的逆元 o(n) 求 n的阶乘 代码实现 https://www.cnblogs.com/linyujun/p/5194189 ...