级别: 中级 Gabriel Mateescu, 高级开发人员, Virginia Bioinformatics Institute at Virginia Tech
2009 年 10 月 19 日 Hadoop Database (HBase) 非常适于创建一个语义 Web 并提取现有知识或计算知识。学习如何在 HBase 数据库中为科学文章表示 RDF/XML 断言,了解 HBase 和 Bigtable 如何发起一种存储和处理数据的新方法。
HBase 是一个针对结构化数据的可伸缩、分布式和面向列的动态模式数据库。它能有效和可靠地管理分布在数千个商品服务器上的大规模数据(千兆兆字节或更多)。HBase 根据 Google 的 Bigtable 数据库建模,是 Apache Software Foundation 的 Hadoop 项目的子项目。
 |
常用缩略词
- API:应用程序编程接口
- DOI:数字对象标识符
- HTTP:超文本传输协议
- REST:具象状态传输
- SQL:结构化查询语言
- URI:统一资源标识符
- XML:可扩展标记语言
|
|
注意:在撰写本文时,HBase 的最新版本是 V0.19.3。本文提供的信息适用于这个版本。
HBase 数据模型
HBase 数据被建模为多维映射,其中值(表单元)通过 4 个键索引:
value = Map(TableName, RowKey, ColumnKey, Timestamp)
|
其中:
TableName 是一个字符串。
RowKey 和 ColumnKey
是二进制值(Java 类型 byte[])。
Timestamp 是一个 64 位整数(Java 类型 long)。
value 是一个未解释的字节数组(Java™ 类型 byte[])。
二进制数据被编码为 Base64,以便通过网络传输。
行键是表的主键,通常是一个字符串。行通过行键按字典顺序排序。
存储在表中的信息的结构为列族(column family),您可以将这种结构视为类别。每个列族可以拥有任意数量的成员,它们通过标签(或修饰符)识别。column 键由族名、: 号和标签组成。例如,对于系列 info 和成员 date,列键为 info:date。
一个 HBase 表模式定义多个列族,但当您向表中插入一行时,应用程序能够在运行时创建新成员。对于一个列族,表中的不同行可以拥有不同数量的成员。换句话说,HBase 支持一个动态模式 模型。
一个 HBase 表示例
表 1 展示了一个名为 Persons 的 HBase 表的简单示例,该表有两个列族:name 和 contact。
表 1. 带有两个列族的 Persons 表
| 行键 | 时间戳 | 列族 |
|---|
| name | contact |
|---|
| 000001 | t3 | | contact:http research.google.com/people/jeff/ | | t2 | name:first Jeffrey |
| | t1 | name:last Dean |
| | 000002 | t5 | name:first Gabriel |
| | t4 | name:last Mateescu |
|
一个空单元没有与单元的键相关联的值。在表 1 中,与键 (000002, contact:http, t4) 关联的单元为空。空单元不存储在 HBase 中,读取空单元类似于根据不存在的键从映射提取值。HBase 表以这种方法适应稀疏的 行。
对于任意行,一次只能访问一个列族的一个成员(这与关系数据库不同,在关系数据库中,一个查询可以访问来自一个行中的多个列的单元)。您可以将一个行中的一个列族的成员视为子行。
表被分解为多个表区域,等同于 Bigtable 片(tablet)。一个区域包含某个范围中的行。将一个表分解为多个区域是高效处理大型表的关键机制。
RDF 和语义 Web
考虑一下这个问题:如何表示关于科学文章的信息?文章和它们的作者是属于资源。在资源描述框架(Resource Description Framework,RDF)中,关于资源的知识通过断言(参见 参考资料)表示,其中断言是一个三元组:
(subject, predicate, object)
|
谓语定义主语(断言引用的资源)和宾语之间的关系。例如,句子 “The article has the title Bigtable” 可以表示为:
(The article, has title, Bigtable)
|
断言的主语 是一个必须通过 URI 识别的资源。谓语 必须在词汇表中定义,以便它与词汇表的名称空间 URI 关联。断言的宾语 可以通过 URI 或文本 识别,如果它是另一个断言的主语,则它必须通过 URI 识别。
有关一篇文章的知识可以表达为断言,断言可以用 RDF/XML 格式表示。
一篇期刊文章的 RDF 描述
考虑这篇关于 Bigtable 的期刊文章:
F. Chang, J. Dean, S. Ghemawat, W. Hsieh, D. Wallach, M. Burrows,
T. Chandra, A. Fikes, and R. Gruber, "Bigtable: A Distributed Storage System for Structured Data",
ACM Trans. Comput. Syst. 26 (2), June 2008.
您可以通过一组句子描述这篇文章,例如:
- The Bigtable journal article has the title "Bigtable: A Distributed
Storage System for Structured Data."
- The Bigtable journal article is written by Fay Chang.
其中:
- The Bigtable journal article 是两个句子的主语。
- has the title 是第一个句子的谓语。
- “Bigtable: A Distributed Storage System for Structured Data” 是第一个句子的宾语。
- is written by 是第二个句子的谓语。
- Fay Chang 是第二个句子的宾语。
要用 RDF/XML 表示这些句子,必须在适当的名称空间中确定主语的 URI 和谓语的名称。对于文章 URI,使用 Bigtable 论文的 Digital Object Identifier (DOI) URI:http://doi.acm.org/10.1145/1365815.1365816。按照如下方式重新组织第一个句子:
The article with the URI "http://doi.acm.org/10.1145/1365815.1365816"
has the title "Bigtable: A Distributed Storage System for Structured Data."
对于谓语,使用来自表 2 中的词汇表的术语:
表 2. 名称空间 URI 和前缀
| 前缀 | 名称空间 URI | 描述 |
|---|
| rdf | http://www.w3.org/1999/02/22-rdf-syntax-ns# | RDF 词汇表术语 | | dc | http://purl.org/dc/elements/1.1/ | Dublin Core 元素 | | dcterms | http://purl.org/dc/terms/ | Dublin Core 术语 | | eprint | http://purl.org/eprint/terms/ | Eprints 术语 | | foaf | http://xmlns.com/foaf/0.1/ | FOAF 词汇表术语 |
根据这些词汇表,您可以以 RDF/XML 格式组织这个关于 Bigtable 的句子,如清单 1 所示:
清单 1. RDF/XML 格式的文章描述
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/" >
<rdf:Description rdf:about="http://doi.acm.org/10.1145/1365815.1365816">
<dc:title>Bigtable: A Distributed Storage System for Structured Data</title>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Fay_Chang"/>
</rdf:Description>
</rdf:RDF>
|
这里,谓语 <dc:title> 的宾语是一个文本,而 <dc:creator> 的宾语是一个 URI。
清单 2 显示了这篇文章的完整描述,包括所有作者、发表日期、出版商名称等信息,其中谓语 <dc:type> 定义文章类型。
清单 2. 完整的 RDF/XML 文章描述
$ cat rdf/Bigtable.xml
<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/" >
<rdf:Description rdf:about="http://doi.acm.org/10.1145/1365815.1365816">
<dc:title>Bigtable: A Distributed Storage System for Structured Data</title>
<dc:type>http://purl.org/eprint/type/JournalArticle</dc:type>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Fay_Chang"/>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Jeffrey_Dean"/>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Sanjay_Ghemawat"/>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Wilson_Hsieh"/>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Deborah_Wallach"/>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Mike_Burrows"/>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Tushar_Chandra"/>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Andrew_Fikes"/>
<dc:creator
rdf:resource="http://purl.org/sweb/Authors/google/research/Robert_Gruber"/>
<dc:publisher>ACM, New York, NY, USA</dc:publisher>
<dcterms:issued>2008-06</dcterms:issued>
<dc:subject>distributed databases</dc:subject>
<dcterms:isPartOf rdf:resource="urn:ISSN:0734-2071" />
<dcterms:bibliographicCitation>
ACM Trans. Comput. Syst., 26 (2) 26 pages (2008)
</dcterms:bibliographicCitation>
</rdf:Description>
</rdf:RDF>
|
我们使用 Friend of a Friend (FOAF) 词汇表描述作者。清单 3 显示关于主语 Jeffrey Dean 的断言。
清单 3. 一个作者的 RDF/XML 描述
$ cat rdf/Jeffrey.xml
<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:eprint="http://purl.org/eprint/terms/" >
<rdf:Description
rdf:about="http://purl.org/sweb/Authors/google/research/Jeffrey_Dean">
<foaf:Person>
<foaf:givenname>Jeffrey</foaf:givenname>
<foaf:family_name>Dean</foaf:family_name>
<foaf:homepage rdf:resource="http://research.google.com/people/jeff/" />
</foaf:Person>
<eprint:affiliatedInstitution>Google, Inc.</eprint:affiliatedInstitution>
</rdf:Description>
</rdf:RDF>
|
使用 HBase 建模一个语义 Web
要使用 HBase 建模一个语义 Web,第一步是将 RDF 映射到 HBase 表。要存储文章和作者的 RDF/XML 描述,需要创建两个表,分别命名为 Articles 和 Authors。设计这些表时要记住一点:您需要支持关于作者的从属关系的查询。
Articles 表的行键源自文章的 DOI。例如,Bigtable 论文的行键是 doi.org.acm_10.1145_1365815_1365816。这个模式拥有 3 个列族:
info 用于标题、出版物名称和出版日期等信息。
authors 用于作者的 URI。
affiliations 用于作者的从属关系。
Authors 表的行键源自作者的 URI。例如,Jeffrey Dean 的 URI(参见 清单 3)转换为键 google_research_Jeffrey_Dean。这个模式包含两个列族:info 用于存储作者信息,比如姓名和主页;affiliations 用于存储作者的从属关系历史。
创建 HBase 表
与 HBase 交互的一种方法是通过 REST API。使用清单 4 中显示的 HTTP 请求创建表。
清单 4. 创建 Articles 和 Authors 表
$ cat tables/Articles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<table>
<name>Articles</name>
<columnfamilies>
<columnfamily>
<name>info</name>
</columnfamily>
<columnfamily>
<name>authors</name>
</columnfamily>
<columnfamily>
<name>affiliations</name>
</columnfamily>
</columnfamilies>
</table>
$ cat tables/Authors.xml
<?xml version="1.0" encoding="UTF-8" ?>
<table>
<name>Authors</name>
<columnfamilies>
<columnfamily>
<name>info</name>
</columnfamily>
<columnfamily>
<name>affiliations</name>
</columnfamily>
</columnfamilies>
</table>
$ cat tables/Articles.xml | curl -X POST -T - http://localhost:60010/api/
$ cat tables/Authors.xml | curl -X POST -T - http://localhost:60010/api/
|
在表中插入数据
使用 “一篇期刊文章的 RDF 描述” 部分的信息填充 Authors 和 Articles 表。清单 5 显示如何将关于作者 Jeffrey Dean 的信息插入 Authors 表(值以 Base64 编码)。
清单 5. 填充 Authors 表
$ more rows/Jeffrey_Dean_info.xml
<?xml version="1.0" encoding="UTF-8" ?>
<column>
<name>info:name</name>
<value>SmVmZnJleSBEZWFuCg==</value>
</column>
$ more rows/Jeffrey_Dean_affiliation.xml
<?xml version="1.0" encoding="UTF-8" ?>
<column>
<name>affiliations:</name>
<value>R29vZ2xlCg==</value>
</column>
$ cat rows/Jeffrey_Dean_info.xml | \
curl -X POST -T - http://localhost:60010/api/Authors/row/google_research_Jeffrey_Dean
$ cat rows/Jeffrey_Dean_affiliation.xml | \
curl -X POST -T - http://localhost:60010/api/Authors/row/google_research_Jeffrey_Dean
|
对每个插入执行 POST 请求,省略时间戳,因为 HBase 分配默认时间戳。清单 6 显示如何使用关于 Bigtable 期刊文章的信息填充 Articles 表。
清单 6. 填充 Articles 表
$ more rows/Bigtable_info.xml
<?xml version="1.0" encoding="UTF-8" ?>
<column>
<name>info:title</name>
<value>QmlndGFibGU6IEEgRGlzdHJpYnV0ZWQgU3RvcmFnZSBTeXN0ZW0gZm9yIFN0cnVjdHVyZWQgRGF0
YQo==
</value>
</column>
$ more rows/Bigtable_author_2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<column>
<name>authors:2</name>
<value>SmVmZnJleSBEZWFuCg==</value>
</column>
$ cat rows/Bigtable_info.xml | curl -X POST \
-T - http://localhost:60010/api/Articles/row/doi.org.acm_10.1145_1365815_136581
$ cat rows/Bigtable_author_2.xml | curl -X POST \
-T - http://localhost:60010/api/Articles/row/doi.org.acm_10.1145_1365815_136581
|
挖掘表
填充 Authors 和 Articles 后,可以对数据执行批处理操作。这里,您将寻找这篇 Bigtable 论文的作者的从属关系。一个批处理流程将扫描表并从 Authors 表的相同列族中提取信息,从而在 articles 表的 affiliations 列族中构建信息。对于这篇 Bigtable 论文,批处理流程的操作等同于清单 7 中的代码,其中该论文的所有作者都从属于 Google。
清单 7. 将处理后的信息添加到 Articles 表
$ more rows/Bigtable_affiliations.xml
<?xml version="1.0" encoding="UTF-8" ?>
<column>
<name>affiliations:</name>
<value>R29vZ2xlCg==</value>
</column>
$ cat rows/Bigtable_affiliations.xml | curl -X POST \
-T - http://localhost:60010/api/Articles/row/doi.org.acm_10.1145_1365815_136581
|
要获取这篇 Bigtable 论文的作者的从属关系,在 Bigtable 论文行的 affiliations: 列上执行一个 GET 操作。
清单 8. 从 Articles 表提取信息
$ curl -X GET http://localhost:60010/api/Articles/row/\
doi.org.acm_10.1145_1365815_136581?column=affiliations:
<?xml version="1.0" encoding="UTF-8" ?>
<row>
<count>
1
</count>
<column>
<name>
YWZmaWxpYXRpb25zOg==
</name>
<value>
R29vZ2xlCg==
</value>
<timestamp>
1250049020108
</timestamp>
</column>
|
解码 Base64 值,affiliations: 变为 YWZmaWxpYXRpb25zOg==,Google 变为 R29vZ2xlCg==。
一个简单示例
您可以使用 HBase 解决比本文中讨论的问题更困难的问题。例如,您可以处理一个 “Jeopardy!” 线索(比如 “The
author of this journal article about Bigtable has worked for the World Health
Organization”)并形成响应。为此,创建一个名为 Keywords 的表,该表的行键是期刊文章的关键字。这个表包含一个列族 journal_articles,用于存储其中出现该关键字的文章的 DOI。
存储这篇 Bigtable 论文的关键字后,Keywords 表将包含一个行,该行的行键为 Bigtable,列键为 journal_articles:1,单元值为 doi.acm.org_10.1145_1365815_1365816。要完成这个小测验,执行以下操作:
- 在 Keywords 表中查询关键字
Bigtable,获取这篇论文的 DOI。
- 通过步骤 1 中获取的 DOI 在 Articles 表中查询这篇论文,获取作者的 URI。
- 通过 URI 在 Authors 表中查询作者,提取(现在和过去的)从属关系。
- 根据步骤 3 中获取的结果集,选择其列族
affiliations 中有一个 World Health Organization 成员的行。
这个响应是:“Who is Jeffrey Dean?”
结束语
HBase 和 Bigtable 提出了一种关于数据处理管道的新的思维方式。在单片系统中提取和变换数据的类 SQL 的流程被一种 “分而治之” 的方法取代,在这种方法中,数据库支持 Create、Read、Update、Delete(CRUD)操作,而复杂的数据转换则被委托给专门用于并行处理的外部组件。例如,MapReduce 应用程序可以完成并行处理,而一个分布式和复制式文件系统,如 Hadoop Distributed File System (HDFS) 或 Google File System,则可以实现高吞吐量。
在缺乏表连接时,HBase 通常使用去规范化(de-normalization)来将相关信息保存在一个表中。本文展示了这种方法。
尽管 HBase 的性能仍需改善,但它的确展示了成为主流解决方案的前景。
下载 | 描述 | 名字 | 大小 | 下载方法 |
|---|
| 样例 XML | os-hbase-source_hbase.zip | 11KB | HTTP |
|---|
参考资料 学习
获得产品和技术
讨论
关于作者  | |  | Gabriel Mateescu 为管理和执行数据和计算密集型应用程序(如生物信息学和高能物理模拟)构建分布式系统。他曾参与以下几个项目的工作:LHC Computing Grid、Distributed European Infrastructure for Supercomputing Applications (DEISA)、GridCanada 和 NIH MIDAS。 |
对本文的评价
|