お試しBulbflow

昨日、「第1回 GraphDB 勉強会 in Tokyo」をusterm中継をしていて、[twitter:@bibrost]さんがbulbflowというRexster経由でGraphDBにアクセスできる
Pythonのライブラリが紹介されていたので、Quickstartを読んで試しに初めてみたけど、はまったのでメモしました。

RexsterについてはここからPackageがダウンロードできます。

あらじめJVM環境をインストールしておく。 ubuntu

$ sudo vim /etc/apt/sources.list
deb http://archive.canonical.com/ubuntu maverick partner

$ sudo apt-get update
$ sudo apt-get install sun-java6-jdk 
$ java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

Rexsterをパッケージからダウンロード

$ cd ~/src
$ wget https://github.com/downloads/tinkerpop/rexster/rexster-0.6.zip
$ unzip rexster-0.6.zip

bulbflowのセットアップ

$ cd ~/work
$ mkdir graph
$ cd graph
$ virtualenv env
$ . env/bin/activate
$ pip install bulbs

試しに、標準で入っているデータベースのtinkergraphにアクセスしてみる。

まずは、curlを使ってrexsterからデータが取得できるか試す。
RexsterのREST-APIは以下を参照。
https://github.com/tinkerpop/rexster/wiki/Basic-REST-API

$ cd ~/src/rexster-0.6
$ ./rexster.sh -s
$ curl -s -X GET http://localhost:8182/graphs/tinkergraph
{"version":"0.6","name":"tinkergraph","graph":"tinkergraph[vertices:6 edges:6 directory:data\/graph-example-1]","readOnly":false,"type":"com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph","queryTime":4.819085,"upTime":"0[d]:00[h]:00[m]:16[s]","extensions":[{"title":"evaluate an ad-hoc Gremlin script for a graph.","method":"GET","href":"tp\/gremlin"},{"title":"evaluate an ad-hoc Gremlin script for a graph.","method":"POST",

PythonのIntractive Modeで確認

$ python
>>> from bulbs.graph import Graph
>>> g = Graph()
>>> g.V
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/bulbs/graph.py", line 63, in V
    return list(vertices)
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/bulbs/gremlin.py", line 64, in query
    resp = self._query(script,*classes,**kwds)
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/bulbs/gremlin.py", line 125, in _query
    resp = self.resource.get(self.base_target,params)
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/bulbs/rest.py", line 43, in get
    return self.request("GET",target,params)
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/bulbs/rest.py", line 92, in request
    resp = Response(http.request(url, method, body, headers))
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/httplib2/__init__.py", line 1436, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/httplib2/__init__.py", line 1188, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/httplib2/__init__.py", line 1123, in _conn_request
    conn.connect()
  File "/home/saicologic/work/graph/env/lib/python2.6/site-packages/httplib2/__init__.py", line 796, in connect
    raise socket.error, msg
socket.error: [Errno 111] Connection refused

うーん。エラーになった。よくよく確認すると
http://bulbflow.com/docs/quickstart/#rexster
で書かれている、http://localhost:8182/tinkergraph
がそもそもあやしい。パスが違う。RexstarのCHANGELOGを確認。

https://github.com/tinkerpop/rexster/blob/master/CHANGELOG.textile

The REST URI scheme has changed slightly to include a graphs segment, as in: http://localhost:8182/graphs/tinkergraph

どうやら0.6でパスが変わったみたいだ。

試しに、ヴァージョンを落として0.5で試す

$ cd ~src
$ wget https://github.com/downloads/tinkerpop/rexster/rexster-0.5.zip
$ unzip rexster-0.5.zip
$ cd rexster-0.5
$ ./rexster-start.sh

curlで試す

curl -s -X GET http://localhost:8182/tinkergraph
{"version":"0.5","name":"tinkergraph","graph":"tinkergraph[vertices:6 edges:6]","readOnly":false,"type":"com.tinkerpop.blueprints.pgm.impls.tg.TinkerGraph","queryTime":0.428157,"upTime":"0[d]:00[h]:00[m]:24[s]","extensions":[{"title":"evaluate an ad-hoc Gremlin script for a graph.","method":"GET","href":"tp\/gremlin"},{"title":"evaluate an ad-hoc Gremlin script for a graph.","method":"POST","href":"tp\/gremlin"}]}(env)saic

OK。次にpythonのIntractiveModeで試します。

$ python
>>> from bulbs.graph import Graph
>>> g = Graph()
>>> g.V
[{u'lang': u'java', u'_type': u'vertex', u'_id': u'3', u'name': u'lop'}, {u'_type': u'vertex', u'age': 27, u'_id': u'2', u'name': u'vadas'}, {u'_type': u'vertex', u'age': 29, u'_id': u'1', u'name': u'marko'}, {u'_type': u'vertex', u'age': 35, u'_id': u'6', u'name': u'peter'}, {u'lang': u'java', u'_type': u'vertex', u'_id': u'5', u'name': u'ripple'}, {u'_type': u'vertex', u'age': 32, u'_id': u'4', u'name': u'josh'}]

うまくいった。結果からbulbflowがRexster0.6に対応していないことがわかるのでcommitログを確認。

どうやら最近0.6に対応したようです。
https://github.com/espeed/bulbs/commit/9150dae4f5b6386bdcb4cfcf43f38e4e78eb4f15

added keepalive, updated to Rexster 0.6


ということで、githubから最新のコードをとってきます。

$ pip uninstall bulbs
$ pip install -e git+git://github.com/espeed/bulbs.git#egg=Package

pythonのIntractiveModeで試します。

>>> from bulbs.graph import Graph
>>> g = Graph()
>>> g.V
[{u'lang': u'java', u'_type': u'vertex', u'_id': u'3', u'name': u'lop'}, {u'_type': u'vertex', u'age': 27, u'_id': u'2', u'name': u'vadas'}, {u'_type': u'vertex', u'age': 29, u'_id': u'1', u'name': u'marko'}, {u'_type': u'vertex', u'age': 35, u'_id': u'6', u'name': u'peter'}, {u'lang': u'java', u'_type': u'vertex', u'_id': u'5', u'name': u'ripple'}, {u'_type': u'vertex', u'age': 32, u'_id': u'4', u'name': u'josh'}]

できましたー。

もう一度[twitter:@bibrost]のプレゼンテーションを確認すると、
お話の中でさらっと

0.6には対応していないので0.5をつかってください。

とおしゃっていたのを聞きのがしていたので、はまってしまいました。
でも結果的に0.6に対応できたので良かったです。
次回は、bulbflowを使った細かい紹介をしたいと思います。