HBase Java Api詳解

來源:互聯網
上載者:User

  基本用法參照:http://www.cnblogs.com/NicholasLee/archive/2012/09/14/2684815.html

一、Put操作

package hbase;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.client.Get;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.HTablePool;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.util.Bytes;public class HBaseClient {        // 聲明靜態配置    static Configuration conf = null;    static final HTablePool tablePool;    static {        conf = HBaseConfiguration.create();        conf.set("hbase.zookeeper.quorum", "libin2");        tablePool = new HTablePool(conf, 10);    }        /*     * 建立表     *      * @tableName 表名     *      * @family 列族列表     */    public static void creatTable(String tableName, String[] family)            throws Exception {        HBaseAdmin admin = new HBaseAdmin(conf);        HTableDescriptor desc = new HTableDescriptor(tableName);        for (int i = 0; i < family.length; i++) {            desc.addFamily(new HColumnDescriptor(family[i]));        }        if (admin.tableExists(tableName)) {            System.out.println("table Exists!");            System.exit(0);        } else {            admin.createTable(desc);            System.out.println("create table Success!");        }    }        public static void putData(String tableName) throws IOException{        HTable table = (HTable) tablePool.getTable(tableName);// 擷取表        System.out.println("Auto flush:" + table.isAutoFlush());        table.setAutoFlush(false);        Put put1 = new Put(Bytes.toBytes("rowkey1"));        put1.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));        table.put(put1);                Put put2 = new Put(Bytes.toBytes("rowkey2"));        put2.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));        table.put(put2);                Get get = new Get(Bytes.toBytes("rowkey1"));        Result res1 =  table.get(get);        System.out.println("Result:" + res1);                Put put3 = new Put(Bytes.toBytes("rowkey2"));        put3.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"), Bytes.toBytes("val2"));        table.put(put3);                table.flushCommits();                Result res2=  table.get(get);        System.out.println("Result:" + res2);    }        public static void main(String[] args) throws Exception {        //String tableName = "libinHTable"; String[] family = {"colfam1"};        //creatTable(tableName,family);        putData("libinHTable");    }}

 Auto flush:true
Result:keyvalues=NONE
Result:keyvalues={rowkey1/colfam1:qual1/1354092615995/Put/vlen=4}

  其中,通過調用HTable.setAutoFlush(false)方法可以將HTable寫用戶端的自動flush關閉,這樣可以批量寫入資料到 HBase,而不是有一條put就執行一次更新,只有當put填滿用戶端寫緩衝時,才實際向HBase服務端發起寫請求。預設情況下auto flush是開啟的。

  res1為NONE是因為用戶端write buffer還在記憶體裡,並沒有發送到服務端。

  批量Put操作

public static void putListData(String tableName) throws IOException{        HTable table = (HTable) tablePool.getTable(tableName);// 擷取表        List<Put> puts = new ArrayList<Put>();                Put put1 = new Put(Bytes.toBytes("rowkey1"));        put1.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));        puts.add(put1);                Put put2 = new Put(Bytes.toBytes("rowkey2"));        put2.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));        puts.add(put2);                table.put(puts);    }    

   這些Put進程如果在服務端那邊出錯後會儲存在本地write buffer裡,可以用getWriteBuffer()方法進行訪問,批量插入的資料的時間戳記都是相同的。

  使用批量Put,我們無法控制put在服務端那邊的順序。

  批量Put的出錯處理:

public static void putListData(String tableName) throws IOException {        HTable table = (HTable) tablePool.getTable(tableName);// 擷取表        List<Put> puts = new ArrayList<Put>();        Put put1 = new Put(Bytes.toBytes("rowkey1"));        put1.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),                Bytes.toBytes("val1"));        puts.add(put1);        Put put2 = new Put(Bytes.toBytes("rowkey2"));        // put2.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),        // Bytes.toBytes("val1"));        puts.add(put2);        Put put3 = new Put(Bytes.toBytes("rowkey3"));        put3.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),                Bytes.toBytes("val1"));        puts.add(put3);        try {            table.put(puts);        } catch (Exception e) {            System.out.println("Error:" + e);            table.flushCommits();        }    }

   table.put(puts);會先把put放到table中,如果出錯,就自動認可flush。例如put2是空的,那麼put2之前的資料可以put到表中:

   Error:java.lang.IllegalArgumentException: No columns to insert

  checkAndPut:

public static void putAndCheck(String tableName) throws IOException {        HTable table = (HTable) tablePool.getTable(tableName);// 擷取表        Put put1 = new Put(Bytes.toBytes("rowkey1"));        put1.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),                Bytes.toBytes("val1"));                boolean res1 = table.checkAndPut(Bytes.toBytes("rowkey1"),Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),                null, put1);        System.out.println("Put applied:" + res1);                boolean res2 = table.checkAndPut(Bytes.toBytes("rowkey1"),Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),                null, put1);        System.out.println("Put applied:" + res2);                boolean res3 = table.checkAndPut(Bytes.toBytes("rowkey2"),Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),                null, put1);        System.out.println("Put applied:" + res3);    }

Put applied:true
Put applied:false
Exception in thread "main" org.apache.hadoop.hbase.DoNotRetryIOException: org.apache.hadoop.hbase.DoNotRetryIOException: Action's getRow must match the passed row

  第一個put執行的時候,put1不存在,可以插入,第二個put因為put1已經存在,所以執行失敗,第三個報錯,因為check和put針對的是同一個row,即同一個rowkey。

 

二、Get操作

public static void getData(String tableName, String rowKey) throws IOException {        HTable table = (HTable) tablePool.getTable(tableName);// 擷取表        Get get = new Get(Bytes.toBytes(rowKey));        get.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"));        Result result = table.get(get);        byte[] val = result.getValue(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"));        System.out.println("value:" + Bytes.toString(val));        System.out.println(Bytes.toString(result.getRow()));        System.out.println(Bytes.toString(result.value()));        System.out.println(result.size());                for (KeyValue kv : result.list()) {            System.out.println("family:" + Bytes.toString(kv.getFamily()));            System.out                    .println("qualifier:" + Bytes.toString(kv.getQualifier()));            System.out.println("value:" + Bytes.toString(kv.getValue()));            System.out.println("Timestamp:" + kv.getTimestamp());        }        System.out.println(result.getColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")));    }

value:val1
rowkey1
val1
1
family:colfam1
qualifier:qual1
value:val1
Timestamp:1354167940755
[rowkey1/colfam1:qual1/1354167940755/Put/vlen=4]
  byte[] value返回的順序是按字典排序的第一行;

  size()返回KeyValue的個數,可以遍曆KeyValue擷取Result的詳細資料;

  getColumn返回的是完成的列的資訊。批量Get操作的時候如果遇到任何一個get報錯(比如get不存在的列)整個執行都會終端,不會返回任何結果。

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.