{
"zzzzz" : "woot",
"xyz" : "hello",
"aaaab" : "world",
"1" : "x",
"aaaaa" : "y"
}
{
"1" : "x",
"aaaaa" : "y",
"aaaab" : "world",
"xyz" : "hello",
"zzzzz" : "woot"
}
{
"1" : {
"A" : "x",
"B" : "z"
},
"aaaaa" : {
"A" : "y",
"B" : "w"
},
"aaaab" : {
"A" : "world",
"B" : "ocean"
},
"xyz" : {
"A" : "hello",
"B" : "there"
},
"zzzzz" : {
"A" : "woot",
"B" : "1337"
}
}
{
// ...
"aaaaa" : {
"A" : {
"foo" : {
15 : "y",
4 : "m"
},
"bar" : {
15 : "d",
}
},
"B" : {
"" : {
6 : "w"
3 : "o"
1 : "w"
}
}
},
// ...
}
Row Key | Timestamp | ColumnFamily contents | ColumnFamily anchor | ColumnFamily people |
“com.cnn.www” | t9 | anchor:cnnsi.com = “CNN” | ||
“com.cnn.www” | t8 | anchor:my.look.ca = “CNN.com” | ||
“com.cnn.www” | t6 | contents:html = “…” | ||
“com.cnn.www” | t5 | contents:html = “…” | ||
“com.cnn.www” | t3 | contents:html = “…” |
{
"com.cnn.www": {
contents: {
t6: contents:html: "<html>..."
t5: contents:html: "<html>..."
t3: contents:html: "<html>..."
}
anchor: {
t9: anchor:cnnsi.com = "CNN"
t8: anchor:my.look.ca = "CNN.com"
}
people: {}
}
"com.example.www": {
contents: {
t5: contents:html: "<html>..."
}
anchor: {}
people: {
t5: people:author: "John Doe"
}
}
}
{
"com.cnn.www": {
contents: {
t6: contents:html: "<html>..."
t5: contents:html: "<html>..."
t3: contents:html: "<html>..."
}
anchor: {
t9: anchor:cnnsi.com = "CNN"
t8: anchor:my.look.ca = "CNN.com"
}
people: {}
}
"com.example.www": {
contents: {
t5: contents:html: "<html>..."
}
anchor: {}
people: {
t5: people:author: "John Doe"
}
}
}
尽管在概念视图里,table 可以被看成是一个稀疏的 row 的集合。但在物理上,它的是按照 column family 存储的。新的 column qualifier (column_family:column_qualifier)可以随时添加进已有的 column family 。
下表是一个 ColumnFamily anchor
:
Row Key | Timestamp | Column Family anchor |
“com.cnn.www” | t9 | anchor:cnnsi.com = “CNN” |
“com.cnn.www” | t8 | anchor:my.look.ca = “CNN.com” |
contents
:
Row Key | Timestamp | ColumnFamily contents: |
“com.cnn.www” | t6 | contents:html = “…” |
“com.cnn.www” | t5 | contents:html = “…” |
“com.cnn.www” | t3 | contents:html = “…” |
contents:html
,他的结果就是空。相似的,若请求为获取 t9 时间的
anchor:my.look.ca
,结果也是空。但是,如果不指明 timestamp,将会返回最新时间的 column。例如,如果请求为获取行键为“com.cnn.www”,没有指明 timestamp 的话,返回的结果是 t6 下的
contents:html
,t9下的
anchor:cnnsi.com
和 t8 下
anchor:my.look.ca
。
四个主要的数据模型操作是 Get、Put、Scan 和 Delete。通过 Table 实例进行操作。有关 Table 的 API 可以参见 http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Table.html。
Get
Get 返回特定 row 的属性。 Get 通过 Table.get 执行。有关 Get 的 API 可以参见 http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Get.html。
Put
Put 要么向 table 增加新 row(如果 key 是新的)或更新 row(如果 key 已经存在)。 Put 通过 Table.put(writeBuffer)或 Table.batch(非 writeBuffer)执行。有关 Put 的 API 可以参见 http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Put.html。
Scan
Scan 允许多个 row 特定属性迭代。
下面是一个在 Table 表实例上的 Scan 示例。假设 table 有几行 row key 为“row1”、“row2”、“row3”,还有一些 row key 值为“abc1”、 “abc2” 和“abc3”。下面的示例展示 Scan 实例如何返回“row”打头的 row。
public static final byte[] CF = "cf".getBytes();
public static final byte[] ATTR = "attr".getBytes();
...
Table table = ... // instantiate a Table instance
Scan scan = new Scan();
scan.addColumn(CF, ATTR);
scan.setRowPrefixFilter(Bytes.toBytes("row"));
ResultScanner rs = table.getScanner(scan);
try {
for (Result r = rs.next(); r != null; r = rs.next()) {
// process result...
}
} finally {
rs.close(); // always close the ResultScanner!
}
注意,通常最简单的方法来指定用于 scan 停止点是采用 InclusiveStopFilter 类,其 API 可以参见 http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/filter/InclusiveStopFilter.html。
Delete
Delete 用于从 table 中删除 row。Delete 通过 Table.delete 执行。有关 Delete 的 API 可以参见 http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Delete.html。
HBase 没有修改数据的合适方法。所以 delete 通过创建名为 tombstones 的新标志进行处理。这些 tombstones 和死去的值,会在 major compaction 时清除掉。
参考引用
- http://hbase.apache.org/book.html
- Shashwat Shriparv.Learning Hbase中文版.周彦伟,蒲聪,娄帅译.北京:电子工业出版社,2015
- Lars George.HBase: The Definitive Guide, 2nd Edition.Sebastopol:O’Reilly Media,2016
- http://jimbojw.com/wiki/index.php?title=Understanding_Hbase_and_BigTable
***********************
心得之谈:欢迎指正,一起学习。
***********************