HBase教程

HBase 创建数据

使用 HBase Shell 插入数据

本章演示如何在 HBase 表中创建数据。在 HBase 表中创建数据,使用以下命令和方法:
put 命令, add() Put 类的方法,以及 put() HTable 类的方法。
例如,我们将在 HBase 中创建下表。
HBase 表
使用 put 命令,您可以向表中插入行。其语法如下:
put ’<table name>’,’row1’,’<colfamily:colname>’,’<value>’

插入第一行

让我们将第一行值插入到 emp 表中,如下所示。
hbase(main):005:0> put 'emp','1','personal data:name','raju'
0 row(s) in 0.6600 seconds
hbase(main):006:0> put 'emp','1','personal data:city','hyderabad'
0 row(s) in 0.0410 seconds
hbase(main):007:0> put 'emp','1','professional
data:designation','manager'
0 row(s) in 0.0240 seconds
hbase(main):007:0> put 'emp','1','professional data:salary','50000'
0 row(s) in 0.0240 seconds
以同样的方式使用 put 命令插入剩余的行。如果你插入整个表格,你会得到如下输出。
hbase(main):022:0> scan 'emp'
   ROW                        COLUMN+CELL
1 column=personal data:city, timestamp=1417524216501, value=hyderabad
1 column=personal data:name, timestamp=1417524185058, value=ramu
1 column=professional data:designation, timestamp=1417524232601,
 value=manager
 
1 column=professional data:salary, timestamp=1417524244109, value=50000
2 column=personal data:city, timestamp=1417524574905, value=chennai
2 column=personal data:name, timestamp=1417524556125, value=ravi
2 column=professional data:designation, timestamp=1417524592204,
 value=sr:engg
 
2 column=professional data:salary, timestamp=1417524604221, value=30000
3 column=personal data:city, timestamp=1417524681780, value=delhi
3 column=personal data:name, timestamp=1417524672067, value=rajesh
3 column=professional data:designation, timestamp=1417524693187,
value=jr:engg
3 column=professional data:salary, timestamp=1417524702514,
value=25000 

使用 Java API 插入数据

您可以使用 Put 类的 add() 方法将数据插入到Hbase 中。您可以使用 HTable 类的 put() 方法保存它。这些类属于 org.apache.hadoop.hbase.client 包。下面给出了在 HBase 的 Table 中创建数据的步骤。

第一步:实例化配置类

Configuration 类将 HBase 配置文件添加到其对象中。您可以使用 HbaseConfiguration 类的 create() 方法创建配置对象,如下所示。
Configuration conf = HbaseConfiguration.create();

第 2 步:实例化 HTable 类

您有一个名为 HTable 的类,它是 HBase 中 Table 的实现。此类用于与单个 HBase 表进行通信。在实例化这个类时,它接受配置对象和表名作为参数。您可以实例化 HTable 类,如下所示。
HTable hTable = new HTable(conf, tableName);

第 3 步:实例化 PutClass

要将数据插入到 HBase 表中,需要使用 add() 方法及其变体。该方法属于 Put,因此实例化put类。这个类需要你想要插入数据的行名,字符串格式。您可以实例化 Put 类,如下所示。
Put p = new Put(Bytes.toBytes("row1"));

第 4 步:插入数据

Put 类的 add() 方法用于插入数据。它需要 3 个字节的数组,分别表示列族、列限定符(列名)和要插入的值。使用 add() 方法将数据插入到 HBase 表中,如下所示。
p.add(Bytes.toBytes("coloumn family "), Bytes.toBytes("column
name"),Bytes.toBytes("value"));

步骤 5:将数据保存在表中

插入需要的行后,通过将put实例添加到HTable类的 put()方法中保存更改,如下所示。
hTable.put(p); 

第六步:关闭HTable实例

在 HBase Table 中创建数据后,使用 close() 方法关闭 HTable 实例,如下所示。
hTable.close(); 
下面给出了在 HBase 表中创建数据的完整程序。
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class InsertData{
   public static void main(String[] args) throws IOException {
      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();
      // Instantiating HTable class
      HTable hTable = new HTable(config, "emp");
      // Instantiating Put class
      // accepts a row name.
      Put p = new Put(Bytes.toBytes("row1")); 
      // adding values using add() method
      // accepts column family name, qualifier/row name ,value
      p.add(Bytes.toBytes("personal"),
      Bytes.toBytes("name"),Bytes.toBytes("raju"));
      p.add(Bytes.toBytes("personal"),
      Bytes.toBytes("city"),Bytes.toBytes("hyderabad"));
      p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),
      Bytes.toBytes("manager"));
      p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),
      Bytes.toBytes("50000"));
      
      // Saving the put Instance to the HTable.
      hTable.put(p);
      System.out.println("data inserted");
      
      // closing HTable
      hTable.close();
   }
}
编译并执行上述程序,如下所示。
$javac InsertData.java
$java InsertData
以下应该是输出:
data inserted
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4