【HBase】关于包org.apache.hadoop.hbase.client

时间:2022-03-25 08:28:00

Package org.apache.hadoop.hbase.client Description

提供HBase客户端

关于表


Overview

对于HBase管理员, 可使用 HBaseAdmin进行增删查改操作. 表一旦被创建,可通过 HTable的一个实例来访问,如一次向一个表插入一行. 而插入操作可通过 Put 对象来完成. 通过指定值,目标列及可选的时间戳,类似于 HTable.put(Put)即可提交更新. 
可使用 Get 来获取已插入的值.  Get 使用的范围较广-- 获取某一行的所有数据 --或一部分(如返回一列). 在创建了Get的实例后,可以调用  HTable.get(Get)
使用 Scan 可创建扫描器 -- 如用于访问数据的游标. 在创建及配置Scan实例后, 调用  HTable.getScanner(Scan)并在返回对象激活下一个scanner.  HTable.get(Get) 与  HTable.getScanner(Scan) 都会返回一个  Result对象. 一个Result对象就是一组 KeyValues的队列. 它被以不同的格式打包后返回. 
使用  Delete 可删除数据. 你可以删除单个列或整个列族, 等等. 具体删除用法如  HTable.delete(Delete) .

Put, Get 和 Delete这些操作在执行期间会对行进行加锁. 而在单行上的并发修改会被序列化执行. 在没有行锁的制约时,Get and scan可并行运行,并且保证不返回包含未完全写入的数据.

通过ZooKeeper,客户端代码可访问某个集群信息. 这意味着必须在客户端上的CLASSPATH设置了ZooKeeper的代理配置,才能正常使用. 这通常也是说要保证客户端能找到hbase-site.xml文件.

用法用例

当你拥有一个正在运行的HBase服务, 你可能回想在上面运行你的一个应用. 如果你的程序是用Java构建的,那么就应该使用Java API来进行对HBase的操作. 以下是一个简单的操作HBase示例. 该示例假定已创建了一个名为"myTable"的表,其列族名为"myColumnFamily".

import java.io.IOException;

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.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;


// Class that has nothing but a main.
// Does a Put, Get and a Scan against an hbase table.
public class MyLittleHBaseClient {
  public static void main(String[] args) throws IOException {
    // You need a configuration object to tell the client where to connect.
    // When you create a HBaseConfiguration, it reads in whatever you've set
    // into your hbase-site.xml and in hbase-default.xml, as long as these can
    // be found on the CLASSPATH
    Configuration config = HBaseConfiguration.create();

    // This instantiates an HTable object that connects you to
    // the "myLittleHBaseTable" table.
    HTable table = new HTable(config, "myLittleHBaseTable");

    // To add to a row, use Put.  A Put constructor takes the name of the row
    // you want to insert into as a byte array.  In HBase, the Bytes class has
    // utility for converting all kinds of java types to byte arrays.  In the
    // below, we are converting the String "myLittleRow" into a byte array to
    // use as a row key for our update. Once you have a Put instance, you can
    // adorn it by setting the names of columns you want to update on the row,
    // the timestamp to use in your update, etc.If no timestamp, the server
    // applies current time to the edits.
    Put p = new Put(Bytes.toBytes("myLittleRow"));

    // To set the value you'd like to update in the row 'myLittleRow', specify
    // the column family, column qualifier, and value of the table cell you'd
    // like to update.  The column family must already exist in your table
    // schema.  The qualifier can be anything.  All must be specified as byte
    // arrays as hbase is all about byte arrays.  Lets pretend the table
    // 'myLittleHBaseTable' was created with a family 'myLittleFamily'.
    p.add(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
      Bytes.toBytes("Some Value"));

    // Once you've adorned your Put instance with all the updates you want to
    // make, to commit it do the following (The HTable#put method takes the
    // Put instance you've been building and pushes the changes you made into
    // hbase)
    table.put(p);

    // Now, to retrieve the data we just wrote. The values that come back are
    // Result instances. Generally, a Result is an object that will package up
    // the hbase return into the form you find most palatable.
    Get g = new Get(Bytes.toBytes("myLittleRow"));
    Result r = table.get(g);
    byte [] value = r.getValue(Bytes.toBytes("myLittleFamily"),
      Bytes.toBytes("someQualifier"));
    // If we convert the value bytes, we should get back 'Some Value', the
    // value we inserted at this location.
    String valueStr = Bytes.toString(value);
    System.out.println("GET: " + valueStr);

    // Sometimes, you won't know the row you're looking for. In this case, you
    // use a Scanner. This will give you cursor-like interface to the contents
    // of the table.  To set up a Scanner, do like you did above making a Put
    // and a Get, create a Scan.  Adorn it with column names, etc.
    Scan s = new Scan();
    s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
    ResultScanner scanner = table.getScanner(s);
    try {
      // Scanners return Result instances.
      // Now, for the actual iteration. One way is to use a while loop like so:
      for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
        // print out the row we found and the columns we were looking for
        System.out.println("Found row: " + rr);
      }

      // The other approach is to use a foreach loop. Scanners are iterable!
      // for (Result rr : scanner) {
      //   System.out.println("Found row: " + rr);
      // }
    } finally {
      // Make sure you close your scanners when you are done!
      // Thats why we have it inside a try/finally clause
      scanner.close();
    }
  }
}

关于插入数据到HBase或从中获取数据,还有更多的方法, 但目前上面的示例已经可以让你开始访问并操作HBase数据了. 可参考HTable javadoc以了解更多的方法. 另外,类HBaseAdmin提供很多管理HBase表的方法接口.

如果你的程序并非为Java所构建的, 那你应该考虑使用Thrift或者REST中的库了.

相关文档

同时可参考HBase Reference Guide一节,其内容提及了 HBase Client. 其中还有一小节介绍了如何在多线程下访问HBase,并在客户端程序中如何控制资源等等.


原文链接:http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/package-summary.html