LoginSignup
1
1

More than 5 years have passed since last update.

Groonga読書会3 参加レポート

Last updated at Posted at 2014-07-24

概要

2014-07-23 に開催されたGroonga読書会3の参加レポートです。

Groongaの公式ドキュメントを輪読形式で進めていき、Groongaに対する理解を深めるのが目的です。

内容

今回はドキュメントの「4.2. リモートアクセス」〜 「4.3.8. ベクターカラム」の範囲でした。

4.2. リモートアクセス

Groongaのアクセスには以下の3種類があります

  • Groonga専用プロトコル GQTP
  • memcachedバイナリプロトコル
  • HTTP

4.2.1.1. GQTPサーバの起動

Groonga専用のプロトコルです。

-sパラメータは Serverの'S'だそうです。

[root@vagrant-centos65 ~]# groonga -s /var/lib/groonga/db/test.db
^C
[root@vagrant-centos65 ~]# groonga -d /var/lib/groonga/db/test.db
2089
[root@vagrant-centos65 ~]# groonga -c
> status
[[0,1406198271.94556,0.0115501880645752],{"alloc_count":158,"starttime":1406198237,"uptime":34,"version":"4.0.2","n_queries":0,"cache_hit_rate":0.0,"command_version":1,"default_command_version":1,"max_command_version":2}]
> shutdown
[[0,1406198293.45464,0.000202417373657227],true]

4.2.2. memcachedバイナリプロトコル

memcached のバイナリプロトコルが使える。
しかし、固定のテーブル名、カラム名でしか使えないので、完全に専用にしか使えない。
なのでそれほど使いやすくはないとの事でした。

[root@vagrant-centos65 ~]# groonga -d --protocol memcached /var/lib/groonga/db/test.db
2094
[root@vagrant-centos65 ~]# groonga -c
> status
[[0,1406198354.59759,0.000953435897827148],{"alloc_count":158,"starttime":1406198342,"uptime":12,"version":"4.0.2","n_queries":0,"cache_hit_rate":0.0,"command_version":1,"default_command_version":1,"max_command_version":2}]
> shutdown
[[0,1406198357.02855,0.000128030776977539],true]

4.2.3. HTTP

—-protocolはDBの指定よりも前に書かないと認識しない。

[root@vagrant-centos65 ~]# groonga --protocol http -d /var/lib/groonga/db/test.db
1951
[root@vagrant-centos65 ~]# curl http://localhost:10041/d/status
[[0,1406115804.30593,0.00733184814453125],{"alloc_count":159,"starttime":1406115788,"uptime":16,"version":"4.0.3","n_queries":0,"cache_hit_rate":0.0,"command_version":1,"default_command_version":1,"max_command_version":2}]

4.2.4. ブラウザベースの管理ツール

管理画面では様々なことができる。
テーブル作成なども可能。
次のリリースから英語がデフォルトになるとの事でした。

4.3. いろいろなデータの保存

4.3.2. 真偽値

> table_create --name ToyBox --flags TABLE_HASH_KEY --key_type ShortText
[[0,1406116021.5421,0.00503277778625488],true]
> column_create --table ToyBox --name is_animal --type Bool
[[0,1406116104.50107,0.00287890434265137],true]
> load --table ToyBox
[
{"_key":"Monkey","is_animal":true}
{"_key":"Flower","is_animal":false}
{"_key":"Block"}
]
> > > > > [[0,1406116121.91809,0.00100255012512207],3]
> select --table ToyBox --output_columns _key,is_animal
[[0,1406116129.92308,0.000800371170043945],[[[3],[["_key","ShortText"],["is_animal","Bool"]],["Monkey",true],["Flower",false],["Block",false]]]]
>
> select --table ToyBox --output_columns _key,is_animal
[[0,1406116259.48107,6.55651092529297e-05],[[[3],[["_key","ShortText"],["is_animal","Bool"]],["Monkey",true],["Flower",false],["Block",false]]]]
>

4.3.3. 数値

> column_create --table ToyBox --name price --type Int8
[[0,1406116401.98101,0.00216436386108398],true]
> column_create --table ToyBox --name weight --type Float
[[0,1406116408.71905,0.00391960144042969],true]
> load --table ToyBox
[
{"_key":"Monkey","price":15.9}
{"_key":"Flower","price":200,"weight":0.13}
{"_key":"Block","weight":25.7}
]> > > > >
[[0,1406116415.87705,0.799067974090576],3]
> select --table ToyBox --output_columns _key,price,weight
[[0,1406116432.41787,0.000347375869750977],[[[3],[["_key","ShortText"],["price","Int8"],["weight","Float"]],["Monkey",15,0.0],["Flower",-56,0.13],["Block",0,25.7]]]]

4.3.4. 文字列

> column_create --table ToyBox --name name --type ShortText
[[0,1406116569.42604,0.00609278678894043],true]
> load --table ToyBox
[
{"_key":"Monkey","name":"Grease"}
{"_key":"Flower","name":"Rose"}
]
> > > > [[0,1406116577.4511,0.000348806381225586],2]
> select --table ToyBox --output_columns _key,name
[[0,1406116583.84304,0.000105619430541992],[[[3],[["_key","ShortText"],["name","ShortText"]],["Monkey","Grease"],["Flower","Rose"],["Block",""]]]]

4.3.5. 日時

> column_create --table ToyBox --name time --type Time
[[0,1406116756.22809,0.0036931037902832],true]
> load --table ToyBox
[
{"_key":"Flower","time":1234567890.1234569999}
{"_key":"Block","time":-1234567890}
]> > > >
[[0,1406116765.55907,0.909050226211548],2]
> select --table ToyBox --output_columns _key,time
[[0,1406116776.66106,0.000119447708129883],[[[3],[["_key","ShortText"],["time","Time"]],["Monkey",0.0],["Flower",1234567890.12346],["Block",-1234567890.0]]]]

{"_key":"Flower","time":1234567890.1234569999}
をやったら
["Flower",1234567890.12346]
と表示された

小数部と整数部を分けるので、その時に誤差が出ることがある。

内部的には小数部と整数部を分けて保持するような仕組みになっている。
しかし、小数を扱っている為、誤差が生じる可能性がある。
これは小数部と整数部を分ける前と、分けていたものを取り出して結合した時に起こりうる。

日付は文字列でも指定できる。
形式は以下のとおり。

YYYY/MM/DD hh:mm:ss.micro
YYYY-MM-DD hh:mm:ss.micro

文字列だとおそらく誤差がでない

4.3.6. 経緯度

> column_create --table ToyBox --name location --type WGS84GeoPoint
[[0,1406117441.35007,0.00436139106750488],true]
> load --table ToyBox
[
{"_key":"Monkey","location":"128452975x503157902"}
{"_key":"Block","location":"35.6813819x139.7660839"}
]> > > >
[[0,1406117448.4841,0.887007713317871],2]
> select --table ToyBox --output_columns _key,location
[[0,1406117455.81907,0.000102519989013672],[[[3],[["_key","ShortText"],["location","WGS84GeoPoint"]],["Monkey","128452975x503157902"],["Flower","0x0"],["Block","128452975x503157902"]]]]

WGS84GeoPoint
1984年に定義されたので84

基本的には度数表記を使えば良い
が、正確なのはミリ秒。内部もミリ秒

4.3.7. テーブル参照

> table_create --name Site --flags TABLE_HASH_KEY --key_type ShortText
[[0,1406118245.02305,0.00791382789611816],true]
> column_create --table Site --name title --type ShortText
[[0,1406118253.91505,0.00605010986328125],true]
> load --table Site
[
{"_key":"http://example.org/","title":"This is test record 1!"},
{"_key":"http://example.net/","title":"test record 2."},
{"_key":"http://example.com/","title":"test test record three."},
{"_key":"http://example.net/afr","title":"test record four."},
{"_key":"http://example.org/aba","title":"test test test record five."},
{"_key":"http://example.com/rab","title":"test test test test record six."},
{"_key":"http://example.net/atv","title":"test test test record seven."},
{"_key":"http://example.org/gat","title":"test test record eight."},
{"_key":"http://example.com/vdw","title":"test test record nine."},
]> > > > > > > > > > >
[[0,1406118262.86547,0.503674268722534],9]
> column_create --table Site --name link --type Site
[[0,1406118290.09701,0.00923919677734375],true]
> load --table Site
[{"_key":"http://example.org/","link":"http://example.net/"}]>
[[0,1406118297.02581,1.79354977607727],1]
> select --table Site --output_columns _key,title,link._key,link.title --query title:@this
[[0,1406118308.87306,0.00467777252197266],[[[1],[["_key","ShortText"],["title","ShortText"],["link._key","ShortText"],["link.title","ShortText"]],["http://example.org/","This is test record 1!","http://example.net/","test record 2."]]]]

IDは自動で振っているので、使うべきではない。_keyを使う。

4.3.8. ベクターカラム

> column_create --table Site --name links --flags COLUMN_VECTOR --type Site
[[0,1406118767.67214,0.00692200660705566],true]
> load --table Site
[{"_key":"http://example.org/","links":["http://example.net/","http://example.org/","http://example.com/"]}]>
[[0,1406118779.04815,1.05134391784668],1]
> select --table Site --output_columns _key,title,links._key,links.title --query title:@this
[[0,1406118794.95199,0.000702857971191406],[[[1],[["_key","ShortText"],["title","ShortText"],["links._key","ShortText"],["links.title","ShortText"]],["http://example.org/","This is test record 1!",["http://example.net/","http://example.org/","http://example.com/"],["test record 2.","This is test record 1!","test test record three."]]]]]

ベクターカラムの返却は、登録時の順番になる。
上書き更新しかできない。
(内部的には差し込み等もできるようになっているが、JSONで表現ができないので。)

問題点

TryGroonga でコピペができなかった。
Windowsでは問題なく出来た模様。MacだとChrome,FF共にNG。

所感

今回はメモをとる時間やコマンドを実際に実行してみる時間を多めに設けたため、全体的に進みは遅めでした。
ですが、その分理解が深まったように思います。
特にカラム型の説明などは、実際にコマンドを実行してみると、非常にわかりやすいと感じました。

今後、更にGroongaの動作周りの説明に入っていくので、今回のように自分でコマンドを実行し、実際の挙動を確認しながら進めるのが、より深い理解につながると思います。
また、実行結果したりしてみて、疑問に思った事をすぐに質問できるので、とても充実した時間となりました。

次回も是非参加したいと思います。

最後に、いつもお忙しい中主催していただいている須藤さん及びクリアコードの皆さん、ありがとうございます。

1
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1