Hatena::ブログ(Diary)

Multiple Choices このページをアンテナに追加 RSSフィード

2011-10-09 お試しBulbflow このエントリーを含むブックマーク このエントリーのブックマークコメント

昨日、「第1回 GraphDB 勉強会 in Tokyo」をusterm中継をしていて、@さんが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'}]

できましたー。

もう一度@プレゼンテーションを確認すると、

お話の中でさらっと

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

とおしゃっていたのを聞きのがしていたので、はまってしまいました。

でも結果的に0.6に対応できたので良かったです。

次回は、bulbflowを使った細かい紹介をしたいと思います。

2010-07-14 MongoDB勉強会 このエントリーを含むブックマーク このエントリーのブックマークコメント

先週、株式会社万葉さんで開催された勉強会の発表資料です。

まだまだ、自分もMongoDB Level1くらいのスキルですが、

ぼちぼち人柱した結果を発表していきます。

2009-06-26 eventmachineのインストール失敗解決方法

eventmachineのインストール失敗解決方法

eventmachineのインストールでまれに見るエラーだったのでメモ

shell>gem install eventmachine
Building native extensions.  This could take a while...
ERROR:  Error installing eventmachine:
	ERROR: Failed to build gem native extension.

/usr/bin/ruby1.8 extconf.rb
checking for rb_trap_immediate in ruby.h,rubysig.h... yes
checking for rb_thread_blocking_region()... no
checking for inotify_init() in sys/inotify.h... yes
checking for writev() in sys/uio.h... yes
checking for sys/event.h... no
checking for sys/epoll.h... yes
checking for main() in -lcrypto... no
creating Makefile

make
g++ -I. -I. -I/usr/lib/ruby/1.8/i486-linux -I. -DBUILD_FOR_RUBY -DHAVE_RB_TRAP_IMMEDIATE -DHAVE_RBTRAP -DHAVE_INOTIFY_INIT -DHAVE_INOTIFY -DHAVE_WRITEV -DHAVE_WRITEV -DOS_UNIX -DHAVE_SYS_EPOLL_H -DHAVE_EPOLL -DWITHOUT_SSL -I/include/include  -D_FILE_OFFSET_BITS=64  -fPIC -fno-strict-aliasing -g -g -O2  -fPIC    -c em.cpp
make: g++: Command not found
make: *** [em.o] Error 127

make: g++: Command not found

shell>apt-get install g++
shell>gem install eventmachine
Building native extensions.  This could take a while...
Successfully installed eventmachine-0.12.8
1 gem installed
Installing ri documentation for eventmachine-0.12.8...
Installing RDoc documentation for eventmachine-0.12.8...

2009-06-23 [Wakame]Rails2.3.2+mysql_replication_adapterでDB負荷分散

ElasticLoadBalancerをWakameで使う(2/n)

前回のElasticLoadBalancerをWakameで使うの続きです

Wakameの次期バージョンではMysqlのSlaveが増やせるようになります。

それに伴い現在開発中のコードで、Rails2.3.2を使ってMysql-Slave構成の

アプリケーションのセットアップを行いました。

Wakame 0.3.x
Rails 2.3.2
Mysql 5.0.67-0ubuntu6-log
ELB   Amazon ElasticLoadBalancing CLI version 1.0.1.23 (API 2009-05-15)

1.まずはインスタンスを立ち上げるたときの初期状態

shell>/home/wakame/corelib/bin/wakameadm status

Cluster : Wakame::Service::WebCluster (0)
  Wakame::Service::Apache_LB : <current=1 min=1, max=1, type=ConstantCounter> 
  Wakame::Service::Apache_WWW : <current=1 min=1, max=5, type=ConstantCounter> 
  Wakame::Service::Apache_APP : <current=1 min=1, max=5, type=ConstantCounter> 
  Wakame::Service::MySQL_Slave : <current=1 min=1, max=5, type=ConstantCounter> 
  Wakame::Service::MySQL_Master : <current=1 min=1, max=1, type=ConstantCounter> 

Agents :
  i-8384a0ea : 10.252.46.68, 174.129.175.158 load=0.18, 10 sec(s) (1)

2.launch_clusterコマンドを実行。

この時点で1台のインスタンスの中で

MysqlMasterがPORT:3306で

MysqlSlaveがPORT:3307

でそれぞれ立ち上がってます。

shell>/home/wakame/corelib/bin/wakameadm launch_cluster
Cluster : Wakame::Service::WebCluster (1)
  Wakame::Service::Apache_LB : <current=1 min=1, max=1, type=ConstantCounter> 
     d12b2759abb761a3c8b5eaf99041eb785784bb8f (ONLINE)
  Wakame::Service::Apache_WWW : <current=1 min=1, max=5, type=ConstantCounter> 
     5d2c848392140b0ae961427e7e86cadbb9f6f8e6 (ONLINE)
  Wakame::Service::Apache_APP : <current=1 min=1, max=5, type=ConstantCounter> 
     e86ea71b4d991b85f4b9169ab87dbde7040ca791 (ONLINE)
  Wakame::Service::MySQL_Slave : <current=1 min=1, max=5, type=ConstantCounter> 
     f9add259b40dfc8e642b76373887312938b76b33 (ONLINE)
  Wakame::Service::MySQL_Master : <current=1 min=1, max=1, type=ConstantCounter> 
     067f2b412d632388b8023b05e1636ffe1b763691 (ONLINE)

Instances :
  f9add259b40dfc8e642b76373887312938b76b33 : Wakame::Service::MySQL_Slave (ONLINE)
    On VM instance: i-8384a0ea
  e86ea71b4d991b85f4b9169ab87dbde7040ca791 : Wakame::Service::Apache_APP (ONLINE)
    On VM instance: i-8384a0ea
  067f2b412d632388b8023b05e1636ffe1b763691 : Wakame::Service::MySQL_Master (ONLINE)
    On VM instance: i-8384a0ea
  5d2c848392140b0ae961427e7e86cadbb9f6f8e6 : Wakame::Service::Apache_WWW (ONLINE)
    On VM instance: i-8384a0ea
  d12b2759abb761a3c8b5eaf99041eb785784bb8f : Wakame::Service::Apache_LB (ONLINE)
    On VM instance: i-8384a0ea

Agents :
  i-8384a0ea : 10.252.46.68, 174.129.175.158 load=0.52, 2 sec(s) (1)
    Services (5): Wakame::Service::MySQL_Slave, Wakame::Service::Apache_APP, Wakame::Service::MySQL_Master, Wakame::Service::Apache_WWW, Wakame::Service::Apache_LB

3.propagate_serviceコマンドでslaveを増やします。

shell>/home/wakame/corelib/bin/wakameadm propagate_service Wakame::Service::MySQL_Slave
shell>/home/wakame/corelib/bin/wakameadm status

Cluster : Wakame::Service::WebCluster (1)
  Wakame::Service::Apache_LB : <current=1 min=1, max=1, type=ConstantCounter> 
     d12b2759abb761a3c8b5eaf99041eb785784bb8f (ONLINE)
  Wakame::Service::Apache_WWW : <current=1 min=1, max=5, type=ConstantCounter> 
     5d2c848392140b0ae961427e7e86cadbb9f6f8e6 (ONLINE)
  Wakame::Service::Apache_APP : <current=1 min=1, max=5, type=ConstantCounter> 
     e86ea71b4d991b85f4b9169ab87dbde7040ca791 (ONLINE)
  Wakame::Service::MySQL_Slave : <current=2 min=1, max=5, type=ConstantCounter> 
     f9add259b40dfc8e642b76373887312938b76b33 (ONLINE)
     eea54acb977bc557c01c4fed319edf5b4a9688b7 (ONLINE)
  Wakame::Service::MySQL_Master : <current=1 min=1, max=1, type=ConstantCounter> 
     067f2b412d632388b8023b05e1636ffe1b763691 (ONLINE)

Instances :
  f9add259b40dfc8e642b76373887312938b76b33 : Wakame::Service::MySQL_Slave (ONLINE)
    On VM instance: i-8384a0ea
  e86ea71b4d991b85f4b9169ab87dbde7040ca791 : Wakame::Service::Apache_APP (ONLINE)
    On VM instance: i-8384a0ea
  067f2b412d632388b8023b05e1636ffe1b763691 : Wakame::Service::MySQL_Master (ONLINE)
    On VM instance: i-8384a0ea
  eea54acb977bc557c01c4fed319edf5b4a9688b7 : Wakame::Service::MySQL_Slave (ONLINE)
    On VM instance: i-6587a30c
  5d2c848392140b0ae961427e7e86cadbb9f6f8e6 : Wakame::Service::Apache_WWW (ONLINE)
    On VM instance: i-8384a0ea
  d12b2759abb761a3c8b5eaf99041eb785784bb8f : Wakame::Service::Apache_LB (ONLINE)
    On VM instance: i-8384a0ea

Agents :
  i-8384a0ea : 10.252.46.68, 174.129.175.158 load=0.04, 7 sec(s) (1)
    Services (5): Wakame::Service::MySQL_Slave, Wakame::Service::Apache_APP, Wakame::Service::MySQL_Master, Wakame::Service::Apache_WWW, Wakame::Service::Apache_LB
  i-6587a30c : 10.252.75.212, 174.129.127.228 load=0.09, 5 sec(s) (1)
    Services (1): Wakame::Service::MySQL_Slave

新しいインスタンスi-6587a30cが立ち上がり、Wakame::Service::MySQL_Slaveが立ち上がっています。

MysqlSlaveは複製され、PORT:3307で接続できます。

インスタンス i-8384a0eaには

Wakame::Service::MySQL_Slave

Wakame::Service::Apache_APP

Wakame::Service::MySQL_Master

Wakame::Service::Apache_WWW

Wakame::Service::Apache_LB

インスタンス i-6587a30cには

Wakame::Service::MySQL_Slave

がそれぞれ立ち上がっています。

4.次にELBを作成します。

ProtcolをTCP、Portは3307で通信します。

注意しなければいけないのはavailability-zonesでインスタンスの同じzoneを指定する必要があります。

shell>elb-create-lb wakame-lb --availability-zones us-east-1a --listener "protocol=TCP,lb-port=3307,instance-port=3307"
DNS-NAME  wakame-lb-7663338.us-east-1.elb.amazonaws.com

5.さきほど立ち上がったインスタンス、i-8384a0ea,i-6587a30cをELBに追加します

shell>elb-register-instances-with-lb wakame-lb --instances i-8384a0ea
shell>elb-register-instances-with-lb wakame-lb --instances i-6587a30c

6.通信可能かチェック

shell>elb-describe-instance-health wakame-lb
INSTANCE-ID  i-8384a0ea  InService
INSTANCE-ID  i-6587a30c  InService

7.ELBのIPをあらかじめ取得

shell>host wakame-lb-7663338.us-east-1.elb.amazonaws.com
wakame-lb-7663338.us-east-1.elb.amazonaws.com has address 174.129.228.168

8.データベースを作成。適当に

create database hoge_development;
create table tests(
id int unsigned auto_increment primary key,
text varchar(255)
);

9.任意のRailsプロジェクトを作成。適当に

10.mysql_replication_adapterをインストール

cd ./vendor/plugins/
wget http://github.com/findchris/mysql_replication_adapter/tarball/master
tar xvzf ./mysql_replication_adapter.tar.gz
mv ./findchris-mysql_replication_adapter-be201ae1ae1d944400d4d479d3a9c543c7900819 \
./mysql_replication_adapter
rm -i ./mysql_replication_adapter.tar.gz

11.database.ymlを修正

10.252.46.68はi-8384a0eaのIPでPORT:3306でMysqlのMasterへと接続します。

174.129.228.168はELBのIPを指定。PORT:3307で接続します。

shell>vim ./config/database.yml

development:
  adapter: mysql_replication 
  database: hoge_development
  host: 10.252.46.68 
  username: wakame
  password: wakame
  port: 3306
  slaves:
    - host: 174.129.228.168
      adapter: mysql
      database: hoge_development
      username: wakame
      password: wakame
      port: 3307

12.適当なcontrollerに

class HogeController < ApplicationController
  def index
    @test = Test.find(:all, :use_slave => true) #connecting slave
  end
end
:use_slave => true

としておくとSlaveへ読みにいきます。指定しなければMasterを見に行きます。


13.2台のSlaveのログを見て振り分けられているか確認

tail -f /var/log/mysql/mysql-slave.log

今回はまだ手動でMaster-Slaveの設定を行っていますが、4以降の手順は自動化できます。

将来のバージョンでELBへの割当も自動でおこなう予定です!!

2009-06-08 merbをインストールしてみたら このエントリーを含むブックマーク このエントリーのブックマークコメント

$sudo gem install merb

**************************************************

  Thank you for installing rspec-1.2.6

  Please be sure to read History.rdoc and Upgrade.rdoc
  for useful information about this release.

**************************************************




Building native extensions.  This could take a while...
Successfully installed rspec-1.2.6
Successfully installed addressable-2.0.2
Successfully installed extlib-0.9.12
Successfully installed data_objects-0.9.12
Successfully installed dm-core-0.9.11
Successfully installed dm-migrations-0.9.11
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.4
Successfully installed json_pure-1.1.6
Successfully installed mime-types-1.16
Successfully installed thor-0.9.9
Successfully installed merb-core-1.0.11
Successfully installed merb_datamapper-1.0.11
Successfully installed ZenTest-4.1.1
Successfully installed RubyInline-3.8.1
Successfully installed sexp_processor-3.0.1
Successfully installed ParseTree-3.0.3
Successfully installed ruby2ruby-1.2.2
Successfully installed merb-action-args-1.0.11
Successfully installed merb-assets-1.0.11
Successfully installed merb-slices-1.0.11
Successfully installed merb-auth-core-1.0.11
Successfully installed merb-auth-more-1.0.11
Successfully installed merb-auth-slice-password-1.0.11
Successfully installed merb-auth-1.0.11
Successfully installed merb-cache-1.0.11
Successfully installed merb-exceptions-1.0.11
Successfully installed highline-1.5.1
Successfully installed diff-lcs-1.1.2
Successfully installed templater-0.5.0
Successfully installed merb-gen-1.0.11
Successfully installed haml-2.0.9
Successfully installed merb-haml-1.0.11
Successfully installed merb-helpers-1.0.11
Successfully installed mailfactory-1.4.0
Successfully installed merb-mailer-1.0.11
Successfully installed merb-param-protection-1.0.11
Successfully installed merb-more-1.0.11
Successfully installed do_sqlite3-0.9.12
Successfully installed dm-timestamps-0.9.11
Successfully installed dm-types-0.9.11
Successfully installed dm-aggregates-0.9.11
Successfully installed dm-validations-0.9.11
Successfully installed randexp-0.1.4
Successfully installed dm-sweatshop-0.9.11
Successfully installed dm-serializer-0.9.11
Successfully installed merb-1.0.11
47 gems installed

自分で好きなようにライブラリが選べるとはいえ、使ったことないモジュールばかりだ。

まあぼちぼちソースコードくらいは眺めてみよう。