前回のCouchDBインストールに引き続き、CouchDBをさわりました。
また、CouchDBでアプリケーション開発するツールであるCouchAppをインストールし、少しさわってみます。
1. adminユーザ設定
データベース作成の前に、必要に応じてadminユーザ設定を行います。
/etc/couchdb/local.ini内にadminユーザ設定箇所あるので、任意のユーザを設定します。
設定後、管理コンソール「Futon」に行き、ログインして確認します。
場所が分かりづらかったのですが、右下部にログインリンクがあります。
また、コマンドライン経由で何らかのアクションを実行する場合、URLにユーザ名とパスワードを含めます。
例えば、http://[user]:[passwd]@[serverIP]:[port]/[db] のようなイメージとなります。
2. データベース作成
データベース作成は、ブラウザ経由でやっても良いですし、コマンドラインからも行えます。
今回はコマンドラインから行います。
下記では最初にadminユーザの指定をしなかったので、エラーとなっています。
また、”PUT”を指定しているところからも、Restfulインタフェースを通じてCouchDBを操作しているのが
確認出来ます。
[shmachid@hoge couchdb]$ curl -X PUT http://49.212.1.17:xxxx/test {"error":"unauthorized","reason":"You are not a server admin."} [shmachid@hoge couchdb]$ curl -X PUT http://[user]:[passwd]@49.212.1.17:xxxx/test {"ok":true}
上記を実行後、Futon(http://49.212.1.17:xxxx/_utils/index.html)からデータベースを確認してください。
3. データ投入
作成したデータベースにデータ投入を行います。
最初はコマンドラインから直に指定してみます。
[shmachid@hoge couchdb6]$ curl -X POST -d '{"data":"hoge test"}' -H 'Content-Type:appl ication/json; charset=UTF-8' http://[user]:[passwd]@49.212.1.17:xxxx/test {"ok":true,"id":"ee326263173915e2e424eb8d62002388","rev":"1-2328b45ab4c4437012c5b7defd9ef59d"}
コマンドライン、ブラウザの双方でデータがJSON形式になっているのが確認出来ます。
次に作成済みのHTMLファイルを投入します。
まず、投入対象のHTMLファイルを作ります。
<html> <body>test hogehoge</body> </html>
下記のようなJSONファイルを作ります。コマンドラインで指定をしていたデータ情報を記述します。
{ "id" : "test_data", "_attachments" : { "index.html" : { "data" : "PGh0bWw+CiA8Ym9keT50ZXN0IGhvZ2Vob2dlPC9ib2R5Pgo8L2h0bWw+Cg==", "content_type" : "text/html" } } }
上記JSONファイル中のdataには下記のbase64でエンコードした文字列をセットします。
[shmachid@hoge couchdb_test]$ openssl base64 -in index.html PGh0bWw+CiA8Ym9keT50ZXN0IGhvZ2Vob2dlPC9ib2R5Pgo8L2h0bWw+Cg==
準備は完了したので、投入してみます。
[shmachid@hoge couchdb_test]$ curl -X POST -H 'Content-Type:application/json; charset= UTF-8' -d @test_data.json http://[user]:[passwd]@49.212.1.17:xxxx/test {"ok":true,"id":"ee326263173915e2e424eb8d62002945","rev":"1-d7387e19cfef70dc557a98a8d4d65816"}
再び、Futonからデータベース内を確認すると、データが増えているのが分かります。
index.htmlもJSON形式で格納されており、開く事も出来ます。
今度はFutonからExcelファイルをアップロードしてみます。
New Documentボタンを押下すると、Key値が自動提案されてきます。
4. CouchAppインストール
CouchDBでアプリケーション開発を行うツールであるCouchAppをインストールします。
ここが結構ハマったのですが、CouchAppを使用する場合、Pythonはversion2.6以上である必要があります。
sakuraVPSの場合、デフォルトではversion2.4となる為、最後のインストール部分でSyntax errorとなります。
Python2.6へのアップグレードはこちらを参考にさせてもらいました。
では、CouchAppのセットアップをしていきます。
途中、Python2.6が認識出来ずに2.4が適用されてしまう部分では、明示的に指定しています。
[shmachid@hoge couchdb6]$ curl -O http://python-distribute.org/distribute_setup.py % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 15757 100 15757 0 0 11195 0 0:00:01 0:00:01 --:--:-- 25853 [shmachid@hoge couchdb6]$ sudo /usr/local/python-2.6.2/bin/python distribute_setup.py [sudo] password for shmachid: Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.19.tar.gz Extracting in /tmp/tmpGa3LNw Now working in /tmp/tmpGa3LNw/distribute-0.6.19 Installing Distribute Before install bootstrap. Scanning installed packages Setuptools installation detected at /usr/local/python-2.6.2/lib/python2.6/site-packages/setuptools-0.6c12dev_r88846-py2.6.egg Egg installation Patching... Renaming /usr/local/python-2.6.2/lib/python2.6/site-packages/setuptools-0.6c12dev_r88846-py2.6.egg into /usr/local/python-2.6.2/lib/python2.6/site-packages/setuptools-0.6c12dev_r88846-py2.6.egg.OLD.1312983070.12 Patched done. Relaunching... Before install bootstrap. Scanning installed packages Setuptools installation detected at /usr/local/python-2.6.2/lib/python2.6/site-packages/setuptools-0.6c12dev_r88846-py2.6.egg Egg installation Already patched. running install running bdist_egg running egg_info writing distribute.egg-info/PKG-INFO writing top-level names to distribute.egg-info/top_level.txt writing dependency_links to distribute.egg-info/dependency_links.txt writing entry points to distribute.egg-info/entry_points.txt reading manifest file 'distribute.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'distribute.egg-info/SOURCES.txt' installing library code to build/bdist.linux-x86_64/egg running install_lib running build_py creating build creating build/lib copying pkg_resources.py -> build/lib copying easy_install.py -> build/lib copying site.py -> build/lib creating build/lib/setuptools copying setuptools/extension.py -> build/lib/setuptools copying setuptools/__init__.py -> build/lib/setuptools copying setuptools/depends.py -> build/lib/setuptools copying setuptools/package_index.py -> build/lib/setuptools copying setuptools/sandbox.py -> build/lib/setuptools copying setuptools/archive_util.py -> build/lib/setuptools copying setuptools/dist.py -> build/lib/setuptools creating build/lib/setuptools/command copying setuptools/command/bdist_rpm.py -> build/lib/setuptools/command copying setuptools/command/test.py -> build/lib/setuptools/command copying setuptools/command/build_py.py -> build/lib/setuptools/command copying setuptools/command/easy_install.py -> build/lib/setuptools/command copying setuptools/command/sdist.py -> build/lib/setuptools/command copying setuptools/command/__init__.py -> build/lib/setuptools/command copying setuptools/command/upload_docs.py -> build/lib/setuptools/command copying setuptools/command/rotate.py -> build/lib/setuptools/command copying setuptools/command/saveopts.py -> build/lib/setuptools/command copying setuptools/command/install_lib.py -> build/lib/setuptools/command copying setuptools/command/install_scripts.py -> build/lib/setuptools/command copying setuptools/command/alias.py -> build/lib/setuptools/command copying setuptools/command/build_ext.py -> build/lib/setuptools/command copying setuptools/command/upload.py -> build/lib/setuptools/command copying setuptools/command/register.py -> build/lib/setuptools/command copying setuptools/command/egg_info.py -> build/lib/setuptools/command copying setuptools/command/setopt.py -> build/lib/setuptools/command copying setuptools/command/bdist_wininst.py -> build/lib/setuptools/command copying setuptools/command/install.py -> build/lib/setuptools/command copying setuptools/command/install_egg_info.py -> build/lib/setuptools/command copying setuptools/command/develop.py -> build/lib/setuptools/command copying setuptools/command/bdist_egg.py -> build/lib/setuptools/command creating build/lib/setuptools/tests copying setuptools/tests/test_build_ext.py -> build/lib/setuptools/tests copying setuptools/tests/doctest.py -> build/lib/setuptools/tests copying setuptools/tests/test_develop.py -> build/lib/setuptools/tests copying setuptools/tests/server.py -> build/lib/setuptools/tests copying setuptools/tests/test_upload_docs.py -> build/lib/setuptools/tests copying setuptools/tests/__init__.py -> build/lib/setuptools/tests copying setuptools/tests/test_resources.py -> build/lib/setuptools/tests copying setuptools/tests/test_sandbox.py -> build/lib/setuptools/tests copying setuptools/tests/test_packageindex.py -> build/lib/setuptools/tests copying setuptools/tests/test_easy_install.py -> build/lib/setuptools/tests copying setuptools/gui.exe -> build/lib/setuptools copying setuptools/cli.exe -> build/lib/setuptools creating build/bdist.linux-x86_64 creating build/bdist.linux-x86_64/egg copying build/lib/easy_install.py -> build/bdist.linux-x86_64/egg copying build/lib/pkg_resources.py -> build/bdist.linux-x86_64/egg copying build/lib/site.py -> build/bdist.linux-x86_64/egg creating build/bdist.linux-x86_64/egg/setuptools copying build/lib/setuptools/extension.py -> build/bdist.linux-x86_64/egg/setuptools copying build/lib/setuptools/__init__.py -> build/bdist.linux-x86_64/egg/setuptools creating build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/bdist_rpm.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/test.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/build_py.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/easy_install.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/sdist.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/__init__.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/upload_docs.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/rotate.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/saveopts.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/install_lib.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/install_scripts.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/alias.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/build_ext.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/upload.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/register.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/egg_info.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/setopt.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/bdist_wininst.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/install.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/install_egg_info.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/develop.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/command/bdist_egg.py -> build/bdist.linux-x86_64/egg/setuptools/command copying build/lib/setuptools/depends.py -> build/bdist.linux-x86_64/egg/setuptools copying build/lib/setuptools/gui.exe -> build/bdist.linux-x86_64/egg/setuptools copying build/lib/setuptools/package_index.py -> build/bdist.linux-x86_64/egg/setuptools copying build/lib/setuptools/sandbox.py -> build/bdist.linux-x86_64/egg/setuptools creating build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/test_build_ext.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/doctest.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/test_develop.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/server.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/test_upload_docs.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/__init__.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/test_resources.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/test_sandbox.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/test_packageindex.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/tests/test_easy_install.py -> build/bdist.linux-x86_64/egg/setuptools/tests copying build/lib/setuptools/cli.exe -> build/bdist.linux-x86_64/egg/setuptools copying build/lib/setuptools/archive_util.py -> build/bdist.linux-x86_64/egg/setuptools copying build/lib/setuptools/dist.py -> build/bdist.linux-x86_64/egg/setuptools byte-compiling build/bdist.linux-x86_64/egg/easy_install.py to easy_install.pyc byte-compiling build/bdist.linux-x86_64/egg/pkg_resources.py to pkg_resources.pyc byte-compiling build/bdist.linux-x86_64/egg/site.py to site.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/extension.py to extension.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/__init__.py to __init__.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/bdist_rpm.py to bdist_rpm.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/test.py to test.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/build_py.py to build_py.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/easy_install.py to easy_install.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/sdist.py to sdist.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/__init__.py to __init__.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/upload_docs.py to upload_docs.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/rotate.py to rotate.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/saveopts.py to saveopts.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/install_lib.py to install_lib.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/install_scripts.py to install_scripts.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/alias.py to alias.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/build_ext.py to build_ext.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/upload.py to upload.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/register.py to register.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/egg_info.py to egg_info.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/setopt.py to setopt.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/bdist_wininst.py to bdist_wininst.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/install.py to install.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/install_egg_info.py to install_egg_info.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/develop.py to develop.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/bdist_egg.py to bdist_egg.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/depends.py to depends.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/package_index.py to package_index.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/sandbox.py to sandbox.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/test_build_ext.py to test_build_ext.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/doctest.py to doctest.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/test_develop.py to test_develop.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/server.py to server.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/test_upload_docs.py to test_upload_docs.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/__init__.py to __init__.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/test_resources.py to test_resources.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/test_sandbox.py to test_sandbox.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/test_packageindex.py to test_packageindex.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/tests/test_easy_install.py to test_easy_install.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/archive_util.py to archive_util.pyc byte-compiling build/bdist.linux-x86_64/egg/setuptools/dist.py to dist.pyc creating build/bdist.linux-x86_64/egg/EGG-INFO copying distribute.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO copying distribute.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying distribute.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying distribute.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying distribute.egg-info/entry_points.txt.orig -> build/bdist.linux-x86_64/egg/EGG-INFO copying distribute.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO copying distribute.egg-info/zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO creating dist creating 'dist/distribute-0.6.19-py2.6.egg' and adding 'build/bdist.linux-x86_64/egg' to it removing 'build/bdist.linux-x86_64/egg' (and everything under it) Processing distribute-0.6.19-py2.6.egg creating /usr/local/python-2.6.2/lib/python2.6/site-packages/distribute-0.6.19-py2.6.egg Extracting distribute-0.6.19-py2.6.egg to /usr/local/python-2.6.2/lib/python2.6/site-packages Adding distribute 0.6.19 to easy-install.pth file Installing easy_install script to /usr/local/python-2.6.2/bin Installing easy_install-2.6 script to /usr/local/python-2.6.2/bin Installed /usr/local/python-2.6.2/lib/python2.6/site-packages/distribute-0.6.19-py2.6.egg Processing dependencies for distribute==0.6.19 Finished processing dependencies for distribute==0.6.19 After install bootstrap. Creating /usr/local/python-2.6.2/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info Creating /usr/local/python-2.6.2/lib/python2.6/site-packages/setuptools.pth
[shmachid@hoge couchdb6]$ sudo easy_install pip Searching for pip Reading http://pypi.python.org/simple/pip/ Reading http://www.pip-installer.org Reading http://pip.openplans.org Best match: pip 1.0.2 Downloading http://pypi.python.org/packages/source/p/pip/pip-1.0.2.tar.gz#md5=47ec6ff3f6d962696fe08d4c8264ad49 Processing pip-1.0.2.tar.gz Running pip-1.0.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-g89SGU/pip-1.0.2/egg-dist-tmp-Ci0LUo warning: no files found matching '*.html' under directory 'docs' warning: no previously-included files matching '*.txt' found under directory 'docs/_build' no previously-included directories found matching 'docs/_build/_sources' Adding pip 1.0.2 to easy-install.pth file Installing pip script to /usr/local/python-2.6.2/bin Installing pip-2.6 script to /usr/local/python-2.6.2/bin Installed /usr/local/python-2.6.2/lib/python2.6/site-packages/pip-1.0.2-py2.6.egg Processing dependencies for pip Finished processing dependencies for pip
[shmachid@hoge couchdb6]$ sudo pip install couchapp Downloading/unpacking couchapp Downloading Couchapp-0.8.1.tar.gz (422Kb): 422Kb downloaded Running setup.py egg_info for package couchapp warning: manifest_maker: MANIFEST.in, line 5: 'recursive-include' expects <dir> <pattern1> <pattern2> ... Installing collected packages: couchapp Running setup.py install for couchapp building 'couchapp.autopush._select' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DHAVE_EPOLL=1 -DHAVE_SYS_EPOLL_H=1 -I/usr/local/python-2.6.2/include/python2.6 -c couchapp/autopush/selectmodule.c -o build/temp.linux-x86_64-2.6/couchapp/autopush/selectmodule.o gcc -pthread -shared build/temp.linux-x86_64-2.6/couchapp/autopush/selectmodule.o -o build/lib.linux-x86_64-2.6/couchapp/autopush/_select.so Linking /home/shmachid/work/couchdb6/build/couchapp/build/lib.linux-x86_64-2.6/couchapp/autopush/_select.so to /home/shmachid/work/couchdb6/build/couchapp/couchapp/autopush/_select.so changing mode of build/scripts-2.6/couchapp from 644 to 755 warning: manifest_maker: MANIFEST.in, line 5: 'recursive-include' expects <dir> <pattern1> <pattern2> ... changing mode of /usr/local/python-2.6.2/bin/couchapp to 755 Successfully installed couchapp Cleaning up...
5. CouchApp 確認
CouchAppのインストールが完了したので、確認します。
アプリケーションを作ってみます。
[shmachid@hoge couchdb6]$ couchapp generate fuga 2011-08-11 02:02:00 [INFO] /home/shmachid/work/couchdb6/fuga generated.
作成したベーステンプレートをCouchDBへpushします。
[shmachid@hoge couchdb6]$ cd fuga [shmachid@hoge fuga]$ couchapp push http://[user]:[passwd]@49.212.1.17:xxxx/test 2011-08-11 02:04:28 [INFO] Visit your CouchApp here: http://49.212.1.17:xxxx/test/_design/fuga/index.html
Futonにて、一緒にpushしたその他のファイルが格納されたことを確認できます。
なお、これらはみてわかるクラウドマガジンvol.3 (日経BPパソコンベストムック)
を参考にさせていただきました。
以上です。