zoukankan      html  css  js  c++  java
  • Client API:The basics

    Things to remember:

    1.Create HTable instances only once, usually when your application starts.

    2.Create a seperate HTable instance for every thread you execute(or use HTablePool).

    3.Updates are atomic(原子性的) on a per row basis.

    Put Mehtod

    A row in HBase is identified by a unique row key and -as is the case with most values in HBase - this is Java byte[] array.

    View Code
    package client;
    
    // cc PutExample Example application inserting data into HBase
    // vv PutExample
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.util.Bytes;
    // ^^ PutExample
    import util.HBaseHelper;
    // vv PutExample
    
    import java.io.IOException;
    
    public class PutExample {
    
      public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create(); // co PutExample-1-CreateConf Create the required configuration.
    
        // ^^ PutExample
        HBaseHelper helper = HBaseHelper.getHelper(conf);
        helper.dropTable("testtable");
        helper.createTable("testtable", "colfam1");
        // vv PutExample
        HTable table = new HTable(conf, "testtable"); // co PutExample-2-NewTable Instantiate a new client.
    
        Put put = new Put(Bytes.toBytes("row1")); // co PutExample-3-NewPut Create put with specific row.
    
        put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
          Bytes.toBytes("val1")); // co PutExample-4-AddCol1 Add a column, whose name is "colfam1:qual1", to the put.
        put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
          Bytes.toBytes("val2")); // co PutExample-4-AddCol2 Add another column, whose name is "colfam1:qual2", to the put.
    
        table.put(put); // co PutExample-5-DoPut Store row with column into the HBase table.
      }
    }
    // ^^ PutExample

    Versioning of Data

    A special feature of HBase is the possibility to store multiple versions of each cell(the value of a particular column).This is achived by using timestamps for each of the versions and storing them in a descending order.

    When you put a value into HBASE you hava the choice of either explicitly proving a timestamp or omit that value, which in turn is then filled in by the RegionServer when the put operation is performed.(example P156)

    Client-side Write Buffer

    The Hbase API comes with a built in client-side write buffer that collects put operations so that they are sent in one RPC call to the server(s).(P163)

    View Code
    package client;
    
    // cc PutWriteBufferExample Example using the client-side write buffer
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.Get;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.util.Bytes;
    import util.HBaseHelper;
    
    import java.io.IOException;
    
    public class PutWriteBufferExample {
    
      public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
    
        HBaseHelper helper = HBaseHelper.getHelper(conf);
        helper.dropTable("testtable");
        helper.createTable("testtable", "colfam1");
        // vv PutWriteBufferExample
        HTable table = new HTable(conf, "testtable");
        System.out.println("Auto flush: " + table.isAutoFlush());  // co PutWriteBufferExample-1-CheckFlush Check what the auto flush flag is set to, should print "Auto flush: true".
    
        table.setAutoFlush(false); // co PutWriteBufferExample-2-SetFlush Set the auto flush to false to enable the client-side write buffer.
    
        Put put1 = new Put(Bytes.toBytes("row1"));
        put1.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
          Bytes.toBytes("val1"));
        table.put(put1); // co PutWriteBufferExample-3-DoPut Store some rows with columns into HBase.
    
        Put put2 = new Put(Bytes.toBytes("row2"));
        put2.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
          Bytes.toBytes("val2"));
        table.put(put2);
    
        Put put3 = new Put(Bytes.toBytes("row3"));
        put3.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
          Bytes.toBytes("val3"));
        table.put(put3);
    
        Get get = new Get(Bytes.toBytes("row1"));
        Result res1 = table.get(get);
        System.out.println("Result: " + res1); // co PutWriteBufferExample-6-Get1 Try to load previously stored row, this will print "Result: keyvalues=NONE".
    
        table.flushCommits(); // co PutWriteBufferExample-7-Flush Force a flush, this causes an RPC to occur.
    
        Result res2 = table.get(get);
        System.out.println("Result: " + res2); // co PutWriteBufferExample-8-Get2 Now the row is persisted and can be loaded.
        // ^^ PutWriteBufferExample
      }
    }

    Explicit Flush(显式的)

      Use the flushCommits() call to send the data to the servers for permanent storage.

    Implicit Flush(隐式的)

      This is triggered when you call put(), or setWriteBufferSize().Both calls compare the currently used buffer size with the configured limit and optionally invokes the flushCommits() method.In case the entire buffer is disabled,setting setAutoFlush(true),will force the client to call the flush method for every invocation of put().

      Another call triggering the flush implicitly and unconditionally is the close() method of HTable.

    List of Puts

    The client API has the ability to insert single Put instance as shown above, but it also has the advanced feature of batching operations together.

    View Code
    package client;
    
    // cc PutListExample Example inserting data into HBase using a list
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.util.Bytes;
    import util.HBaseHelper;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class PutListExample {
    
      public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        HBaseHelper helper = HBaseHelper.getHelper(conf);
        helper.dropTable("testtable");
        helper.createTable("testtable", "colfam1");
        HTable table = new HTable(conf, "testtable");
    
        // vv PutListExample
        List<Put> puts = new ArrayList<Put>(); // co PutListExample-1-CreateList Create a list that holds the Put instances.
    
        Put put1 = new Put(Bytes.toBytes("row1"));
        put1.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
          Bytes.toBytes("val1"));
        puts.add(put1); // co PutListExample-2-AddPut1 Add put to list.
    
        Put put2 = new Put(Bytes.toBytes("row2"));
        put2.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
          Bytes.toBytes("val2"));
        puts.add(put2); // co PutListExample-3-AddPut2 Add another put to list.
    
        Put put3 = new Put(Bytes.toBytes("row2"));
        put3.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
          Bytes.toBytes("val3"));
        puts.add(put3); // co PutListExample-4-AddPut3 Add third put to list.
    
        table.put(puts); // co PutListExample-5-DoPut Store multiple rows with columns into HBase.
        // ^^ PutListExample
      }
    }
  • 相关阅读:
    Atiitt 对象转换json 序列化规范 Java 循环引用的解决 设置序列化层次深度 去除不必的属性 太长不方便月度 jsonObject.remove("num1"); Prety fo
    Atitit 研发管理之道 attilax总结 艾龙 著 研发管理 1 简介 1 基本理念 2 基本原则 2 内容 3 团队建设 4 流程设计 4 成本管理 4 项目管理 4 绩效管理 4 风险管理
    Atitit 软件设计中的各种图纸 uml 之道 1. 常见设计成果与图纸 1 1.1. ui原型图与html 1 1.2. 业务逻辑 伪代码 各种uml图 1 1.3. 业务逻辑 流程图 ns
    Atitit ForkJoinTask的使用以及与futuretask的区别 1.1. Forkjoin原理图 1 1.2. Fork/Join使用两个类完成以上两件事情:ForkJoinTask
    Atitit 利用前端cache indexdb localStorage 缓存提升性能优化attilax总结 1.1. indexdb 更加强大点,但是结果测试,api比较繁琐 使用叫麻烦些 1
    Atitit pg10分区 总结 1.1. create table tmp_log (  1 1.2. 创建索引 1 1.3. 查看表 in pgadmin4 2 2. 二 分区表管理 2 2.1
    ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法(转载)
    .net3.5SP1开发项目引发的血案
    仿QQ弹出窗口
    ASP.NET中的数据绑定:哪个更快?
  • 原文地址:https://www.cnblogs.com/zxpgo/p/2779862.html
Copyright © 2011-2022 走看看