您的位置:首页 > 运维架构 > Apache

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

2013-10-29 00:26 369 查看


Package org.apache.hadoop.hbase.client

提供HBase客户端接口

参考: Description

Interface概要

InterfaceDescription
Attributes

HConnection

创建于集群间的连接.
HTableInterface

使用单一的HBase表进行数据交互.
HTableInterfaceFactory

定义了创建HTableInterface的方法.
ResultScanner

提供客户端扫描表的接口.
Row

HBase表中的一行数据.
Class概要

ClassDescription
Append

在单行上进行追加操作.
ClientScanner

实现了面向HBase客户端调用的scanner接口.
ClientSmallScanner

用于客户端scanner的小范围扫描.
Delete

用于在单行上执行Delete操作.
Get

用于在单行上执行Get操作.
HBaseAdmin

提供了一个接口和admin级别方法去管理HBase数据库及相关表数据.
HConnectionManager

用于创建的
HConnection
s的不可实例化的类.
HTable

用于访问单个HBase表.
HTableFactory

用于创建HTable实例的工厂类.
HTableMultiplexer

HTableMultiplexer提供了应用于表间的线程安全非阻塞的PUT API.
Increment

用于在单行上执行增量操作.
Mutation

Operation

可以匹配可能的应用级别查询的任意类型的超类.
OperationWithAttributes

Put

用于在单行上执行Put操作.
Result

查询
Get
Scan
的单行结果.
RowMutations

在单行上执行原子性的多种更新操作(Put or Delete).
Scan

用于执行查询Scan操作.
UnmodifyableHTableDescriptor

Read-only表的描述符.
Enum概要

EnumDescription
Durability

该枚举类型描述了表操作的持续性(不被切换或中断)类型,意味着数据项必须按增量的durability顺序来存储。
IsolationLevel

查询操作的隔离级别.
Exception Summary

ExceptionDescription
NoServerForRegionException

当无法找到分区的regionserver时报此异常
RegionOfflineException

当无法定义某个表时报此异常
RetriesExhaustedException

某些操作被连续尝试但一直失败时,与HTable相关的方法报此异常.
RetriesExhaustedWithDetailsException

当有更多的类似与哪个服务器上的哪一行数据引起异常的信息时,它
作为
RetriesExhaustedException
的子类被抛出
ScannerTimeoutException

Scan操作超时.
WrongRowIOException


Package org.apache.hadoop.hbase.client Description

提供HBase客户端

关于表

Overview

Example API Usage


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对象就是一组
KeyValue
s的队列. 它被以不同的格式打包后返回.
使用
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 Home Page

HBase Wiki

Hadoop Home Page

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

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐