`
nlslzf
  • 浏览: 1039570 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用HBase的一个典型例子,涉及了HBase中很多概念

阅读更多

http://hi.baidu.com/xuelianglv/blog/item/8c68bb01633166d0267fb552.html

一个使用HBase的例子,如下。

import java.io.IOException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Scanner;
import org.apache.hadoop.hbase.io.BatchUpdate;
import org.apache.hadoop.hbase.io.Cell;
import org.apache.hadoop.hbase.io.RowResult;

public class MyClient {

public static void main(String args[]) throws IOException {
    // You need a configuration object to tell the client where to connect.
    // But don't worry, the defaults are pulled from the local config file.
    HBaseConfiguration config = new HBaseConfiguration();

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

    // To do any sort of update on a row, you use an instance of the BatchUpdate
    // class. A BatchUpdate takes a row and optionally a timestamp which your
    // updates will affect. 
    BatchUpdate batchUpdate = new BatchUpdate("myRow");

    // The BatchUpdate#put method takes a Text that describes what cell you want
    // to put a value into, and a byte array that is the value you want to 
    // store. Note that if you want to store strings, you have to getBytes() 
    // from the string for HBase to understand how to store it. (The same goes
    // for primitives like ints and longs and user-defined classes - you must 
    // find a way to reduce it to bytes.)
    batchUpdate.put("myColumnFamily:columnQualifier1", 
      "columnQualifier1 value!".getBytes());

    // Deletes are batch operations in HBase as well. 
    batchUpdate.delete("myColumnFamily:cellIWantDeleted");

    // Once you've done all the puts you want, you need to commit the results.
    // The HTable#commit method takes the BatchUpdate instance you've been 
    // building and pushes the batch of changes you made into HBase.
    table.commit(batchUpdate);

    // Now, to retrieve the data we just wrote. The values that come back are
    // Cell instances. A Cell is a combination of the value as a byte array and
    // the timestamp the value was stored with. If you happen to know that the 
    // value contained is a string and want an actual string, then you must 
    // convert it yourself.
    Cell cell = table.get("myRow", "myColumnFamily:columnQualifier1");
    String valueStr = new String(cell.getValue());
    
    // 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.
    Scanner scanner = 
      // we want to get back only "myColumnFamily:columnQualifier1" when we iterate
      table.getScanner(new String[]{"myColumnFamily:columnQualifier1"});
    
    
    // Scanners in HBase 0.2 return RowResult instances. A RowResult is like the
    // row key and the columns all wrapped up in a single interface. 
    // RowResult#getRow gives you the row key. RowResult also implements 
    // Map, so you can get to your column results easily. 
    
    // Now, for the actual iteration. One way is to use a while loop like so:
    RowResult rowResult = scanner.next();
    
    while(rowResult != null) {
      // print out the row we found and the columns we were looking for
      System.out.println("Found row: " + new String(rowResult.getRow()) + " with value: " +
       rowResult.get("myColumnFamily:columnQualifier1".getBytes()));
      
      rowResult = scanner.next();
    }
    
    // The other approach is to use a foreach loop. Scanners are iterable!
    for (RowResult result : scanner) {
      // print out the row we found and the columns we were looking for
      System.out.println("Found row: " + new String(result.getRow()) + " with value: " +
       result.get("myColumnFamily:columnQualifier1".getBytes()));
    }
    
    // Make sure you close your scanners when you are done!
    scanner.close();
}
}

在这个例子中,使用了HBase中的很多概念,包括:
HBaseConfiguration: 用于告诉client如何连接,连接到哪个HBase的服务器上。
HTable:代表一个HBase表格。
BatchUpdate:用于表格中一行的更新。包括添加某个列,修改某列的值,删除某列等。
commit:table的一个方法。代表某个BatchUpdate操作可以生效了。类似于数据库中的commit操作。

Cell:table中对应某个(行key, 列值,时间戳)下的单元格值。
获取Cell的方法。For example:
table.get("myRow", "myColumnFamily:columnQualifier1");

scanner:用于遍历表格。
rowResult:遍历过程当中保存某行信息。

--

从上面可以看到,HBase中的数据都是Bytes。HBase并不care里面实际存的数据到底是什么数据,只要
该数据可以转化成byte[]即可。

分享到:
评论

相关推荐

    hadoop大数据平台技术与应用---第5章分布式数据库HBase.pdf

    HBase的设计理念让它在很多方面优于传统的关系型数据库。例如,传统关系数据库在处理大规模数据时,可能无法应对系统扩展和性能问题,且在数据结构变化时一般需要停机维护。与此相反,HBase支持随机访问,能提供在线...

    HBase:权威指南

    比如,HBase URL短链接服务“Hush”的实现和运行过程就是一个很好的例子。通过这些实例,读者可以更好地理解HBase如何解决现实世界中的问题。 综上所述,《HBase:权威指南》是一本全面而深入的HBase教程,不仅适合...

    HBase schema 设计(英文)

    HBase作为一个基于Google Bigtable模型的开源分布式数据库,特别适用于需要快速随机访问大量数据的应用程序。它的架构与传统的MySQL、PostgreSQL、Oracle等关系型数据库截然不同,这些特点为应用程序提供的灵活性和...

    行业分类-设备装置-一种应用于HBASE数据库的数据写入方法及系统.zip

    标题中的“行业分类-设备装置-一种应用于HBASE数据库的数据写入方法及系统”表明了这个压缩包内容涉及的是IT行业的数据库管理系统,特别是针对HBase这种分布式列式存储数据库的数据写入策略。HBase是建立在Apache ...

    5-1+基于HBase的New+SQL落地实践.pdf

    总的来说,这份文档提供了关于如何在实际业务环境中部署和使用基于HBase的New SQL的深入指导,涵盖了理论知识、实践经验和技术挑战,对于希望在大数据领域利用HBase进行高性能查询的开发者和管理员具有很高的参考...

    gohbaseHBasego客户端.pdf

    ### gohbase:HBase go客户端 #### 知识点概述 gohbase是一个使用Go语言编写的HBase...随着云计算技术的持续发展,gohbase作为云计算领域的一个组件,其应用前景广阔,同时也为开发者提供了更多的编程语言选择。

    Parquet-Hbase-bulkload-Put-Buffered-example:通过使用 WAL 和缓冲的 Put 从 Hdfs 文件中摄取 HBase 记录

    另一个例子: : 在添加的这个示例中,性能明显提高,将更多的缓冲项从 1 增加到 1000,基于 14 个节点的集群将时间从 25 分钟减少到 50 秒。 但是,将缓冲增加到 10K,响应不如超时开始播放。 这个过程很简单。 ...

    SpringHbaseDemo-master.zip

    标题中的"SpringHbaseDemo-master.zip"表明这是一个关于Spring与HBase集成的示例项目,主要关注于如何在Java环境中利用Spring框架操作HBase数据库。HBase是一个分布式、版本化的NoSQL数据库,常用于大数据存储。...

    Secondary Indexing in Phoenix

    由于HBase本身只能根据主键进行排序和查询,这在很多情况下限制了查询的灵活性和效率。因此,引入二级索引成为解决这一问题的关键。 #### 二、关于演讲者与Phoenix项目 本次演讲由Jesse Yates呈现,他是Salesforce...

    hadoop-demo

    可能是在前一个示例的基础上进行了更深入的学习和实践,例如,可能涉及更复杂的数据处理逻辑,或者引入了Hadoop生态系统中的其他组件,如Hive(用于数据仓库)、Pig(用于数据分析)或者HBase(一个NoSQL数据库)。...

    Programming Hive (hive编程).pdf

    对于初学者来说,安装一个预配置好的虚拟机可以省去很多配置环境的时间和精力,也使得学习过程更为平滑。书中会有详细的安装指南,让读者能够一步步按照说明搭建好开发环境。 除了基础的安装和配置知识,书中还会...

    hadoop与空间数据挖掘分析

    Hadoop与空间数据挖掘分析是大数据技术在特定领域应用的一个典型例子。在当今数字化时代,大数据的管理和分析成为了一个重要的研究和应用领域。大数据通常指的是无法用传统数据库工具在合理时间内进行捕获、管理和...

    5万字2023年数据中台项目建设方案.docx

    我们通过一个实际的例子来详细说明了元数据中心的实现方式和内容,如订单数据的开发流程,订单交易明细表通过一个任务,按照 SKU 的粒度,计算每日 SKU 的交易金额和订单数量,最终输出到 SKU 每日汇总表中。...

    分布式JAVA应用基础与实践(林昊)完整版pad+源码

    分布式JAVA应用基础与实践是Java开发领域中一个重要的主题,主要涵盖了如何在大规模网络环境中设计、部署和管理Java应用程序。本书由林昊编著,旨在帮助开发者深入理解分布式系统的基本概念,掌握Java在分布式环境中...

    使用Flume收集数据内含源码以及说明书可以自己运行复现.zip

    在IT行业中,数据采集是大数据处理的关键步骤,而Apache Flume正是一个专为此设计的分布式、可靠且可用的服务。这份资料“使用Flume收集数据内含源码以及说明书可以自己运行复现.zip”包含了实现Flume数据采集的源码...

    hadoop开发者第三期

    在Hadoop生态系统中,DBInputFormat提供了一个很好的例子,说明如何将Hadoop技术与现有的数据存储方案结合起来,发挥各自的优势,解决大数据时代下的各种数据处理需求。通过这种技术结合,开发者可以利用Hadoop的...

    大数据处理技术概述.pptx

    例如,在单词统计的例子中,Map函数会读取文档内容,对每个单词产生一个键值对,其中键是单词,值是“1”。 2. Shuffle阶段:Map阶段的输出会被自动排序和分区,确保相同键的值会被发送到同一个Reduce节点。这个...

    数据的秘密(下):如何分析数据?.pdf

    举一个我们创业产品项目的例子,我们发现日活中的用户,有相当一部分用户只是注册了,但是并 没有使用我们产品的核心功能,于是我们担心会不会有一些付费推广渠道「刷量」。 所以,我们将新增用户中不活跃的比例按...

    MongoDB 应用场景与最佳实践.docx

    MongoDB 是一个非常流行的 NoSQL 数据库,它的应用场景非常广泛,包括日志存储、监控报表、信息展示、游戏开发等等。学习 MongoDB 需要注意使用 3.2 或以上版本,并建议使用 Linux 系统进行学习。同时,MongoDB 的...

Global site tag (gtag.js) - Google Analytics