Apache Tajo 与 HBase 的集成


Apache Tajo 支持 HBase 集成。这使我们能够访问 Tajo 中的 HBase 表。 HBase 是建立在 Hadoop 文件系统之上的分布式面向列的数据库。它是 Hadoop 生态系统的一部分,提供对 Hadoop 文件系统中数据的随机实时读/写访问。配置 HBase 集成需要执行以下步骤。

设置环境变量


将以下更改添加到“conf/tajo-env.sh”文件。

$ vi conf/tajo-env.sh  
# HBase home directory. It is opitional but is required mandatorily to use HBase. 
# export HBASE_HOME = path/to/HBase

包含 HBase 路径后,Tajo 会将 HBase 库文件设置为类路径。

创建外部表


使用以下语法创建外部表:

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] <table_name> [(<column_name> <data_type>, ... )] 
USING hbase WITH ('table' = '<hbase_table_name>' 
, 'columns' = ':key,<column_family_name>:<qualifier_name>, ...' 
, 'hbase.zookeeper.quorum' = '<zookeeper_address>' 
, 'hbase.zookeeper.property.clientPort' = '<zookeeper_client_port>') 
[LOCATION 'hbase:zk:// <主机名>:<端口>/'] ;

要访问 HBase 表,你必须配置表空间位置。

Here,

  • Table :设置hbase源表名。如果要创建外部表,则该表必须存在于 HBase 上。

  • Columns : Key 指 HBase 行键。条目的列数需要等于 Tajo 表的列数。

  • hbase.zookeeper.quorum : 设置zookeeper quorum地址。

  • hbase.zookeeper.property.clientPort :设置zookeeper客户端端口。

Query

CREATE EXTERNAL TABLE students (rowkey text,id int,name text) 
USING hbase WITH ('table' = 'students', 'columns' = ':key,info:id,content:name') 
LOCATION 'hbase:zk:// <主机名>:<端口>/';

在这里,Location path 字段设置了 zookeeper 客户端端口 ID。如果不设置端口,Tajo 会引用 hbase-site.xml 文件的属性。

在 HBase 中创建表


你可以使用“hbase shell”命令启动 HBase 交互式 shell,如以下查询所示。

Query

/bin/hbase shell 

Result

上述查询将生成以下结果。

hbase(main):001:0>

查询 HBase 的步骤

要查询 HBase,你应该完成以下步骤:

步骤 1 : 将以下命令通过管道传送到 HBase shell 以创建“教程”表。

Query

hbase(main):001:0> create ‘students’,{NAME => ’info’},{NAME => ’content’} 
put 'students', ‘row-01', 'content:name', 'Adam' 
put 'students', ‘row-01', 'info:id', '001' 
put 'students', ‘row-02', 'content:name', 'Amit' 
put 'students', ‘row-02', 'info:id', '002' 
put 'students', ‘row-03', 'content:name', 'Bob' 
put 'students', ‘row-03', 'info:id', ‘003' 

步骤 2 : 现在,在 hbase shell 中发出以下命令,将数据加载到表中。

main):001:0> cat ../hbase/hbase-students.txt | bin/hbase shell

步骤 3 : 现在,回到 Tajo shell,执行以下命令查看表的元数据:

default> \d students;  

table name: default.students 
table path: 
store type: HBASE 
number of rows: unknown 
volume: 0 B 
Options: 
    'columns' = ':key,info:id,content:name'
    'table' = 'students'

schema: 
rowkey  TEXT 
id  INT4 
name TEXT

步骤 4 : 要从表中获取结果,请使用以下查询:

Query

default> select * from students

Result

上述查询将获取以下结果:

rowkey,  id,  name 
------------------------------- 
row-01,  001,  Adam 
row-02,  002,  Amit 
row-03   003,  Bob