小白入门篇| HBase1.0+ java-api介绍

Posted ITPUB技术小栈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小白入门篇| HBase1.0+ java-api介绍相关的知识,希望对你有一定的参考价值。

作者:cjfeii 
鸣谢:感谢ChinaUnix技术社区的cjfeii提供稿件 ,如需转载,请注明出处。

在网上查了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. */
publicstaticvoid createTable(String tableName, String[] family)
            throws Exception {
        Admin admin = connect.getAdmin();
 
TableNametn = TableName.valueOf(tableName);
HTableDescriptordesc = newHTableDescriptor(tn);
for  (inti = 0; i<family.length; i++) {
desc.addFamily(newHColumnDescriptor(family[i]));
        }
 
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. */
TableNametn = TableName.valueOf(tableName);
        Table table = connect.getTable(tn);
 
/* create put. */
        Put put = new Put(Bytes.toBytes(rowKey));
HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies();
 
        for (inti = 0; i<columnFamilies.length; i++) {
            String f = columnFamilies[i].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()方法,统统改用public List<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();
ResultScannerrs = 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));
ResultScannerrs = 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 voidupdateTable(StringtableName, 
String rowKey,
String familyName, StringcolumnName, 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!");
    }

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 voiddeleteColumn(StringtableName, 
String rowKey,
String falilyName, StringcolumnName) 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 voiddeleteAllColumn(StringtableName, StringrowKey)
            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!");
    }

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!");
    }

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

只为一场完美的技术盛宴!
小白入门篇| HBase1.0+ java-api介绍
小白入门篇| HBase1.0+ java-api介绍
SACC2016
作为国内最受欢迎的架构师盛会, 2016第八届中国系统架构师大会(SACC)将于2016年10月27日-29日在北京万达索菲特大酒店撼世来袭!

大会以"架构创新之路"为主题,共设置两个主场分享时段,24个技术交流专场时段;邀请来自互联网、电子商务、金融、电信、政府、行业协会等20多个领域,150多位技术专家及行业领袖来分享他们的经验;并将吸引4000多名系统运维、架构师、及各种企业的IT决策人士参会,为他们提供最具价值的交流平台。
最优折扣来袭!
7月28日前,订购SACC2016门票可立享6.8折优惠!团购更有折上折!
点击 “阅读原文”立即购票!

以上是关于小白入门篇| HBase1.0+ java-api介绍的主要内容,如果未能解决你的问题,请参考以下文章

java小白入门篇eclipse使用技巧

java小白入门篇常见错误

vue入门基础教程之经验总结篇(小白入门必备)|建议收藏

小白入门文本挖掘之基础篇

java小白入门篇eclipse 经典快捷键

性能测试小白入门之基础知识篇一