該文章已在ChinaUnix技術社區(qū)發(fā)表,鏈接地址:
http://mp.weixin.qq.com/s?__biz= ... e=0#wechat_redirect
HBase1.0+java-api介紹
在網(wǎng)上查了hbase的java client api的介紹,發(fā)現(xiàn)很多都是之前的版本,雖然現(xiàn)在還可以運行,但是里面有很多不建議使用(@deprecated)的class及相關方法。其中很關鍵的就是增加了Connection類,使其給人感覺更像一個專業(yè)的客戶端api,還有就是用Admin替換了HBaseAdmin,還有一些細節(jié)上的修改,比如:增加了TableName,獲取數(shù)據(jù)的時候用Cell替換KeyValue,以及其他的一些修改。
本文主要介紹了這樣的一些改動,并寫了一些常用操作的代碼以供參考。
1. 創(chuàng)建config和connection
org.apache.hadoop.hbase.client.Connection是hbase從0.99.0開始增加的類,Connection主要創(chuàng)建一個hbase客戶端連接,用于和hbase通信:
Configuration conf = HBaseConfiguration.create(); conf.addResource("hbase-site.xml"); Connection connect = ConnectionFactory.createConnection(conf);
2. 創(chuàng)建表
0.99.0之前的版本主要用HBaseAdmin創(chuàng)建新的Table,但是在0.99.0之后,可以直接用Admin來create, drop, list, enable, disable Table了。
還有一個要注意的,在操作table的過程中,表名稱不能再用String類型,而需要用TableName類型了:
/* test create table. */ public static voidcreateTable(String tableName, String[] family) throws Exception { Admin admin = connect.getAdmin(); TableName tn = TableName.valueOf(tableName); HTableDescriptor desc = newHTableDescriptor(tn); for (int i = 0; i < family.length; i++) { desc.addFamily(newHColumnDescriptor(family)); } if(admin.tableExists(tn)) { System.out.println("table Exists!"); System.exit(0); } else { admin.createTable(desc); System.out.println("create table Success!"); } }
3. 寫入數(shù)據(jù)
新的版本中(0.99.0+)還有一個重要的改動是,HTable不再是客戶端api,一些操作Table的動作(get, put, delete, scan)都需要直接用Table class來完成:
/* put data into table. */ public static void addData(String rowKey, String tableName, String[] column1, String[] value1, String[] column2, String[] value2) throws IOException { /* get table. */ TableName tn = TableName.valueOf(tableName); Table table = connect.getTable(tn); /* create put. */ Put put = new Put(Bytes.toBytes(rowKey)); HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies(); for (int i = 0; i <columnFamilies.length; i++) { String f = columnFamilies.getNameAsString(); if (f.equals("article")) { for (int j = 0; j < column1.length; j++) { put.addColumn(Bytes.toBytes(f), Bytes.toBytes(column1[j]),Bytes.toBytes(value1[j])); } } if (f.equals("author")) { for (int j = 0; j < column2.length; j++) { put.addColumn(Bytes.toBytes(f), Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j])); } } } /* put data. */ table.put(put); System.out.println("add data Success!"); }
4. 獲取數(shù)據(jù)
獲取數(shù)據(jù)的時候,在0.96.0及之后的版本,有一個很重要的改動就是,棄用了Result class中的public List<KeyValue> list()方法,統(tǒng)統(tǒng)改用publicList<Cell> listCells()方法:
/* get data. */ public static void getResult(String tableName, String rowKey) throws IOException { /* get table. */ Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } }
5. 遍歷表數(shù)據(jù)
/* scan table. */ public static void getResultScan(String tableName) throws IOException { Scan scan = new Scan(); ResultScanner rs = null; Table table = connect.getTable(TableName.valueOf(tableName)); try { rs = table.getScanner(scan); for (Result r : rs) { for (Cell cell : r.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey:" + new String(CellUtil.cloneRow(cell))); System.out.println("family:" + new String(CellUtil.cloneFamily(cell))); System.out.println("column:" + new String(CellUtil.cloneQualifier(cell))); System.out.println("value :" + new String(CellUtil.cloneValue(cell))); System.out.println("timest:" + cell.getTimestamp()); } } } finally { rs.close(); } }
6. 遍歷一定范圍的表數(shù)據(jù)
/* range scan table. */ public static void getResultScan(String tableName, String start_rowkey, String stop_rowkey) throws IOException { Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(start_rowkey)); scan.setStopRow(Bytes.toBytes(stop_rowkey)); ResultScanner rs = null; Table table = connect.getTable(TableName.valueOf(tableName)); try { rs = table.getScanner(scan); for (Result r : rs) { for (Cell cell : r.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey:" + new String(CellUtil.cloneRow(cell))); System.out.println("family:" + new String(CellUtil.cloneFamily(cell))); System.out.println("column:" + new String(CellUtil.cloneQualifier(cell))); System.out.println("value :" + new String(CellUtil.cloneValue(cell))); System.out.println("timest:" + cell.getTimestamp()); } } } finally { rs.close(); } }
7. 獲取指定行指定列數(shù)據(jù)
/* get column data. */ public static void getResultByColumn(String tableName, String rowKey, String familyName, String columnName) throws IOException { /* get table. */ Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); // 獲取指定列族和列修飾符對應的列 Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } }
8. 修改列數(shù)據(jù)
和之前的實現(xiàn)類似,就是直接覆蓋:
/* update. */ public static void updateTable(String tableName, String rowKey, String familyName, String columnName, String value) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value)); table.put(put); System.out.println("updatetable Success!"); }
9. 獲得多個版本的數(shù)據(jù)
/* get multi-version data. */ public static void getResultByVersion(String tableName, String rowKey, String familyName, String columnName) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); get.setMaxVersions(5); Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("timest: " + cell.getSequenceId()); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } }
10. 刪除數(shù)據(jù)
以下方法包括刪除列、行數(shù)據(jù):
/* delete column. */ public static void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Delete deleteColumn = new Delete(Bytes.toBytes(rowKey)); deleteColumn.addColumns(Bytes.toBytes(falilyName), Bytes.toBytes(columnName)); table.delete(deleteColumn); System.out.println(falilyName + ":" + columnName + "is deleted!"); } /* delete row. */ public static void deleteAllColumn(String tableName, String rowKey) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Delete deleteAll = new Delete(Bytes.toBytes(rowKey)); table.delete(deleteAll); System.out.println("allcolumns are deleted!"); }
11. 刪除表
和創(chuàng)建表一樣,都需要Admin來刪除表:
/* delete table. */ public static void deleteTable(String tableName) throws IOException { Admin admin = connect.getAdmin(); admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); System.out.println(tableName + "is deleted!"); }
12. 所有的代碼
前面也介紹了很多,完整的代碼在此,都是經(jīng)過測試過的:
package chapter12; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.util.Bytes; public class HBaseTestCase { /* create config and connection. */ static Configuration conf = HBaseConfiguration.create(); static Connection connect = null; static { try { connect = ConnectionFactory.createConnection(conf); } catch (Exception e) { e.printStackTrace(); } } /* test create table. */ public static void createTable(String tableName, String[] family) throws Exception { Admin admin = connect.getAdmin(); TableName tn = TableName.valueOf(tableName); HTableDescriptor desc = new HTableDescriptor(tn); for (int i = 0; i <family.length; i++) { desc.addFamily(newHColumnDescriptor(family)); } if (admin.tableExists(tn)) { System.out.println("table Exists!"); System.exit(0); } else { admin.createTable(desc); System.out.println("create table Success!"); } } /* put data into table. */ public static void addData(String rowKey, String tableName, String[] column1, String[] value1, String[] column2, String[] value2) throws IOException { /* get table. */ TableName tn = TableName.valueOf(tableName); Table table = connect.getTable(tn); /* create put. */ Put put = new Put(Bytes.toBytes(rowKey)); HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies(); for (int i = 0; i <columnFamilies.length; i++) { String f = columnFamilies.getNameAsString(); if (f.equals("article")) { for (int j = 0; j < column1.length; j++) { put.addColumn(Bytes.toBytes(f), Bytes.toBytes(column1[j]),Bytes.toBytes(value1[j])); } } if (f.equals("author")) { for (int j = 0; j < column2.length; j++) { put.addColumn(Bytes.toBytes(f), Bytes.toBytes(column2[j]),Bytes.toBytes(value2[j])); } } } /* put data. */ table.put(put); System.out.println("add data Success!"); } /* get data. */ public static void getResult(String tableName, String rowKey) throws IOException { /* get table. */ Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } } /* scan table. */ public static void getResultScan(String tableName) throws IOException { Scan scan = new Scan(); ResultScanner rs = null; Table table = connect.getTable(TableName.valueOf(tableName)); try { rs = table.getScanner(scan); for (Result r : rs) { for (Cell cell : r.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey:" + new String(CellUtil.cloneRow(cell))); System.out.println("family:" + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value :" + new String(CellUtil.cloneValue(cell))); System.out.println("timest:" + cell.getTimestamp()); } } } finally { rs.close(); } } /* range scan table. */ public static void getResultScan(String tableName, String start_rowkey, String stop_rowkey) throws IOException { Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(start_rowkey)); scan.setStopRow(Bytes.toBytes(stop_rowkey)); ResultScanner rs = null; Table table = connect.getTable(TableName.valueOf(tableName)); try { rs = table.getScanner(scan); for (Result r : rs) { for (Cell cell : r.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey:" + new String(CellUtil.cloneRow(cell))); System.out.println("family:" + new String(CellUtil.cloneFamily(cell))); System.out.println("column:" + new String(CellUtil.cloneQualifier(cell))); System.out.println("value :" + new String(CellUtil.cloneValue(cell))); System.out.println("timest:" + cell.getTimestamp()); } } } finally { rs.close(); } } /* get column data. */ public static void getResultByColumn(String tableName, String rowKey, String familyName, String columnName) throws IOException { /* get table. */ Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); // 獲取指定列族和列修飾符對應的列 Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } } /* update. */ public static void updateTable(String tableName, String rowKey, String familyName, String columnName, String value) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value)); table.put(put); System.out.println("update table Success!"); } /* get multi-version data. */ public static void getResultByVersion(String tableName, String rowKey, String familyName, String columnName) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); get.setMaxVersions(5); Result result = table.get(get); for (Cell cell : result.listCells()) { System.out.println("------------------------------------"); System.out.println("timest: " + cell.getSequenceId()); System.out.println("rowkey: " + new String(CellUtil.cloneRow(cell))); System.out.println("family: " + new String(CellUtil.cloneFamily(cell))); System.out.println("column: " + new String(CellUtil.cloneQualifier(cell))); System.out.println("value : " + new String(CellUtil.cloneValue(cell))); System.out.println("timest: " + cell.getTimestamp()); } } /* delete column. */ public static void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Delete deleteColumn = new Delete(Bytes.toBytes(rowKey)); deleteColumn.addColumns(Bytes.toBytes(falilyName),Bytes.toBytes(columnName)); table.delete(deleteColumn); System.out.println(falilyName + ":" + columnName + "is deleted!"); } /* delete row. */ public static void deleteAllColumn(String tableName, String rowKey) throws IOException { Table table = connect.getTable(TableName.valueOf(tableName)); Delete deleteAll = new Delete(Bytes.toBytes(rowKey)); table.delete(deleteAll); System.out.println("all columns are deleted!"); } /* delete table. */ public static void deleteTable(String tableName) throws IOException { Admin admin = connect.getAdmin(); admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); System.out.println(tableName + "is deleted!"); } public static void main (String[] agrs) throws Exception { try { /* create table. */ String tableName = "blog2"; String[] family = { "article", "author" }; createTable(tableName, family); /* put data. */ String[] column1 = { "title", "content", "tag" }; String[] value1 = { "Head First HBase", "HBase is the Hadoop database. Use it when you need random, realtimeread/write access to your Big Data.", "Hadoop,HBase,NoSQL" }; String[] column2 = { "name", "nickname" }; String[] value2 = { "nicholas", "lee" }; addData("rowkey1", tableName, column1, value1, column2, value2); addData("rowkey2", tableName, column1, value1, column2, value2); addData("rowkey3", tableName, column1, value1, column2, value2); /* scan query. */ getResultScan(tableName, "rowkey4", "rowkey5"); /* range scan query. */ getResultScan(tableName, "rowkey4", "rowkey5"); /* get data. */ getResult(tableName, "rowkey1"); /* get column data. */ getResultByColumn(tableName, "rowkey1", family[1], "name"); /* update column data. */ updateTable(tableName, "rowkey1", family[1], "name", "bin"); /* get column data. */ getResultByColumn(tableName, "rowkey1", family[1], "name"); /* get multi-version data. */ getResultByVersion(tableName, "rowkey1", family[1], "name"); /* delete column.*/ deleteColumn(tableName, "rowkey1", family[1], "nickname"); /* delete row. */ deleteAllColumn(tableName, "rowkey1"); /* delete table. */ deleteTable(tableName); } catch (Exception e) { e.printStackTrace(); } } }
ref
http://www.cnblogs.com/ggjucheng/p/3381328.html
http://blog.sina.com.cn/s/blog_66474b1601017fxr.html
http://www.aboutyun.com/thread-7149-1-1.html
|