HHeLiBeXの日記 正道編

日々の記憶の記録とメモ‥

Hypertableを使ってみる

せっかくHypertableをセットアップしたので、書籍*1に載っているWebページの例を実際に乗せてみた。
まずはサーバー起動などの準備。必要に応じて行う。

$ ./bin/start-all-servers.sh local
Successfully started DFSBroker (local)
Successfully started Hyperspace
Successfully started Hypertable.Master
Successfully started Hypertable.RangeServer
$ ./bin/hypertable
Welcome to the HQL command interpreter.

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

hypertable> 

準備ができたら、実際のテーブル生成とデータ投入。今回はINSERT文で1件ずつ投入したが、ファイルからロードすることも可能。詳しくはチュートリアルを参照*2

hypertable> CREATE TABLE WebPages(contents MAX_VERSIONS=5, anchor);
hypertable> SHOW CREATE TABLE WebPages;

CREATE TABLE WebPages (
  anchor,
  contents MAX_VERSIONS=5,
  ACCESS GROUP default (anchor, contents)
)

hypertable> DESCRIBE TABLE WebPages;
<Schema generation="1">
  <AccessGroup name="default">
    <ColumnFamily id="1">
      <Name>anchor</Name>
    </ColumnFamily>
    <ColumnFamily id="2">
      <Name>contents</Name>
      <MaxVersions>5</MaxVersions>
    </ColumnFamily>
  </AccessGroup>
</Schema>

hypertable> 

テーブルを作成する時に指定するのはカラムファミリーの名前。ここでは'contents'と'anchor'がそれに該当する。また、MAX_VERSIONSを指定すると、タイムスタンプがの値が大きいものから指定した個数分だけのデータを保持できる。
このテーブルに実際にデータを入れてみる。まずはcontentsカラムファミリーだけ。

hypertable> INSERT INTO WebPages VALUES ('2008-09-06 00:00:00', 'google.com', 'contents', '<html>...9/6...</html>');
hypertable> INSERT INTO WebPages VALUES ('2008-09-13 00:00:00', 'google.com', 'contents', '<html>...9/13...</html>');
hypertable> INSERT INTO WebPages VALUES ('2008-09-20 00:00:00', 'google.com', 'contents', '<html>...9/20...</html>');
hypertable> INSERT INTO WebPages VALUES ('2008-09-23 00:00:00', 'google.com', 'contents', '<html>...9/23...</html>');
hypertable> INSERT INTO WebPages VALUES ('2008-09-26 00:00:00', 'google.com', 'contents', '<html>...9/26...</html>');
hypertable> INSERT INTO WebPages VALUES ('2008-09-27 00:00:00', 'google.com', 'contents', '<html>...9/27...</html>');
hypertable> SELECT * FROM WebPages DISPLAY_TIMESTAMPS;
2008-09-27 00:00:00.000000000	google.com	contents	<html>...9/27...</html>
2008-09-26 00:00:00.000000000	google.com	contents	<html>...9/26...</html>
2008-09-23 00:00:00.000000000	google.com	contents	<html>...9/23...</html>
2008-09-20 00:00:00.000000000	google.com	contents	<html>...9/20...</html>
2008-09-13 00:00:00.000000000	google.com	contents	<html>...9/13...</html>
hypertable> 

わざとタイムスタンプが過去のものから順に挿入している。MAX_VERSIONSが5なので、6つ目を挿入すると最初に挿入したものが削除される。
ここで、削除されたものと同じデータを再度挿入しようとしてみる。

hypertable> INSERT INTO WebPages VALUES ('2008-09-06 00:00:00', 'google.com', 'contents', '<html>...9/6...</html>');
hypertable> SELECT * FROM WebPages DISPLAY_TIMESTAMPS;
2008-09-27 00:00:00.000000000	google.com	contents	<html>...9/27...</html>
2008-09-26 00:00:00.000000000	google.com	contents	<html>...9/26...</html>
2008-09-23 00:00:00.000000000	google.com	contents	<html>...9/23...</html>
2008-09-20 00:00:00.000000000	google.com	contents	<html>...9/20...</html>
2008-09-13 00:00:00.000000000	google.com	contents	<html>...9/13...</html>
hypertable> 

しかし、タイムスタンプが新しいものから順に保持されるので、何も変化なし。
今度はタイムスタンプを指定しないで挿入してみる。

hypertable> INSERT INTO WebPages VALUES ('google.com', 'contents', '<html>...9/6...</html>');
hypertable> SELECT * FROM WebPages DISPLAY_TIMESTAMPS;
2008-09-27 17:09:56.942379001	google.com	contents	<html>...9/6...</html>
2008-09-27 00:00:00.000000000	google.com	contents	<html>...9/27...</html>
2008-09-26 00:00:00.000000000	google.com	contents	<html>...9/26...</html>
2008-09-23 00:00:00.000000000	google.com	contents	<html>...9/23...</html>
2008-09-20 00:00:00.000000000	google.com	contents	<html>...9/20...</html>
hypertable> 

すると、タイムスタンプには「現在の時刻」が使用され、今回のデータの中では一番新しいタイムスタンプとなるので、データが挿入される。
次は、anchorカラムファミリーを使って、カラムファミリーの中の列名を指定して挿入してみる。

hypertable> INSERT INTO WebPages VALUES('google.com', 'anchor:example.com', 'Google');
hypertable> INSERT INTO WebPages VALUES('google.com', 'anchor:example.jp', 'グ ーグル');
hypertable> SELECT * FROM WebPages DISPLAY_TIMESTAMPS;
2008-09-27 17:11:42.929682001	google.com	anchor:example.com	Google
2008-09-27 17:12:31.102753001	google.com	anchor:example.jp	グーグル
2008-09-27 17:09:56.942379001	google.com	contents	<html>...9/6...</html>
2008-09-27 00:00:00.000000000	google.com	contents	<html>...9/27...</html>
2008-09-26 00:00:00.000000000	google.com	contents	<html>...9/26...</html>
2008-09-23 00:00:00.000000000	google.com	contents	<html>...9/23...</html>
2008-09-20 00:00:00.000000000	google.com	contents	<html>...9/20...</html>
hypertable> 

SELECT節に"*"を指定すると、すべてのカラムファミリーのデータが表示されるが、特定のカラムファミリーに限定して取得することも可能。

hypertable> SELECT contents FROM WebPages DISPLAY_TIMESTAMPS;
2008-09-27 17:09:56.942379001	google.com	contents	<html>...9/6...</html>
2008-09-27 00:00:00.000000000	google.com	contents	<html>...9/27...</html>
2008-09-26 00:00:00.000000000	google.com	contents	<html>...9/26...</html>
2008-09-23 00:00:00.000000000	google.com	contents	<html>...9/23...</html>
2008-09-20 00:00:00.000000000	google.com	contents	<html>...9/20...</html>
hypertable> SELECT anchor FROM WebPages DISPLAY_TIMESTAMPS;
2008-09-27 17:11:42.929682001	google.com	anchor:example.com	Google
2008-09-27 17:12:31.102753001	google.com	anchor:example.jp	グーグル
hypertable> 

行キーの値が一つしかないのでおもしろくないが、行キーの値で絞り込むことも可能。

hypertable> SELECT anchor FROM WebPages WHERE ROW = 'google.com' DISPLAY_TIMESTAMPS;
2008-09-27 17:11:42.929682001	google.com	anchor:example.com	Google
2008-09-27 17:12:31.102753001	google.com	anchor:example.jp	グーグル
hypertable>