亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1786 | 回復: 0
打印 上一主題 下一主題

[Hadoop&HBase] HBase1.0+ java-api 介紹 [復制鏈接]

論壇徽章:
72
20周年集字徽章-20	
日期:2020-10-28 14:04:30操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-07-13 06:20:0015-16賽季CBA聯(lián)賽之廣夏
日期:2016-07-10 09:04:02數(shù)據(jù)庫技術版塊每日發(fā)帖之星
日期:2016-07-09 06:20:00操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-07-09 06:20:00數(shù)據(jù)庫技術版塊每日發(fā)帖之星
日期:2016-07-07 06:20:00操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-07-07 06:20:00操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-07-04 06:20:00數(shù)據(jù)庫技術版塊每日發(fā)帖之星
日期:2016-07-03 06:20:00操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-07-03 06:20:00數(shù)據(jù)庫技術版塊每日發(fā)帖之星
日期:2016-07-02 06:20:00操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-07-02 06:20:00
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2016-07-07 10:45 |只看該作者 |倒序瀏覽
該文章已在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)建configconnection
org.apache.hadoop.hbase.client.Connectionhbase0.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之后,可以直接用Admincreate, 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


您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP