Hbase API 類和數據模型之間的對應關係
HBaseAdmin
HBaseAdmin提供了一個接口來管理 HBase 數據庫的表信息。它提供的方法包括:創建表,刪 除表,列出表項,使表有效或無效,以及添加或刪除表列族成員等。
HBaseConfiguration
HBaseConfiguration的作用:對 HBase 進行配置。
HTableDescriptor
HTableDescriptor作用:包含了表的名字極其對應表的列族。
HColumnDescriptor
HColumnDescriptor作用:維護着關於列族的信息,例如版本號,壓縮設置等。它通常在創建表或者爲表添 加列族的時候使用。列族被創建後不能直接修改,只能通過刪除然後重新創建的方式。列族被刪除的時候,列族裏面的數據也會同時被刪除。
其他的可以去參考官網API文檔
Java代碼示例
package hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HbaseTest {
private HBaseAdmin admin;
HConnection connection;
/**
* 執行Test之前執行的代碼塊
*/
@Before
public void init() {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
try {
/**
* 創建admin對象,和HMaster建立連接,執行創建,刪除,修改表的操作
*
*/
admin = new HBaseAdmin(conf);
//創建zookeeper連接
connection = HConnectionManager.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void createTable() {
try {
//創建表的描述對象
HTableDescriptor tableDescriptor = new HTableDescriptor("student");
//增加列簇
HColumnDescriptor columnDescriptor = new HColumnDescriptor("info");
//設置列簇的版本數量
columnDescriptor.setMaxVersions(5);
tableDescriptor.addFamily(columnDescriptor);
//判斷表是否存在
if (admin.tableExists("student")) {
System.out.println("表已存在");
return;
}
//執行建表操作
admin.createTable(tableDescriptor);
//判斷表是否存在
if (admin.tableExists("student")) {
System.out.println("建表成功");
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void deleteTable() {
try {
//獲取表的描述信息
HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf("student"));
HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
for (HColumnDescriptor columnFamily : columnFamilies) {
//獲取列簇的名稱
//Bytes hbase提供的把字節數組轉換成具體類型的工具
String name = Bytes.toString(columnFamily.getName());
System.out.println("列簇名:" + name);
int maxVersions = columnFamily.getMaxVersions();
System.out.println("版本數:" + maxVersions);
}
/**
* 刪除表之前先進行disable
*
*/
admin.disableTable("student");
//刪除表
admin.deleteTable("student");
if (!admin.tableExists("student")) {
System.out.println("表以刪除");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 插入一條數據
*/
@Test
public void insert() {
HTableInterface table = null;
try {
//獲取表對象
table = connection.getTable("student");
//創建put對象指向rowkey
Put put = new Put("001".getBytes());
put.add("info".getBytes(), "name".getBytes(), "張三".getBytes());
put.add("info".getBytes(), "age".getBytes(), Bytes.toBytes(23));
put.add("info".getBytes(), "clazz".getBytes(), "文科一班".getBytes());
put.add("info".getBytes(), "clazz".getBytes(), "文科一班".getBytes());
put.add("info".getBytes(), "gender".getBytes(), "男".getBytes());
//插入一行數據數據
table.put(put);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 一次插入多行數據
*/
@Test
public void insertAll() {
HTableInterface table = null;
try {
table = connection.getTable("student");
//加載磁盤上的數據
ArrayList<Student> students = DataUtil.load("E:\\bigdata\\bigdata\\data\\students.txt", Student.class);
ArrayList<Put> puts = new ArrayList<Put>();
for (Student student : students) {
byte[] rowkey = student.getId().getBytes();
Put put = new Put(rowkey);
put.add("info".getBytes(), "age".getBytes(), Bytes.toBytes(student.getAge()));
put.add("info".getBytes(), "name".getBytes(), Bytes.toBytes(student.getName()));
put.add("info".getBytes(), "clazz".getBytes(), Bytes.toBytes(student.getClazz()));
put.add("info".getBytes(), "gender".getBytes(), Bytes.toBytes(student.getGender()));
puts.add(put);
}
//插入多行數據
table.put(puts);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void query() {
HTableInterface table = null;
try {
table = connection.getTable("student");
Get get = new Get("1500100007".getBytes());
//執行查詢返回結果
Result result = table.get(get);
//獲取所有列
List<Cell> cells = result.listCells();
//遍歷每一個單元格
for (Cell cell : cells) {
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
if ("age".equals(qualifier)) {
Integer value = Bytes.toInt(CellUtil.cloneValue(cell));
System.out.println(qualifier + ":" + value);
} else {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(qualifier + ":" + value);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void scanner() {
HTableInterface table = null;
try {
table = connection.getTable("student");
//創建掃描器對象
Scan scan = new Scan();
//指定列簇掃描
scan.addFamily("info".getBytes());
//執行送秒操作,返回多行結果
ResultScanner results = table.getScanner(scan);
Result line;
//迭代結果集合
while ((line = results.next()) != null) {
//獲取這一行的所有列
List<Cell> cells = line.listCells();
//遍歷每一個單元格
for (Cell cell : cells) {
//獲取rowkey
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
System.out.print(rowkey+"\t");
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
if ("age".equals(qualifier)) {
Integer value = Bytes.toInt(CellUtil.cloneValue(cell));
System.out.print(qualifier + ":" + value + "\t");
} else {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.print(qualifier + ":" + value + "\t");
}
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
/**
* 查詢文科班的所有學生
*
*/
public void queryWenke(){
HTableInterface table = null;
try {
table = connection.getTable("student");
//創建掃描器對象
Scan scan = new Scan();
//指定列簇掃描
scan.addFamily("info".getBytes());
//增加過濾器,過濾文科班的學生
RegexStringComparator regexStringComparator = new RegexStringComparator("文科");
SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter("info".getBytes(), "clazz".getBytes(), CompareFilter.CompareOp.EQUAL, regexStringComparator);
SubstringComparator comp = new SubstringComparator("男");
SingleColumnValueFilter columnValueFilter1 = new SingleColumnValueFilter("info".getBytes(), "gender".getBytes(), CompareFilter.CompareOp.EQUAL, comp);
//過濾器集合
FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);
//前綴過濾
BinaryPrefixComparator prefixComparator = new BinaryPrefixComparator(Bytes.toBytes("呂"));
SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"),CompareFilter.CompareOp.EQUAL, prefixComparator);
//過濾文科班的學生
fl.addFilter(columnValueFilter);
//過濾性別爲男的學生
fl.addFilter(columnValueFilter1);
//增加前綴過濾
fl.addFilter(filter);
scan.setFilter(fl);
//執行送秒操作,返回多行結果
ResultScanner results = table.getScanner(scan);
Result line;
//迭代結果集合
while ((line = results.next()) != null) {
//獲取這一行的所有列
List<Cell> cells = line.listCells();
//遍歷每一個單元格
for (Cell cell : cells) {
//獲取rowkey
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
System.out.print(rowkey+"\t");
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
if ("age".equals(qualifier)) {
Integer value = Bytes.toInt(CellUtil.cloneValue(cell));
System.out.print(qualifier + ":" + value + "\t");
} else {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.print(qualifier + ":" + value + "\t");
}
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (table != null) {
try {
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 行鍵過濾器
*
*/
@Test
public void rowFilter() {
HTableInterface table = null;
try {
table = connection.getTable("student");
//創建掃描器對象
Scan scan = new Scan();
//指定列簇掃描
scan.addFamily("info".getBytes());
/**
* 設置開始key 和結束key
*
*/
/*scan.setStartRow("1500100199".getBytes());
scan.setStopRow("1500100253".getBytes());*/
/**
* rowkey前綴過濾
*
*/
BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator("15001002".getBytes());
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, binaryPrefixComparator);
scan.setFilter(rowFilter);
//執行送秒操作,返回多行結果
ResultScanner results = table.getScanner(scan);
Result line;
//迭代結果集合
while ((line = results.next()) != null) {
//獲取這一行的所有列
List<Cell> cells = line.listCells();
//遍歷每一個單元格
for (Cell cell : cells) {
//獲取rowkey
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
System.out.print(rowkey + "\t");
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
if ("age".equals(qualifier)) {
Integer value = Bytes.toInt(CellUtil.cloneValue(cell));
System.out.print(qualifier + ":" + value + "\t");
} else {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.print(qualifier + ":" + value + "\t");
}
}
System.out.println();
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 最後執行
*/
@After
public void close() {
System.out.println("回收資源");
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}