hbase的基本操作
阿新 • • 發佈:2019-01-15
version 分享圖片 import unit tac https exist fix 詳細 1. shell操作
#put table ,rowkey, 列簇:列 ,value
hbase(main):001:0>put ‘user‘,‘rowkey01‘,‘info:age‘,‘18‘
查詢數據
#get
常見命令:
[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’....
# 例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}
查看表的詳細信息
#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‘
查詢數據
#get
#獲取 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‘
#獲取 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‘))"}
查詢數據
#scan
查詢 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]}
刪除數據
#delete
#刪除記錄
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
修改表結構
#alter
#添加兩個列簇 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‘
停用表/啟用表
#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‘
2. API操作(javaAPI)
這裏實現基本的增刪改查:
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();
}
}
}
詳細的源代碼:http://down.51cto.com/data/2457979
hbase的基本操作