HBase常用Java API
本節(jié)介紹與 HBase數(shù)據(jù)存儲(chǔ)管理相關(guān)的 Java API(基于 HBase 版本 1.2.3)。HBase 的常用Java API
HBase 主要包括 5 大類操作:HBase 的配置、HBase 表的管理、列族的管理、列的管理、數(shù)據(jù)操作等。
1)org.apache.hadoop.hbase.HBaseConfiguration
HBaseConfiguration 類用于管理 HBase 的配置信息,使用舉例如下。static Configuration cfg = HBaseConfiguration.create();
2)org.apache.hadoop.hbase.client.Admin
Admin 是 Java 接口類型,不能直接用該接口來(lái)實(shí)例化一個(gè)對(duì)象,而是必須通過(guò)調(diào)用 Connection.getAdmin() 方法,來(lái)調(diào)用返回子對(duì)象的成員方法。該接口用來(lái)管理 HBase 數(shù)據(jù)庫(kù)的表信息。它提供的方法包括創(chuàng)建表,刪除表,列出表項(xiàng),使表有效或無(wú)效,以及添加或刪除表列族成員等。
創(chuàng)建表使用的例子如下。
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
Admin admin = connection.getAdmin();
if(admin.tableExists(tableName)) {//如果存在要?jiǎng)?chuàng)建的表,那么先刪除,再創(chuàng)建
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
admin.createTable(tableDescriptor);
admin.disableTable(tableName);
HColumnDescriptor hd =?new?HColumnDescriptor(columnFamily);
admin.addColumn(tableName,hd);
3)org.apache.hadoop.hbase.HTableDescriptor
HTableDescriptor 包含了表的詳細(xì)信息。創(chuàng)建表時(shí)添加列族使用的例子如下。
HTableDescriptor tableDescriptor =?new?HTableDescriptor (tableName);// 表的數(shù)據(jù)模式
tableDescriptor. addFamily (new?HColumnDescriptor("name"));// 增加列族
tableDescriptor.addFamily(new?HColumnDescriptor("age"));
tableDescriptor.addFamily(new?HColumnDescriptor("gender"));
admin.createTable(tableDescriptor);
4)org.apache.hadoop.hbase.HColumnDescriptor
HColumnDescriptor 類維護(hù)著關(guān)于列族的信息,如版本號(hào)、壓縮設(shè)置等。它通常在創(chuàng)建表或者為表添加列族的時(shí)候使用。列族被創(chuàng)建后不能直接修改,只能通過(guò)刪除然后重新創(chuàng)建的方式。列族被刪除的時(shí)候,列族里面的數(shù)據(jù)也會(huì)同時(shí)被刪除。創(chuàng)建表時(shí)添加列族的例子如下。
HTableDescriptor tableDescriptor =?new?HTableDescriptor(tableName);// 表的數(shù)據(jù)模式
HColumnDescriptor hcd = HColumnDescriptor("name".getBytes.());// 構(gòu)造列族
hcd.setValue("firstName".getBytes(),"John".getBytes());//給列族添加列
hcd.setValue("lastName".getBytes(),"Bates".getBytes());// 給列族添加列
tableDescriptor.addFamily(hcd);//把列族加入表描述
5)org.apache.hadoop.hbase.client.Table
Table 是 Java 接口類型,不可以用 Table 直接實(shí)例化一個(gè)對(duì)象,而是必須通過(guò)調(diào)用 connection.getTable() 的一個(gè)子對(duì)象,來(lái)調(diào)用返回子對(duì)象的成員方法。這個(gè)接口可以用來(lái)和 HBase 表直接通信,可以從表中獲取數(shù)據(jù)、添加數(shù)據(jù)、刪除數(shù)據(jù)和掃描數(shù)據(jù)。例子如下。
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable();
ResultScanner scanner = table.getScanner(family);
6)org.apache.hadoop.hbase.client.Put
Put 類用來(lái)對(duì)單元執(zhí)行添加數(shù)據(jù)操作。給表里添加數(shù)據(jù)的例子如下。
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable();
Put put =?new?Put ("*1111".getBytes());//—個(gè) Put 代表一行數(shù)據(jù),行鍵為構(gòu)造方法中傳入的值
put.addColumn("name".getBytes(),null,"Ghander".getBytes());//本行數(shù)據(jù)的第一列
put.addColumn("age".getBytes(),null,"20".getBytes());// 本行數(shù)據(jù)的第二列
put.addColumn("gender".getBytes(),null,"male".getBytes());// 本行數(shù)據(jù)的第三列
put.add("score".getBytes(),"Math".getBytes(),"99".getBytes());// 本行數(shù)據(jù)的第四列
table.put(put);
7)org.apache.hadoop.hbase.client.Get
Get 類用來(lái)獲取單行的數(shù)據(jù)。獲取指定單元的數(shù)據(jù)的例子如下。
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable();
Get g =?new?Get(rowKey.getBytes());
Result rs = table.get(g);
8)org.apache.hadoop.hbase.client.Result
Result 類用來(lái)存放 Get 或 Scan 操作后的查詢結(jié)果,并以 <key,value〉的格式存儲(chǔ)在映射表中。
獲取指定單元的數(shù)據(jù)的例子如下。
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable();
Get g =?new?Get(rowKey.getBytes());
Result.rs = table.get(g);
for?(KeyValue kv : rs.raw()) {
System.out.printIn("rowkey:"+new?String(kv.getRow()));
System.out.printIn("Column Family:"+new?String(kv.getFamily()));
System.out.printIn("Column :"+?new?String(kv.getQualifier()));
System.out.printIn("value :"+?new?String(kv.getValue()));
}
9)org.apache.hadoop.hbase.client.Scan
Scan 類可以用來(lái)限定需要查找的數(shù)據(jù),如版本號(hào)、起始行號(hào)、終止行號(hào)、列族、列限定符、返回值的數(shù)量的上限等。設(shè)置 Scan 的列族、時(shí)間戳的范圍和每次最多返回的單元數(shù)目的例子如下。
Scan scan =?new?Scan();
scan.addFamily(Bytes.toBytes("columnFamily1"));
scan.setTimeRange(1,3);
scan.setBatch(1000);
10)org.apache.hadoop.hbase.client.ResultScanner
ResultScanner 類是客戶端獲取值的接口,可以用來(lái)限定需要查找的數(shù)據(jù),如版本號(hào)、起始行號(hào)、終止行號(hào)、列族、列限定符、返回值的數(shù)量的上限等。獲取指定單元的數(shù)據(jù)的例子如下。
Scan scan =?new?Scan();
scan.addColumn(Bytes.toBytes("columnFamily1"),Bytes.toBytes("column1"));
scan.setTimeRange(1,3);
scan.setBatch(10);
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable();
try?{
ResultScanner resuitScanner = table.getScanner(scan);
Result rs = resultScanner.next();
for?(; rs !=?null;rs = resultScanner.next()){
for?(KeyValue kv : rs.list()){
System.out.printIn("--------------");
System.out.printIn("rowkey:"+?new?String(kv.getRow()));
System.out.printIn("Column Family: "+?new?String(kv.getFamily()));
System.out.printIn("Column :" +?new?String(kv.getQualifier ()));
System.out.printIn("value :"+?new?String(kv.getValue()));
}
}
}?catch?(IOException e) {
e.printStackTrace();
}