HBase 中用 put 命令添加數(shù)據(jù)烫沙,但在 HBase 中一次只能為一個表的一行數(shù)據(jù)的一個列确丢,也就是一個單元格添加一個數(shù)據(jù),這點和關(guān)系型數(shù)據(jù)庫是不一樣的奇钞,在關(guān)系型數(shù)據(jù)庫中直接是插入整行的數(shù)據(jù),所以 HBase 直接用 shell 命令插入數(shù)據(jù)效率很低漂坏,在實際應(yīng)用中景埃,一般都是利用編程操作 HBase 的。
1. HBase 操作命令
-
表列表:
命令格式:
list
-
創(chuàng)建表:
命令格式(不指定默認(rèn)保存的版本數(shù)顶别,默認(rèn)值為 3):
create '表名','列族1','列族2'
命令格式(指定默認(rèn)保存的版本數(shù)):
create '表名',{NAME=>'列族1',VERSIONS=>保存的版本數(shù)}, {NAME=>'列族2',VERSIONS=>保存的版本數(shù)}
-
查看表描述:
命令格式:
describe '表名'
-
刪除表:
命令格式(先使該表不可用):
disable '表名'
命令格式(刪除表):
drop '表名'
-
添加數(shù)據(jù):
命令格式(列族只有單個列名):
put '表名','行號','列族','列值'
命令格式(列族有多個列名):
put '表名','行號','列族:列名','列值'
-
刪除數(shù)據(jù):
命令格式(刪除某個單元格數(shù)據(jù)):
delete '表名','行號','列族'
命令格式(刪除某行號數(shù)據(jù)):
deleteall '表名','行號'
-
修改數(shù)據(jù):
命令格式(HBase中實際上是沒有修改數(shù)據(jù)命令的):
put '表名','行號','列族','新的單元格值'
-
查看數(shù)據(jù):
命令格式(查看某個列族的數(shù)據(jù)):
get '表名','行號',{COLUMN=>'列族',VERSIONS=>查看的版本數(shù)}
命令格式(查看某行號數(shù)據(jù)):
get '表名','行號'
命令格式(查看整個表的數(shù)據(jù)):
scan '表名'
2. Java 編程來操作 HBase
操作 HBase 所用的 jar 包谷徙,使用 Maven 導(dǎo)入,引入依賴 hbase-it驯绎,pom.xml 文件依賴部分如下:
說明:我安裝的 HBase 是 1.1.0 版本的完慧,所以這里使用的依賴版本也是 1.1.0,請保持 jar 包版本和 HBase 版本一致剩失。
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-it</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
HBase 的提供的 jar 包只是對 HBase 命令操作的基本封裝屈尼,為了便于使用册着,可以在此基礎(chǔ)上做進(jìn)一步的封裝來調(diào)用。
下面是 HBase 封裝后的工具類 HBaseUtil脾歧。
package com.weizhiwen.util;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
public class HBaseUtil {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
/**
* 建表甲捏。HBase的表中會有一個系統(tǒng)默認(rèn)的屬性作為主鍵,
* 主鍵無需自行創(chuàng)建鞭执,默認(rèn)為put命令操作中表名后第一個數(shù)據(jù)摊鸡,
* 因此此處無需創(chuàng)建id列
* @param myTableName 表名
* @param columnFamily 列族數(shù)組
* @throws IOException
*/
@SuppressWarnings("all")
public static void createTable(String myTableName, String[] columnFamily) throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if(admin.tableExists(tableName)) {
System.out.println(myTableName+" is exists");
} else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String columnName : columnFamily) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnName);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("create "+myTableName+" success");
}
close();
}
/**
* 刪除指定表
* @param myTableName 表名
* @throws IOException
*/
public static void deleteTable(String myTableName) throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName); // 先使表無效
admin.deleteTable(tableName); // 在刪除表
}
close();
}
/**
* 查看已有表
* @throws IOException
*/
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor : hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
/**
* 向某一行的某一列插入數(shù)據(jù)
* @param myTableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param column 列名(如果列族下沒有子列名,此參數(shù)可為空)
* @param value 單元格值
* @throws IOException
*/
public static void insertRowData(String tableName, String rowKey, String colFamily, String column, String value) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), column.getBytes(), value.getBytes());
table.put(put);
table.close();
close();
}
/**
* 刪除數(shù)據(jù)
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
//刪除指定列族的所有數(shù)據(jù)
//delete.addFamily(colFamily.getBytes());
//刪除指定列的數(shù)據(jù)
//delete.addColumn(colFamily.getBytes(), col.getBytes());
table.delete(delete);
table.close();
close();
}
/**
* 根據(jù)行鍵查詢數(shù)據(jù)
* @param tableName 表名
* @param rowKey 行鍵
* @param colFamily 列族名
* @param col 列名
* @throws IOException
*/
public static void getRowData(String tableName,String rowKey,String colFamily,String col)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
showCell(result);
table.close();
close();
}
/**
* 格式化輸出
* @param result
*/
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}
// 建立連接
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 關(guān)閉連接
public static void close() {
try {
if(admin != null) {
admin.close();
}
if(connection != null) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
工具類的測試類 HBaseUtilTest蚕冬。
package com.weizhiwen.test;
import java.io.IOException;
import com.weizhiwen.util.HBaseUtil;
public class HBaseTest {
public static void main(String[] args) throws IOException {
// 查看 HBase 數(shù)據(jù)庫中的所有表
HBaseUtil.listTables();
// 刪除成績表(HBase 數(shù)據(jù)庫中已有數(shù)據(jù)表)
HBaseUtil.deleteTable("chengji");
// 重新創(chuàng)建成績表
HBaseUtil.createTable("chengji", new String[]{"score"});
// 插入數(shù)據(jù)
HBaseUtil.insertRowData("chengji", "zhangsan", "score", "English", "98");
HBaseUtil.insertRowData("chengji", "zhangsan", "score", "Math", "94");
HBaseUtil.insertRowData("chengji", "zhangsan", "score", "Computer", "98");
// 查看單行數(shù)據(jù)
HBaseUtil.getRowData("chengji", "zhangsan", "score", "Math");
}
}
上面 HBaseUtil 類的封裝也只是基本的封裝免猾,還可以根據(jù)實際使用 HBase 在來進(jìn)行自己需要的封裝,比如單行多列插入囤热,查詢整表等等猎提。
個人 GitHub 地址:https://github.com/weizhiwen,歡迎來訪旁蔼。