如何使用Java連接HBase數(shù)據(jù)庫
Java連接HBase需要兩個類:
HBaseConfiguration
ConnectionFactory
HBaseConfiguration
要連接 HBase 我們首先需要創(chuàng)建Configuration
對象磨确,這個對象我們需要通過HBaseConfiguration
(HBase 配置)對象來進行創(chuàng)建,HBaseConfiguration
看名字我們就能猜到它的用途:讀取指定路徑下hbase-site.xml
和hbase-default.xml
的配置信息。
具體用法:
Configuration config = HBaseConfiguration.create(); //使用create()靜態(tài)方法就可以得到Configuration對象
ConnectionFactory
獲取到連接對象Connextion
我們就算連接上了HBase了,怎么獲取呢?
通過ConnectionFactory
(連接工廠)的方法我們就能獲取到Connection
(連接對象)了颜懊。
具體用法:
Connection connection = ConnectionFactory.createConnection(config); //config為前文的配置對象
使用這兩個步驟就能完成連接 HBase 了。
注意:在1.0之前的版本HBase是使用HBaseAdmin和HTable等來操作HBase的,但是在1.0之后的版本中這些被棄用了厕鹃,新的客戶端API更加干凈簡潔,本文使用的HBase是2.1.1版本(18年10月發(fā)布)的乍丈,
創(chuàng)建表
要創(chuàng)建表我們需要首先創(chuàng)建一個Admin
對象剂碴,然后讓它來創(chuàng)建一張表:
Admin admin = connection.getAdmin(); //使用連接對象獲取Admin對象
TableName tableName = TableName.valueOf("test");//定義表名
HTableDescriptor htd = new HTableDescriptor(tableName);//定義表對象
HColumnDescriptor hcd = new HColumnDescriptor("data");//定義列族對象
htd.addFamily(hcd); //添加
admin.createTable(htd);//創(chuàng)建表
HBase2.X創(chuàng)建表
上述創(chuàng)建表的方法是 HBase1.X 版本的方式,而在 HBase2.X 的版本中創(chuàng)建表使用了新的 API轻专,創(chuàng)建表關(guān)鍵代碼如下:
TableName tableName = TableName.valueOf("test");//定義表名
//TableDescriptor對象通過TableDescriptorBuilder構(gòu)建忆矛;
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data")).build();//構(gòu)建列族對象
tableDescriptor.setColumnFamily(family);//設(shè)置列族
admin.createTable(tableDescriptor.build());//創(chuàng)建表
在 2.X 版本中主要是HTableDescriptor
對象被棄用,取而代之的是TableDescriptor
對象请垛,TableDescriptor
對象通過TableDescriptorBuilder
構(gòu)建催训;
TableName tableName = TableName.valueOf("test");
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
然后添加列簇方法變更:
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data")).build();//構(gòu)建列族對象
tableDescriptor.setColumnFamily(family); //設(shè)置列族
最后由Admin
對象進行創(chuàng)建表操作:
admin.createTable(tableDescriptor.build());
值得咱們注意的是,如果你的 HBase 環(huán)境是 1.X 的那么你只能使用第一種方式來創(chuàng)建表宗收,如果是 2.X 的版本漫拭,那么兩種方式你都可以使用(本實訓(xùn)使用的 HBase 是 2.1.1 版本,所以兩種都可用)混稽。
添加數(shù)據(jù)
要對一個表添加數(shù)據(jù)采驻,我們需要一個Put
對象审胚,在定義Put
對象之前我們需要獲取到Table
對象,這樣才能對指定的表進行操作:
Table table = connection.getTable(tableName);//獲取Table對象
try {
byte[] row = Bytes.toBytes("row1"); //定義行
Put put = new Put(row); //創(chuàng)建Put對象
byte[] columnFamily = Bytes.toBytes("data"); //列
byte[] qualifier = Bytes.toBytes(String.valueOf(1)); //列族修飾詞
byte[] value = Bytes.toBytes("張三豐"); //值
put.addColumn(columnFamily, qualifier, value);
table.put(put); //向表中添加數(shù)據(jù)
} finally {
//使用完了要釋放資源
table.close();
}
獲取指定行的數(shù)據(jù)
我們使用Get
對象與Table
對象就可以獲取到表中的數(shù)據(jù)了挑宠。
//獲取數(shù)據(jù)
Get get = new Get(Bytes.toBytes("row1")); //定義get對象
Result result = table.get(get); //通過table對象獲取數(shù)據(jù)
System.out.println("Result: " + result);
//很多時候我們只需要獲取“值” 這里表示獲取 data:1 列族的值
byte[] valueBytes = result.getValue(Bytes.toBytes("data"), Bytes.toBytes("1")); //獲取到的是字節(jié)數(shù)組
//將字節(jié)轉(zhuǎn)成字符串
String valueStr = new String(valueBytes,"utf-8");
System.out.println("value:" + valueStr);
上述代碼就可以查到table
對象中行row1
的數(shù)據(jù)了菲盾,親自試試驗證一下結(jié)果吧。
掃描表中的數(shù)據(jù)
只獲取一行數(shù)據(jù)顯然不能滿足我們?nèi)康男枨蟾鞯恚覀兿胍@取表中所有的數(shù)據(jù)應(yīng)該怎么操作呢懒鉴?
Scan
、ResultScanner
對象就派上用場了碎浇,接下來我們看個示例你應(yīng)該就明白這兩個對象的用法了:
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
try {
for (Result scannerResult: scanner) {
System.out.println("Scan: " + scannerResult);
byte[] row = scannerResult.getRow();
System.out.println("rowName:" + new String(row,"utf-8"));
}
} finally {
scanner.close();
}
這樣就能將指定表中的數(shù)據(jù)全部輸出到控制臺了临谱。
運行上述代碼你會看到類似這樣的結(jié)果:
Scan: keyvalues={row1/data:1/1542657887632/Put/vlen=6/seqid=0}
rowName:row1
Scan: keyvalues={row2/data:2/1542657887634/Put/vlen=6/seqid=0}
rowName:row2
將表的數(shù)據(jù)和行以及列都展示了。
刪除表
和 HBase shell 的操作一樣奴璃,在 Java 中我們要刪除表悉默,需要先禁用他,然后在刪除它苟穆。
代碼很簡單:
TableName tableName = TableName.valueOf("test");
admin.disableTable(tableName); //禁用表
admin.deleteTable(tableName); //刪除表