IDSW集羣中Hbase的訪問
環境
項 | 內容 |
---|---|
版本 | hbase-1.4.9 |
hadoop版本 | hadoop2.6.5 |
安裝目錄 | /data/hbase-1.4.9 |
機器 | 10.111.25.140(主) 10.111.25.138 10.111.25.139 |
zookeeper | 使用hbase自帶的zk |
訪問
在10.111.25.140中
hbase shell
注:hbase shell中用“Ctrl+Backspace”進行刪除
遠程連接
pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.4.9</version>
</dependency>
參數 | 內容 |
---|---|
集羣ip | 10.111.25.140(主) 10.111.25.138 10.111.25.139 |
hbase.zookeeper.quorum | idsw-dev-03(或10.111.25.140) |
hbase.zookeeper.property.clientPort | 2181(默認) |
訪問代碼:
package Utils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HbaseTest {
private HBaseAdmin admin = null;
// 定義配置對象HBaseConfiguration
private static Configuration configuration;
public HbaseTest() throws Exception {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","10.111.25.140"); //hbase 服務地址
configuration.set("hbase.zookeeper.property.clientPort","2181"); //端口號
admin = new HBaseAdmin(configuration);
}
// Hbase獲取所有的表信息
public List getAllTables() {
List<String> tables = null;
if (admin != null) {
try {
HTableDescriptor[] allTable = admin.listTables();
if (allTable.length > 0)
tables = new ArrayList<String>();
for (HTableDescriptor hTableDescriptor : allTable) {
tables.add(hTableDescriptor.getNameAsString());
System.out.println(hTableDescriptor.getNameAsString());
}
}catch (IOException e) {
e.printStackTrace();
}
}
return tables;
}
public static void main(String[] args) throws Exception {
HbaseTest hbaseTest = new HbaseTest();
hbaseTest.getAllTables();
}
}
Hbase常用命令
名稱 | 命令表達式 |
---|---|
查看hbase狀態 | status |
創建表 | create '表名','列族名1','列族名2','列族名N' |
查看所有表 | list |
描述表 | describe '表名' |
判斷表存在 | exists '表名' |
判斷是否禁用啓用表 | is_enabled '表名' is_disabled '表名' |
添加記錄 | put '表名','rowkey','列族:列','值' |
查看記錄rowkey下的所有數據 | get '表名','rowkey' |
查看所有記錄 | scan '表名' |
查看錶中的記錄總數 | count '表名' |
獲取某個列族 | get '表名','rowkey','列族:列' |
獲取某個列族的某個列 | get '表名','rowkey','列族:列' |
刪除記錄 | delete '表名','行名','列族:列' |
刪除整行 | deleteall '表名','rowkey' |
刪除一張表 | 先要屏蔽該表,才能對該表進行刪除 第一步 disable '表名',第二步 drop '表名' |
清空表 | truncate '表名' |
查看某個表某個列中所有數據 | scan '表名',{COLUMNS=>'列族名:列名'} |
更新記錄 | 就是覆蓋,hbase沒有修改,都是追加 |
表操作的命令
- list
- create
- describe
- enable
- disable
- is_disabled
- is_enabled
- drop
表數據相關操作的命令
命令 | 說明 | 實例 |
---|---|---|
scan | 就相當於select | scan 'test' |
count | 計數有幾行(row)一共 | count 'test' |
put | 添加 | put 'test','row1','info:age','30' |
get | 取出某一列的數據 | get 'test','0001','info:name' |
delete | 刪除某列 | delete 'test','0001','info:age' |
truncate | 格式化某個表 默認執行下述操作 disable 'test' drop 'test' create 'test' |
truncate 'test' |
alter | 修改 | alter 'xingoo:test_v',NAME=>'v',VERSIONS=>5 |
create_namespace | 創建命名空間 | create_namespace 'xingoo' |
版本相關操作命令
修改版本數
alter 'xingoo:test_v',NAME=>'v',VERSIONS=>5
describe 'xingoo:test_v'
寫入多版本
put 'xingoo:test_v','1','v:c1','value1'
put 'xingoo:test_v','1','v:c1','value2'
put 'xingoo:test_v','1','v:c1','value3'
put 'xingoo:test_v','1','v:c1','value4'
put 'xingoo:test_v','1','v:c1','value5'
put 'xingoo:test_v','1','v:c1','value6'
put 'xingoo:test_v','1','v:c1','value7'
讀取多版本
get 'xingoo:test_v','1',{COLUMN => 'v:c1',VERSIONS=>5}
另外,還可以把版本字段當做一個時間字段來進行範圍查詢,如:
get 't1', 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}