以下API均為HBase API 3.0版本。
API3.0與API2.0對比
Put、Delete、Scan類的API沒有發生太多改變
HTableDescriptor類被TableDescriptorBuilder替代,HColumnDescriptor被ColumnFamilyDescriptor替代,CellUtil類被淘汰。
Hbase的import
參考https://blog.csdn.net/qq_38899342/article/details/97540118。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.TableNotFoundException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
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.ResultScanner;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.client.coprocessor.Batch.Call;
import org.apache.hadoop.hbase.util.Bytes;
連接HBase
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
//建立連接
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.master", "master:60000");
configuration.set("hbase.zookeeper.quorum", "master");
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
創建表
public static void createTable(String tableName,String[] fields) throws IOException {
init();
TableName tablename = TableName.valueOf(tableName);
if(admin.tableExists(tablename)){
System.out.println("table is exists!");
admin.disableTable(tablename);
admin.deleteTable(tablename);//刪除原來的表
}
TableDescriptorBulider tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
for(String str : fields){
tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build());
admin.createTable(tableDescriptor.build());
添加數據Put
public static void putData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
init();
//獲取表對象
Table table = connection.getTable(TableName.valueOf(tableName));
//創建Put對象
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
table.put(put);
table.close();
close();
}
刪除操作Delete
public static void deleteData(String tableName,String rowKey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
//刪除指定列族
delete.addFamily(Bytes.toBytes(colFamily));
//刪除指定列
delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
table.delete(delete);
table.close();
close();
}
全表掃描
scan可以掃描多條記錄。
public static void scanData(String tableName)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);//獲取行的遍歷器
for (Result result:scanner){
printRecoder(result);
}
close();
}
Cell輸出
通過Result.rawCells()可以得到Cell,一個RowKey有多個單元格,一個單元格一個Cell。
public static void printRecoder(Result result)throws IOException{
for(Cell cell:result.rawCells()){
System.out.print("行健: "+new String(Bytes.toString(cell.getRowArray(),cell.getRowOffset(), cell.getRowLength())));
System.out.print("列簇: "+new String( Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(), cell.getFamilyLength()) ));
System.out.print(" 列: "+new String(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(), cell.getQualifierLength())));
System.out.print(" 值: "+new String(Bytes.toString(cell.getValueArray(),cell.getValueOffset(), cell.getValueLength())));
System.out.println("時間戳: "+cell.getTimestamp());
/*
CellUtil在HBase2.0被棄用,在HBase3.0被移除,因此它的cloneRow,cloneFamily等都被淘汰
System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
System.out.println("時間戳: "+cell.getTimestamp());
*/
}
}
指定列族的信息
get獲取一個單元格的多個版本信息
public static void getData(String tableName)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
//創建一個Get對象
Get get = new Get(Bytes.toBytes(rowKey));
//獲取數據的操作
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell:cells){
print(cell);
}
close();
}
Admin接口
Admin接口用於管理HBase數據庫的表信息,包括創建刪除表,列出表等。
返回值 | 方法 | 介紹 |
---|---|---|
void | createTable(TableDescriptor tabledescriptor) | 創建表 |
void | deleteTable(TableName tableName) | 刪除表 |
void | disableTable(TableName tableName) | 使表無效 |
boolean | tableExists(TableName tableName) | 檢查表是否存在 |
TableDescriptor | listTableDescriptors() | 列出所有表 |
TableDescriptorBuilder接口
該接口用於構建TableDescriptorBuilder類,主要方法如下:
返回值 | 方法 | 介紹 |
---|---|---|
TableDescriptor | build() | 構建TableDescriptor |
TableDescriptorBuilder | newBuilder(byte[] name) | 構建TableDescriptorBuilder |
TableDescriptorBuilder | setColumnFamily(ColumnFamilyDescriptor family) | 設置某個列族 |
TableDescriptorBuilder | removeColumnFamily(byte[] name) | 刪除某個列族 |
ColumnFamilyDescriptor接口
該接口用於構建ColumnFamilyDescriptor,其主要方法如下:
返回值 | 方法 | 介紹 |
---|---|---|
ColumnFamilyDescriptor | build() | 構建ColumnFamilyDescriptor |
ColumnFamilyDescriptorBuilder | newBuilder(byte[] name) | 構建ColumnFamilyDescriptorBuilder |