`
zkl_1987
  • 浏览: 244735 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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

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.  If no timestamp, the server applies current time
    // to the edits.
    BatchUpdate batchUpdate = new BatchUpdate("myRow");

    // The BatchUpdate#put method takes a byte [] (or String) that designates
    // 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 store it since HBase is
    // all about byte arrays. The same goes for primitives like ints and longs
    // and user-defined classes - you must find a way to reduce it to bytes.
    // The Bytes class from the hbase util package has utility for going from
    // String to utf-8 bytes and back again and help for other base types.
    batchUpdate.put("myColumnFamily:columnQualifier1",
      Bytes.toBytes("columnQualifier1 value!"));

    // 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");
    // This could throw a NullPointerException if there was no value at the cell
    // location.
    String valueStr = Bytes.toString(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 return RowResult instances. A RowResult is like the
    // row key and the columns all wrapped up in a single Object.
    // 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: " + Bytes.toString(rowResult.getRow()) +
        " with value: " + rowResult.get(Bytes.toBytes("myColumnFamily:columnQualifier1")));
      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: " + Bytes.toString(rowResult.getRow()) +
        " with value: " + rowResult.get(Bytes.toBytes("myColumnFamily:columnQualifier1")));
    }
   
    // Make sure you close your scanners when you are done!
    // Its probably best to put the iteration into a try/finally with the below
    // inside the finally clause.
    scanner.close();
  }
}
分享到:
评论

相关推荐

    Hbase笔记 —— 利用JavaAPI的方式操作Hbase数据库(往hbase的表中批量插入数据).pdf

    在本文档中,我们将深入探讨如何使用Java API与HBase数据库进行交互,特别是关于如何创建表、修改表结构以及批量插入数据。HBase是Apache的一个分布式、可扩展的大数据存储系统,它基于谷歌的Bigtable设计,适用于...

    hbase-0.90.2中创建表、插入数据,更新数据,删除数据

    假设有一个不知道是干什么表:) 表里需要存入人员和其相对应的部门信息 HBaseAdmin admin = new HBaseAdmin(configuration); List<Put> putuser = new ArrayList();

    hbase和hadoop数据块损坏处理

    Export 和 Import 是 HBase 的内置功能,可以将 HBase 表内容输出成 HDFS 的 SequenceFiles 文件,然后将文件插入到 HBase 表中。 Snapshot 是 HBase 的快照功能,可以快速创建快照,然后将快照恢复到 HBase 表中。...

    hbase存储csv数据的代码实现

    本文将深入探讨如何使用代码实现将CSV(逗号分隔值)数据存储到HBase中,帮助你更好地理解和掌握HBase的用法。 首先,我们需要理解HBase的基本概念。HBase是构建在Hadoop之上的NoSQL数据库,它以行键、列族、列和...

    Hadoop数据迁移--从Hadoop向HBase

    具体到从Hadoop向HBase的数据迁移中,Map任务会读取HDFS中的文件,使用自定义的Mapper类对每一行数据进行解析,提取出关键信息,如行键(row key)、列族(column family)、列限定符(column qualifier)和值...

    mysql中数据经处理导入到hbase中

    导入数据到HBase通常通过HBase的客户端API实现,这些API提供了连接到HBase集群、创建表、插入数据等功能。例如,在Java中,我们可以使用HBase的Admin API创建表,然后使用Put对象添加数据。在导入过程中,需要将JSON...

    hbase的操作数据demo

    在HBase中,数据被组织成表格形式,由行键(Row Key)、列族(Column Family)、列(Qualifier)和时间戳(Timestamp)组成。这个压缩包文件“findhbase”可能是提供了一系列HBase操作的示例或工具,帮助用户了解...

    下载ftp文件,读取内容通过Phoenix JDBC连接往Hbase数据库中建表插入数据。

    在Phoenix的帮助下,你可以创建表、插入数据、更新和删除数据,以及执行复杂的查询。 在建表时,你需要定义列族(Column Families),这是Hbase存储数据的基本单元,类似于关系数据库中的表格。每个列族可以包含多...

    浅谈HBASE数据结构设计.pdf

    - Put:Put操作用于向HBase中插入或更新数据。如果指定的行键已存在,之前的值会被新值覆盖。 - Delete:Delete操作用于删除HBase中的数据,可以指定删除某行的全部数据,也可以指定删除某行的某列的数据。 7. 高级...

    HBase官方指南——数据模型篇

    - Put操作用于在指定行中插入或更新数据。 - Scan操作用于扫描表中的数据。 - Delete操作用于删除指定行中的数据或者整个行。 9. 排序(Sort Order) HBase表中的数据是按照行键的字典序进行排序的。这使得检索相邻...

    Python-HBase中文参考指南

    这两种库都允许开发者创建表、插入和查询数据,以及管理HBase集群。 ### 三、HBase表的创建与管理 在Python中创建HBase表,需要指定表名、列族以及可能的初始配置。例如,使用`happybase`创建一个名为`my_table`,...

    基于Apache HBase的CSV数据批量导入与操作工具.zip

    基于Apache HBase的CSV数据批量导入与操作工具 项目简介 ... 插入数据到HBase表中。 通过HBase Shell进行数据查询和操作。 3. 配置与连接管理 通过配置文件管理HBase的连接信息,包括Zookeeper地址等。

    Hbase实验报告.pdf

    同样地,为“lisi”插入数据。 ### 4. 查询数据 查询HBase中的数据通常使用`get`命令。例如,查询“zhangsan”的地址: ```shell get 'student', 'zhangsan', 'info:address' ``` 查询“lisi”的“Hadoop”成绩: ...

    spark读取hbase数据,并使用spark sql保存到mysql

    使用spark读取hbase中的数据,并插入到mysql中

    Hive、MySQL、HBase数据互导

    5. **HBase Java API编程**:掌握如何使用Java编程接口与HBase交互,包括创建表、插入数据、查询数据等操作。 6. **Eclipse开发工具使用**:熟悉Eclipse环境下的Java项目配置和调试,以便编写和测试HBase的Java程序...

    HbaseTemplate 操作hbase

    例如,`put`方法用于向表中写入新的数据,`delete`方法根据行键删除一行,`update`方法则可以更新已有数据。 6. **事务管理**:HbaseTemplate也支持HBase的事务管理。尽管HBase自身并不完全支持ACID事务,但Hbase...

    【HBase企业应用开发】工作中自己总结的Hbase文档,非常全面!

    HBaseshell的基本用法包括创建表和列族、插入数据、按设计的表结构插入值、根据键值查询数据、扫描所有数据、删除指定数据、修改表结构、统计行数、执行disable和enable操作、表的删除以及hbaseshell脚本的使用。...

    hbase导入测试数据集

    在Java代码中,可以创建`Table`对象,然后使用`put`方法插入数据,最后调用`flushCommits`方法提交更改。 在测试环境中,导入数据集有助于验证系统功能、性能和容错性。对于ORDER_INFO表,我们可以测试不同查询场景...

    大数据处理:HBASE.ppt

    【大数据处理:HBASE.ppt】的文档主要探讨了从关系型数据库(RDBMS)向分布式非关系型数据库HBase的转换,以及HBase在大数据处理中的表设计、操作方式和最佳实践。 1. **从RDBMS到HBase的表设计转变** - RDBMS基于...

Global site tag (gtag.js) - Google Analytics