常見命令:java
[root@hadoop01 ~]# hbase shell #進入HBASE客戶端 hbase(main):001:0> help 「dml」 #獲取一組命令的提示hbase(main):001:0> help "put" 獲取一個單獨命令的提示幫助 hbase(main):001:0> exit #退出客戶端
#查看hbase中的全部表 hbase(main):001:0>list
建立表:
語法:create ‘表名’,’列簇1’,’ 列簇2’,’ 列簇3’....shell
# 例1:建立一張表,名稱爲user,該表有info和data兩個列簇 hbase(main):001:0>create 'user' ,'info','data' hbase(main):001:0>create 'user' ,{NAME=>'info'},{NAME=>'data'}
#例2:建立一張表叫作 user_info,包含兩個列簇 base_info 和 extra_info,而且分別指定這兩個列簇 的數據的版本數爲 3 和 1 hbase(main):001:0>create 'user_info',{NAME=>'base_info',VERSIONS=>3},{NAME=>'extra_info',VERSIONS=>1}
查看錶的詳細信息apache
#desc 或者 describe hbase(main):001:0>desc 'user_info' hbase(main):001:0>describe 'user_info'
插入數據
#put table ,rowkey, 列簇:列 ,value
hbase(main):001:0>put 'user','rowkey01','info:age','18'
查詢數據
#getide
#獲取 user 表中 row key 爲 rk0001 的全部信息 hbase(main):001:0>get 'user' ,'rk0001'
#獲取 user 表中 row key 爲 rk0001,info 列簇的全部信息 hbase(main):001:0>get 'user','rk0001','info'
#獲取 user 表中 row key 爲 rk0001,info 列簇的 name、age 列標示符的信息
hbase(main):001:0>get 'user','rk0001','info:name','info:age'oop
#獲取 user 表中 row key 爲 rk0001,列簇爲 info,版本號最新 5 個的信息 hbase(main):001:0>create 'USER','rk0001',{COLUMN=>'info',VERSIONS=>5}
#獲取 user 表中 row key 爲 rk0001,cell 的值爲 zhangsan 的信息 hbase(main):001:0>get 'user', 'rk0001', {FILTER => "ValueFilter(=, 'binary:zhangsan')"}
#獲取 user 表中 row key 爲 rk0001,列標示符中含有 a 的信息 hbase(main):001:0>get 'user','rk0001',{FILTER=>"(QualifierFilter(=,'substring:a'))"}
查詢數據
#scan3d
查詢 user_info 表中的全部信息 hbase(main):001:0>scan 'user_info'
#查詢 user_info 表中的指定列簇的全部信息 hbase(main):001:0>scan 'user_info',{COLUMNS=>'base_info'} hbase(main):001:0>scan 'user_info',{COLUMNS=>'base_info:name'} hbase(main):001:0>scan 'user_info',{COLUMNS=>['base_info','extra_info']}
#查詢 user_info 表中的指定列簇爲 base_info 的全部版本信息 hbase(main):001:0>scan 'user_info',{COLUMNS=>'base_info',VERSIONS=>5}
#查詢 user 表中列簇爲 info 和 data 且列標示符中含有 a 字符的信息 hbase(main):001:0>scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}
#rowkey的範圍查詢 hbase(main):001:0>scan 'user_info', {COLUMNS => 'base_info', STARTROW => 'baiyc_20150716_0003', ENDROW => 'baiyc_20150716_0006'}
#查詢 user 表中 rowkey 以 rk 字符開頭的 hbase(main):001:0>scan 'user',{FILTER=>"PrefixFilter('rk')"}
#查詢 user_info 表中指定時間戳範圍的數據 hbase(main):001:0>scan 'user_info',{TIMERANGE=>[1540882871681,1540882888540]}
刪除數據
#deletecode
#刪除記錄 hbase(main):001:0>delete 'user', 'rk0001' #刪除字段 hbase(main):001:0>delete 'user','rk0001','info:name' #刪除 user 表 rowkey 爲 rk0001,列標示符爲 info:name,timestamp 爲 1392383705316 的數據 hbase(main):001:0>delete 'user','rk0001','info:name',1392383705316
修改表結構
#alterserver
#添加兩個列簇 f2 和 f3 hbase(main):001:0>alter 'user_info',NAME=>'f2' hbase(main):001:0>alter 'user_info',NAME=>'f2'
#刪除一個列簇 f1 hbase(main):001:0>alter 'user_info',NAME=>'f1',METHOD=>'delete'
#將 user_info 表的 base_info 列簇版本號改成 5 hbase(main):001:0>alter 'user_info',NAME=>'base_info',VERSIONS=>5
清空表
#truncate對象
#清空 user 表中的數據 hbase(main):001:0>truncate 'user_info'
停用表/啓用表blog
#disable 和 enable hbase(main):001:0>disable 'user' hbase(main):001:0>enable 'user'
刪除表
#drop
#刪除一張表 hbase(main):001:0>disable 'user #須要先停用這張表,在刪除 hbase(main):001:0>drop 'user'
這裏實現基本的增刪改查:
pom依賴
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.2.6</version> </dependency>
代碼實現:
package com.zy.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Test; import java.io.IOException; public class HbaseTest { // 聲明靜態配置 static Configuration conf = null; private static final String ZK_CONNECT_STR = "hadoop01:2181,hadoop02:2181,hadoop03:2181"; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", ZK_CONNECT_STR); } /** * 1.建立表 */ @Test public void creatTable() { //表名 String tableName = "stu_info"; //列簇名 String[] cf = {"base_info", "extra_info"}; try { //建立HBaseAdmin對象 HBaseAdmin admin = new HBaseAdmin(conf); //建立表的描述信息對象 HTableDescriptor des = new HTableDescriptor(tableName); for (int i = 0; i < cf.length; i++) { des.addFamily(new HColumnDescriptor(cf[i])); } if (admin.tableExists(tableName)) { System.out.println("table Exists!"); System.exit(0); } else { admin.createTable(des); System.out.println("create table Success!"); } } catch (IOException e) { e.printStackTrace(); } } /** * 爲表添加數據 */ @Test public void addData() { //表名 String tableName = "user_info"; //行鍵 String rowkey = "rk0011"; //列簇 String cf = "base_info"; Put put = new Put(rowkey.getBytes()); try { //建立操做hbase表的對象 HTable table = new HTable(conf, tableName.getBytes()); //列簇 列 值 put.addColumn(cf.getBytes(), "name".getBytes(), "zs".getBytes()); table.put(put); } catch (IOException e) { e.printStackTrace(); } } /** * 根據 rwokey 查詢 */ @Test public void getResult() { //表名 String tableName = "user_info"; //行鍵 String rowKey = "rk0001"; Get get = new Get(Bytes.toBytes(rowKey)); try { //建立操做hbase表的對象 HTable table = new HTable(conf, tableName.getBytes()); Result result = table.get(get); for (KeyValue kv : result.list()) { System.out.println("rowkey:" + kv.getRow()); //行鍵 System.out.println("family" + kv.getFamily()); //列簇 System.out.println("qualifier" + kv.getQualifier()); //列 System.out.println("value:" + new String(kv.getValue())); //值 System.out.println("Timestamp" + kv.getTimestamp()); //時間戳 } } catch (IOException e) { e.printStackTrace(); } } /** * 遍歷查詢 hbase 表 */ @Test public void getResultScann() { String tableName = "user_info"; Scan scan = new Scan(); try { HTable table = new HTable(conf, tableName.getBytes()); ResultScanner scanner = table.getScanner(scan); for (Result rs : scanner) { for (KeyValue kv : rs.list()) { System.out.println("rowkey:" + kv.getRow()); //行鍵 System.out.println("family" + kv.getFamily()); //列簇 System.out.println("qualifier" + kv.getQualifier()); //列 System.out.println("value:" + new String(kv.getValue())); //值 System.out.println("Timestamp" + kv.getTimestamp()); //時間戳 } } } catch (IOException e) { e.printStackTrace(); } } /** * 遍歷查詢 hbase 表 指定rowkey的查詢範圍 */ @Test public void getResultScannRange() { String tableName = "user_info"; Scan scan = new Scan(); scan.setStartRow("rk001".getBytes()); scan.setStopRow("rk005".getBytes()); try { HTable table = new HTable(conf, tableName.getBytes()); ResultScanner scanner = table.getScanner(scan); for (Result rs : scanner) { for (KeyValue kv : rs.list()) { System.out.println("rowkey:" + kv.getRow()); //行鍵 System.out.println("family" + kv.getFamily()); //列簇 System.out.println("qualifier" + kv.getQualifier()); //列 System.out.println("value:" + new String(kv.getValue())); //值 System.out.println("Timestamp" + kv.getTimestamp()); //時間戳 } } } catch (IOException e) { e.printStackTrace(); } } /** * 查詢表中的某一列 */ @Test public void getResultByColumn() { String tableName = "user_info"; //表 String rowkey = "rk0001"; //行鍵 String cf = "base_info"; //列簇 String column = "name"; //列 try { HTable table = new HTable(conf, tableName.getBytes()); Get get = new Get(rowkey.getBytes()); get.addColumn(cf.getBytes(), column.getBytes()); Result result = table.get(get); for (KeyValue kv : result.list()) { System.out.println("rowkey:" + kv.getRow()); //行鍵 System.out.println("family" + kv.getFamily()); //列簇 System.out.println("qualifier" + kv.getQualifier()); //列 System.out.println("value:" + new String(kv.getValue())); //值 System.out.println("Timestamp" + kv.getTimestamp()); //時間戳 } } catch (IOException e) { e.printStackTrace(); } } /** * 更新表中的某一列,由於在hbase中能夠以時間戳定義多個版本的值 * 全部,更新操做就是put操做 */ @Test public void updateTable() { String tableName = "user_info"; //表 String rowkey = "rk0001"; //行鍵 String cf = "base_info"; //列簇 String column = "name"; //列 String value = "ll"; //值 try { HTable table = new HTable(conf, Bytes.toBytes(tableName)); Put put = new Put(rowkey.getBytes()); put.addColumn(cf.getBytes(), column.getBytes(), value.getBytes()); table.put(put); } catch (IOException e) { e.printStackTrace(); } } /** * 查詢某列數據的多個版本 */ @Test public void getResultByVersion() { String tableName = "user_info"; //表 String rowkey = "rk0001"; //行鍵 String cf = "base_info"; //列簇 String column = "name"; //列 int version = 5; //hbase的列的版本 try { HTable table = new HTable(conf, Bytes.toBytes(tableName)); Get get = new Get(rowkey.getBytes()); get.addColumn(cf.getBytes(), column.getBytes()); get.setMaxVersions(version); Result result = table.get(get); if (result.list() != null) { for (KeyValue kv : result.list()) { System.out.println("rowkey:" + kv.getRow()); //行鍵 System.out.println("family" + kv.getFamily()); //列簇 System.out.println("qualifier" + kv.getQualifier()); //列 System.out.println("value:" + new String(kv.getValue())); //值 System.out.println("Timestamp" + kv.getTimestamp()); //時間戳 } } } catch (IOException e) { e.printStackTrace(); } } /** * 刪除指定的列 */ @Test public void deleteColumn() { String tableName = "user_info"; //表 String rowkey = "rk0001"; //行鍵 String cf = "base_info"; //列簇 String column = "name"; //列 try { HTable table = new HTable(conf, Bytes.toBytes(tableName)); Delete delete = new Delete(rowkey.getBytes()); delete.addColumn(cf.getBytes(), column.getBytes()); table.delete(delete); System.out.println(cf + ":" + column + "is deleted!"); } catch (IOException e) { e.printStackTrace(); } } /** * 刪除指定rowKey的全部列 */ @Test public void deleteAllColumn() { String tableName = "user_info"; //表 String rowkey = "rk0001"; //行鍵 try { HTable table = new HTable(conf, Bytes.toBytes(tableName)); Delete deleteAll = new Delete(rowkey.getBytes()); table.delete(deleteAll); System.out.println("all columns are deleted!"); } catch (IOException e) { e.printStackTrace(); } } /* * 刪除表 */ @Test public void deleteTable(){ String tableName = "user_info"; //表 try { HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable(tableName.getBytes()); admin.deleteTable(tableName.getBytes()); System.out.println(tableName + "is deleted!"); } catch (IOException e) { e.printStackTrace(); } } }