Install Riak on Ubuntu 12.04

Ubuntu 12.04 がリリースされたので、vanilla Ubuntu にBasho Technologies, Inc の開発する Dynamo クローンの Riak をインストールしてみた。

ゴール
VirtualBox 上の Ubuntu 12.04 server(64 bit) に Riak 1.1.2 と Python クライアントをインストール。

Riak のインストール(オフィシャルレポジトリを利用)

2012/10 から Basho の RHEL/Ubuntu 向けオフィシャルレポジトリが用意された。

Official Basho Package Repositories Now Available @ October 16, 2012

この手順に従うと、以下のように簡単に Riak をインストールできる。

Riak のインストール(非オフィシャルレポジトリ)

Ubuntu/Debian へのインストールはオフィシャル wiki にまとまっているので、書かれているとおりに操作すればOK

http://wiki.basho.com/Installing-on-Debian-and-Ubuntu.html

apt-get で riak パッケージのインストール
起動コマンドは /etc/init.d/riak に、設定ファイルは /etc/riak 以下にインストールされる。

$ sudo apt-get install libssl0.9.8
[sudo] password for user:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
libssl0.9.8
0 upgraded, 1 newly installed, 0 to remove and 6 not upgraded.
Need to get 861 kB of archives.
After this operation, 2,391 kB of additional disk space will be used.
Get:1 http://security.ubuntu.com/ubuntu/ precise-security/universe libssl0.9.8 amd64 0.9.8o-7ubuntu3.1 [861 kB]
Fetched 861 kB in 12s (67.7 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libssl0.9.8.
(Reading database ... 48863 files and directories currently installed.)
Unpacking libssl0.9.8 (from .../libssl0.9.8_0.9.8o-7ubuntu3.1_amd64.deb) ...
Setting up libssl0.9.8 (0.9.8o-7ubuntu3.1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

$ wget http://downloads.basho.com/riak/CURRENT/riak_1.1.2-1_amd64.deb
--2012-04-28 01:39:37--  http://downloads.basho.com/riak/CURRENT/riak_1.1.2-1_amd64.deb
Resolving downloads.basho.com (downloads.basho.com)... 174.129.227.88, 50.17.225.60
Connecting to downloads.basho.com (downloads.basho.com)|174.129.227.88|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 21378360 (20M) [application/x-debian-package]
Saving to: `riak_1.1.2-1_amd64.deb'

100%[======================================>] 21,378,360   683K/s   in 33s

2012-04-28 01:40:11 (630 KB/s) - `riak_1.1.2-1_amd64.deb' saved [21378360/21378360]

FINISHED --2012-04-28 01:40:11--
Total wall clock time: 35s
Downloaded: 1 files, 20M in 33s (630 KB/s)
$ sudo dpkg -i riak_1.1.2-1_amd64.deb
[sudo] password for user:
Selecting previously unselected package riak.
(Reading database ... 48887 files and directories currently installed.)
Unpacking riak (from riak_1.1.2-1_amd64.deb) ...
Setting up riak (1.1.2-1) ...
Adding group `riak' (GID 113) ...
Done.
Adding system user `riak' (UID 106) ...
Adding new user `riak' (UID 106) with group `riak' ...
Not creating home directory `/var/lib/riak'.
Processing triggers for man-db ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot

# 起動を確認

$ curl -v http://127.0.0.1:8098/riak/test
* About to connect() to 127.0.0.1 port 8098 (#0)
*   Trying 127.0.0.1... connected
> GET /riak/test HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: 127.0.0.1:8098
> Accept: */*
>
< HTTP/1.1 200 OK
< Vary: Accept-Encoding
< Server: MochiWeb/1.1 WebMachine/1.9.0 (someone had painted it blue)
< Date: Fri, 27 Apr 2012 23:13:52 GMT
< Content-Type: application/json
< Content-Length: 422
<
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0
{"props":{"name":"test","allow_mult":false,"basic_quorum":false,"big_vclock":50,"chash_keyfun":{"mod":"riak_core_util","fun":"chash_std_keyfun"},"dw":"quorum","last_write_wins":false,"linkfun":{"mod":"riak_kv_wm_link_walker","fun":"mapreduce_linkfun"},"n_val":3,"notfound_ok":true,"old_vclock":86400,"postcommit":[],"pr":0,"precommit":[],"pw":0,"r":"quorum","rw":"quorum","small_vclock":50,"w":"quorum","young_vclock":20}}

ping/pong

ノードが応答するか確認。

$ riak ping
Attempting to restart script through sudo -u riak
[sudo] password for user:
pong

ノードが起動していなければ pangがかえってくる。

バケット操作

curl を使って WEB API を叩く

# Key:Value の追加

$ curl -v -X PUT -d '{"bar":"baz"}' -H "Content-Type: application/json"   -H "X-Riak-Vclock: a85hYGBgzGDKBVIszMk55zKYEhnzWBlKIniO8mUBAA=="   http://127.0.0.1:8098/riak/test/doc?returnbody=true
* About to connect() to 127.0.0.1 port 8098 (#0)
*   Trying 127.0.0.1... connected
> PUT /riak/test/doc?returnbody=true HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: 127.0.0.1:8098
> Accept: */*
> Content-Type: application/json
> X-Riak-Vclock: a85hYGBgzGDKBVIszMk55zKYEhnzWBlKIniO8mUBAA==
> Content-Length: 13
>
* upload completely sent off: 13out of 13 bytes
< HTTP/1.1 200 OK
< X-Riak-Vclock: a85hYGBgymDKBVIcypz/fvrPOtObwZTImMfKkLyM/yQfRIqFOTnnHFS4JILnKF8WAA==
< Vary: Accept-Encoding
< Server: MochiWeb/1.1 WebMachine/1.9.0 (someone had painted it blue)
< Link: </riak/test>; rel="up"
< Date: Fri, 27 Apr 2012 23:23:15 GMT
< Content-Type: application/json
< Content-Length: 13
<
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0
{"bar":"baz"}

# Value を渡すと Response の Location に自動生成された Key が埋まっている

$ curl -v -d 'this is a test' -H "Content-Type: text/plain" \
>   http://127.0.0.1:8098/riak/test
* About to connect() to 127.0.0.1 port 8098 (#0)
*   Trying 127.0.0.1... connected
> POST /riak/test HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: 127.0.0.1:8098
> Accept: */*
> Content-Type: text/plain
> Content-Length: 14
>
* upload completely sent off: 14out of 14 bytes
< HTTP/1.1 201 Created
< Vary: Accept-Encoding
< Server: MochiWeb/1.1 WebMachine/1.9.0 (someone had painted it blue)
< Location: /riak/test/RBV1260xSB8eO8vKPa0sLF9tsOo
< Date: Fri, 27 Apr 2012 23:24:10 GMT
< Content-Type: text/plain
< Content-Length: 0
<
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

# Response にある Location を叩く

$ curl http://127.0.0.1:8098/riak/test/RBV1260xSB8eO8vKPa0sLF9tsOo
this is a test

Python クライアントのインストール

Basho Technologies が Riak の Python クライアントを提供している。

ライブラリのインストール
virtualenv & pip でユーザースペースにインストール。ライブラリ名は “riak“。protobuf も一緒にインストールされる。

$ curl -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   99k  100   99k    0     0  63234      0  0:00:01  0:00:01 --:--:-- 73178
$ python virtualenv.py riak_env
New python executable in riak_env/bin/python
Installing setuptools............................done.
Installing pip.....................done.
$ . riak_env/bin/activate
(riak_env)~$ pip install riak
Downloading/unpacking riak
  Downloading riak-1.4.0.tar.gz (62Kb): 62Kb downloaded
  Running setup.py egg_info for package riak

    warning: no files found matching 'erl_src/*'
Downloading/unpacking protobuf (from riak)
  Downloading protobuf-2.4.1.tar.gz (56Kb): 56Kb downloaded
  Running setup.py egg_info for package protobuf

Requirement already satisfied (use --upgrade to upgrade): setuptools in ./riak_env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg (from protobuf->riak)
Installing collected packages: riak, protobuf
  Running setup.py install for riak

    warning: no files found matching 'erl_src/*'
  Running setup.py install for protobuf
    Skipping installation of /home/user/riak_env/lib/python2.7/site-packages/google/__init__.py (namespace package)

    Installing /home/user/riak_env/lib/python2.7/site-packages/protobuf-2.4.1-py2.7-nspkg.pth
Successfully installed riak protobuf
Cleaning up...

ライブラリのテスト

Riak Python クライアントのチュートリアルにあるバケットへの set/get を試す。
get.py

import riak

client = riak.RiakClient()
user_bucket = client.bucket('user')

johndoe = user_bucket.get('johndoe')

# You've now got a ``RiakObject``. To get at the values in a dictionary
# form, call:
johndoe_dict = johndoe.get_data()
print johndoe_dict

set.py

import riak

client = riak.RiakClient()
user_bucket = client.bucket('user')

# We're creating the user data & keying off their username.
new_user = user_bucket.new('johndoe', data={
    'first_name': 'John',
    'last_name': 'Doe',
    'gender': 'm',
    'website': 'http://example.com/',
    'is_active': True,
})
# Note that the user hasn't been stored in Riak yet.
new_user.store()

プログラムの実行

$ python set.py
$ python get.py
{u'website': u'http://example.com/', u'gender': u'm', u'first_name': u'John', u'last_name': u'Doe', u'is_active': True}

Further Reading

About these ads
Tagged with: , , ,
Posted in database

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: