1. YALToolsの使い方

YALToolsはCouchDB上の文書を、読み、書き、変更し、削除するコマンドを提供します。

その中でもcsv2jsonはViewを定義したり、ドキュメントを作成するための目的で設定されています。

"csv2json"はいくらか特殊な動きをしますし、一般的とはいえません。 それでもYALToolsの他のコマンドと連携して動くコマンドの例のつもりです。

この文書の例では十分な説明になっていないと思いますし、内容によっては特殊な動きをするための 楽しんでCouchDBを操作してください。

1-1. Create new database

To create a database, pwdb, please following steps.

$ sbin/mkdb pwdb
{"ok":true}

[ chapter top | page top ]

1-2. Show existing databases

$ sbin/lsdbs
{"database":["_users","pwdb"]}

[ chapter top | page top ]

1-3. Populate test data: store /etc/passwd to pwdb

$ bin/csv2json -t '["name","pw","uid","gid","gecos","home","shell"]' -s ':' -n  /etc/passwd | bin/postdocs pwdb
{"failed_list":[]}
$ bin/lsdocs pwdb | head -1 | bin/jsonfmt
{
  "_id":  "a1ff622c5b43d9cf8aba1fa1e20074b2",
  "_rev":  "1-a8d09a9a7c1b4b7d9dfe6af45abef79c",
  "name":  "root",
  "pw":  "x",
  "uid":  "0",
  "gid":  "0",
  "gecos":  "root",
  "home":  "/root",
  "shell":  "/bin/bash"
}

[ chapter top | page top ]

1-4. Define a new view

Here is the view definition for pwdb defined in previous section.

"views","uid","map","views","uid","reduce","views","name","map","views","name","reduce"
,,"function(doc) {
  if(doc.uid) {
    emit(doc.uid, 1);
  }
}",,,"_count",,,"function(doc) {
  if(doc.name) {
    emit(doc.name, 1);
  }
}",,,"_count"

A csv file is portable and editable by a text editor or a spreadsheet software, such as emacs and LibreOffice Calc.

Open the csv file in LibreOffice

These two view definition are placed side-by-side in a horizontal direction. 
This is caused by requirements of "csv2json".

Using YALTools, sotre this csv file in to the pwdb.

$ bin/csv2json samples.csv/pwdb_views.csv | bin/putdesign pwdb test

The registered view definitions can be verified to see the /pwdb/_design/test document.

$ bin/lsdoc /pwdb/_design/test | bin/jsonfmt
{
  "_id":  "_design/test",
  "_rev":  "1-39d91a281979eb7fc62b3f1d4a9f47ab",
  "views":  {
    "uid":    {
      "map":      "function(doc) {
  if(doc.uid) {
    emit(doc.uid, 1);
  }
}",
      "reduce":      "_count"
    },
    "name":    {
      "map":      "function(doc) {
  if(doc.name) {
    emit(doc.name, 1);
  }
}",
      "reduce":      "_count"
    }
  }
}

[ chapter top | page top ]

1-5. Search views by username

Let's search the views defined in previous section with "lsviews".

Example searchs the all documents sorted by the "name" field.

$ bin/lsviews pwdb test name

Example searchs the document named postfix.

$ bin/lsviews pwdb test name -k '"postfix"'
{"_id":"ed5b14905389ef0d464c2c8509012eeb","_rev":"1-63f1a7b49dbd18b79a1ff59a38bd4410","name":"postfix","pw":"x","uid":"119","gid":"131","gecos":"","home":"/var/spool/postfix","shell":"/bin/false"}

Example searchs documents, each name is beging from "post".

$ bin/lsviews pwdb test name -s '"post"' -e '"postZ"'
{"_id":"ed5b14905389ef0d464c2c8509012eeb","_rev":"1-63f1a7b49dbd18b79a1ff59a38bd4410","name":"postfix","pw":"x","uid":"119","gid":"131","gecos":"","home":"/var/spool/postfix","shell":"/bin/false"}
{"_id":"ed5b14905389ef0d464c2c850900e436","_rev":"1-5c7f03c54841bec492b626dcffc4540b","name":"postgres","pw":"x","uid":"113","gid":"126","gecos":"PostgreSQL administrator,,,","home":"/var/lib/postgresql","shell":"/bin/bash"}

[ chapter top | page top ]

1-6. Delete all documents

Here is an example to delete all documents on pwdb database.

$ bin/lsdocs pwdb | bin/chjson '["_deleted",true]' | bin/postdocs pwdb
{"failed_list":[]}
$ bin/lsdocs pwdb
$

[ chapter top | page top ]

2. サンプルアプリケーション - 郵便番号検索

https://www.yadiary.net/postal/main.fcgi で公開しているアプリケーションとほぼ同等のコードを含むアーカイブを作成しました。

詳細は https://www.yadiary.net/postal/internals.html にあります。

コード自体はGitHubにありますので、https://github.com/YasuhiroABE/postal-code-searcher を参照してください。

Permalink: /0.1/cs.html

Created: 2011-03-01T18:04:56+09:00
Last modified: 2011-04-04T01:40:58+09:00

lscouchdb.sourceforge.net / 2011 © Yasuhiro ABE <yasu@yasundial.org> JabberID: yadiary@jabber.org

Valid XHTML + RDFa 正当なCSSです!
RDFa it (RDF/XML)!

Creative Commons License lscouchdb.sourceforge.net by Yasuhiro ABE is licensed under a Creative Commons Attribution 2.1 Japan License. Permissions beyond the scope of this license may be available at http://lscouchdb.sourceforge.net/license.html.