该文章已在ChinaUnix技术社区发表,链接地址:
http://mp.weixin.qq.com/s?__biz= ... e=0#wechat_redirect
HBase1.0+java-api介绍
在网上查了hbase的java client api的介绍,发现很多都是之前的版本,虽然现在还可以运行,但是里面有很多不建议使用(@deprecated)的class及相关方法。其中很关键的就是增加了Connection类,使其给人感觉更像一个专业的客户端api,还有就是用Admin替换了HBaseAdmin,还有一些细节上的修改,比如:增加了TableName,获取数据的时候用Cell替换KeyValue,以及其他的一些修改。
本文主要介绍了这样的一些改动,并写了一些常用操作的代码以供参考。
1. 创建config和connection
org.apache.hadoop.hbase.client.Connection是hbase从0.99.0开始增加的类,Connection主要创建一个hbase客户端连接,用于和hbase通信:
Configuration conf = HBaseConfiguration.create(); conf.addResource("hbase-site.xml"); Connection connect = ConnectionFactory.createConnection(conf);
2. 创建表
0.99.0之前的版本主要用HBaseAdmin创建新的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. 写入数据
新的版本中(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. 获取数据
获取数据的时候,在0.96.0及之后的版本,有一个很重要的改动就是,弃用了Result class中的public List<KeyValue> list()方法,统统改用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. 遍历表数据
/* 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. 遍历一定范围的表数据
/* 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. 获取指定行指定列数据
/* 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. 修改列数据
和之前的实现类似,就是直接覆盖:
/* 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. 获得多个版本的数据
/* 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. 删除数据
以下方法包括删除列、行数据:
/* 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. 删除表
和创建表一样,都需要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. 所有的代码
前面也介绍了很多,完整的代码在此,都是经过测试过的:
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
|