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
      }
    }
  • 相关阅读:
    Docker 部署 Nginx
    Docker 安装 Redis
    linux shell "2>&1"
    定时备份docker mysql
    SpringBoot 中拦截器和过滤器的使用
    SpringBoot WebMvcConfigurer
    springboot自定义参数解析HandlerMethodArgumentResolver
    mysql在linux下查看my.cnf位置的方法
    Linux下设置mysql允许远程连接
    Android项目实战(六十):修改项目包名
  • 原文地址:https://www.cnblogs.com/zxpgo/p/2779862.html
Copyright © 2011-2022 走看看