`
836811384
  • 浏览: 548794 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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


分享到:
评论

相关推荐

    flume hbanse2.0 lib

    解决新版本flume 不支持hbase 2.0的问题 此资源包由于过大,分为两个分包,另外一个详情见本人博客

    Hbase中文文档

    15.4. Publishing a new version of hbase.apache.org 15.5. 测试 15.6. Maven Build Commands 15.7. Getting Involved 15.8. 开发 15.9. 提交补丁 A. FAQ B. hbck In Depth B.1. Running hbck to identify ...

    HBase.The.Definitive.Guide.2nd.Edition

    If you’re looking for a scalable storage solution to accommodate a virtually endless amount of data, this updated edition shows you how Apache HBase can meet your needs. Modeled after Google’s ...

    Hbase工具类

    import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.util.Bytes; import java.io.IO

    利用java api读取hbase数据遇到的一些坑及解决方法

    java.lang.RuntimeException: org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=36, exceptions: Fri Feb 14 10:30:36 CST 2020, null, java.net.SocketTimeoutException: ...

    spark-examples_2.10-1.6.4-SNAPSHOT.jar

    解决Spark与Hbase版本升级后Pyspark写入,报错找不到StringToImmutableBytesWritableConverte类和找不到:org.apache.hadoop.hbase.client.Put类中的add方法的问题

    HBase-The Definitive Guide-Second Edition-Early Release.pdf

    If you’re looking for a scalable storage solution to accommodate a virtually endless amount of data, this updated edition shows you how Apache HBase can meet your needs. Modeled after Google’s ...

    hbase-debugging:HBase问题的各种测试

    2015-07-31 12:50:24,647 [main] DEBUG org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation - Removed 192.168.1.131:50981 as a location of big_row_1438368609944,,1438368610048....

    spark_hbase:Scala中的示例通过Spark读取保存在hbase中的数据,以及python的转换器示例

    并且仅在返回org.apache.hadoop.hbase.client.Result并执行.count()调用时停止。 在这里,我们提供了Scala中的一个新示例,该示例涉及通过Spark将hbase中保存的数据传输到String ,以及python转换器的新示例。 ...

    phoenix

    Apache Phoenix通过提供一个利用Apache HBase作为其后备存储的关系数据库层,为Apache Hadoop启用OLTP和运营分析。 它包括与Hadoop生态系统中的Apache Spark,Pig,Flume,Map Reduce和其他产品的集成。 它可以作为...

    hbase-0.94.12

    HBase是一个分布式的、面向列的开源数据库,该技术...HBase是Apache的Hadoop项目的子项目。HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库。另一个不同的是HBase基于列的而不是基于行的模式。

    HBase视频教程下载|基于微博数据应用的HBase实战开发

    在大数据热潮中,推出了NoSQL数据库,这种天生就为分布式存储而设计的技术,尤其以Apache HBase为代表,占领海量数据存储技术的大半壁江山。本教视从实战角度出来,向学员们手把手掌握HBase使用精髓,让学员达到如下...

    支持.Net Core的ZooKeeper异步客户端.zip

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、...

Global site tag (gtag.js) - Google Analytics