跳到主要內容

CouchDB基本操作

CouchDB為Apache專案,發展的部份相當健全,有用過JSON的人應該都知道JSON針對不同語言的實作上支援相當好,官方就提供許多語言的API版本,CouchDB也一樣有各種語言的API實作版本,因此開發者可以相當快速進入並熟悉操作,今天很高興有機會試用一下Apache CouchDB,找了一下網路上文獻,親手在MiCloud上建了一各環境來試試,建置的過程因為是在SmartOS上建立,過程非常簡單,就不贅述,其他環境的安裝與使用可以參考下面"相關參考資料"部份,都有非常詳細說明。
而關於SmartOS,安裝好後,相關檔案位置如下:
  • DB安裝位置為:/opt/local/share/couchdb
  • 設定檔位置:/opt/local/etc/couchdb/default.ini
  • 預設admin web: http://COUCHDB_ADDR:5984/_utils/
  • 服務操作:
    • 啓動:svcadm enable couchdb
    • 關閉:svcadm disable couchdb
    • 重啓:svcadm restart couchdb

相關參考資訊:
#Couch APIs
#Couch Rest API Documents

基本操作:
#在資料檔案($1)下面建立一筆資料(JSON檔案$2)
curl http://COUCHDB_ADDR:5984/$1/ -d @$2 -H "Content-Type: application/json"
curl http://COUCHDB_ADDR:5984/$1/ -d "{\"\name\":\"simonsu\"}" -H "Content-Type: application/json"
ex:
$ curl -u "username" http://COUCHDB_ADDR:5984/sys_props/ -d "{\"name\":\"simonsu\"}" -H "Content-Type: application/json"
Enter host password for user 'username': *****
{"ok":true,"id":"f3471fdb35cb331dc663c252d4000492","rev":"1-f39a7c6b6026c79c7d9b478ab8df662a"}


#取得目前存在之所有資料文檔
ex:
$ curl -u "username:password" http://COUCHDB_ADDR:5984/_all_dbs
["_replicator","_users","sys_props","sys_user","test_suite_reports"]

#取得資料庫版本
ex:
curl -u "username:password" http://COUCHDB_ADDR:5984
{"couchdb":"Welcome","version":"1.1.0"}


#取得特定資料庫($1)內文
ex:
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user{"db_name":"sys_user","doc_count":5,"doc_del_count":1,"update_seq":12,"purge_seq":0,"compact_running":false,"disk_size":102489,"instance_start_time":"1327305757798970","disk_format_version":5,"committed_update_seq":12}

#取得需要認證(username:$1)的資料庫($2)內文
curl -u "$1" -X GET http://COUCHDB_ADDR:5984/$2
ex:
$ curl -u "username" -X GET http://COUCHDB_ADDR:5984/sys_props
Enter host password for user 'username':
{"db_name":"sys_props","doc_count":1,"doc_del_count":0,"update_seq":3,"purge_seq":0,"compact_running":false,"disk_size":12373,"instance_start_time":"1327294182116036","disk_format_version":5,"committed_update_seq":3}


#取得某資料庫($1)內文件列表
ex:
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user/_all_docs
{"total_rows":5,"offset":0,"rows":[
{"id":"123","key":"123","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"123456","key":"123456","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"3203e8685839a8136cddd483cc000a6f","key":"3203e8685839a8136cddd483cc000a6f","value":{"rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0"}},
{"id":"3203e8685839a8136cddd483cc001512","key":"3203e8685839a8136cddd483cc001512","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"3203e8685839a8136cddd483cc00198e","key":"3203e8685839a8136cddd483cc00198e","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}}
]}

#附加資料庫內容列表
ex:
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user/_all_docs?include_docs=true
{"total_rows":5,"offset":0,"rows":[
{"id":"123","key":"123","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"123","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"123456","key":"123456","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"123456","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"3203e8685839a8136cddd483cc000a6f","key":"3203e8685839a8136cddd483cc000a6f","value":{"rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0"},"doc":{"_id":"3203e8685839a8136cddd483cc000a6f","_rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0","name":"simon","_attachments":{"\u87a2\u5e55\u5feb\u7167 2012-01-22 \u4e0a\u53481.41.11.png":{"content_type":"image/png","revpos":2,"digest":"md5-EmkJ3uGGgHSUbbvSyJ9cHQ==","length":78270,"stub":true}}}},
{"id":"3203e8685839a8136cddd483cc001512","key":"3203e8685839a8136cddd483cc001512","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"3203e8685839a8136cddd483cc001512","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"3203e8685839a8136cddd483cc00198e","key":"3203e8685839a8136cddd483cc00198e","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"3203e8685839a8136cddd483cc00198e","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}}
]}

#取得某資料庫($1)中某資料id($2)
ex: 
$ curl -X GET http://COUCHDB_ADDR:5984/sys_user/3203e8685839a8136cddd483cc000a6f
{"_id":"3203e8685839a8136cddd483cc000a6f","_rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0","name":"simon","_attachments":{"\u87a2\u5e55\u5feb\u7167 2012-01-22 \u4e0a\u53481.41.11.png":{"content_type":"image/png","revpos":2,"digest":"md5-EmkJ3uGGgHSUbbvSyJ9cHQ==","length":78270,"stub":true}}}

#建立資料庫名稱為$1 (未認證不可新增資料庫)
curl -u "username" -X PUT http://COUCHDB_ADDR:5984/$1
ex:
$ curl -X PUT http://COUCHDB_ADDR:5984/sys_props
{"error":"unauthorized","reason":"You are not a server admin."}
$ curl -u "username" -X PUT http://COUCHDB_ADDR:5984/sys_props
Enter host password for user 'username':
{"ok":true}


#刪除一筆資料
curl -u "username:password" -X DELETE http://COUCHDB_ADDR:5984/$1/$2?rev=$3
ex:
curl -u "username:password" -X DELETE http://COUCHDB_ADDR:5984/sys_user/62f0eed1d52e61c929e580f90a00012e?rev=1-9f3d0835176fbffc6163e3819d7f503f
{"ok":true,"id":"62f0eed1d52e61c929e580f90a00012e","rev":"2-f5e8452a6729974a036d05fce15c8a51"}

#拷貝一筆資料($1:document, $2:record_id, $3: revision, $4: destination_id)
curl -X COPY http://COUCHDB_ADDR:5984/$1/$2?rev=$3 -H "Destination: $4"
ex:
#目前資料列表
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user/_all_docs
{"total_rows":4,"offset":0,"rows":[
{"id":"123","key":"123","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"3203e8685839a8136cddd483cc000a6f","key":"3203e8685839a8136cddd483cc000a6f","value":{"rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0"}},
{"id":"3203e8685839a8136cddd483cc001512","key":"3203e8685839a8136cddd483cc001512","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"3203e8685839a8136cddd483cc00198e","key":"3203e8685839a8136cddd483cc00198e","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}}
]}
#資料複製
$ curl -u "username:password" -X COPY http://COUCHDB_ADDR:5984/sys_user/3203e8685839a8136cddd483cc00198e?rev=1-9f3d0835176fbffc6163e3819d7f503f -H "Destination: 123456"
{"id":"123456","rev":"1-9f3d0835176fbffc6163e3819d7f503f"}
#複製後檢視資料
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user/_all_docs?include_docs=true
{"total_rows":5,"offset":0,"rows":[
{"id":"123","key":"123","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"123","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"123456","key":"123456","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"123456","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"3203e8685839a8136cddd483cc000a6f","key":"3203e8685839a8136cddd483cc000a6f","value":{"rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0"},"doc":{"_id":"3203e8685839a8136cddd483cc000a6f","_rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0","name":"simon","_attachments":{"\u87a2\u5e55\u5feb\u7167 2012-01-22 \u4e0a\u53481.41.11.png":{"content_type":"image/png","revpos":2,"digest":"md5-EmkJ3uGGgHSUbbvSyJ9cHQ==","length":78270,"stub":true}}}},
{"id":"3203e8685839a8136cddd483cc001512","key":"3203e8685839a8136cddd483cc001512","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"3203e8685839a8136cddd483cc001512","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"3203e8685839a8136cddd483cc00198e","key":"3203e8685839a8136cddd483cc00198e","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"3203e8685839a8136cddd483cc00198e","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}}
]}

這個網誌中的熱門文章

Oracle LISTAGG

同事介紹的一個Oracle的好用查詢:LISTAGG
SELECT A.GROUP_ID,A.KEY, LISTAGG(A.VALUE,'; ')WITHINGROUP(ORDERBYA.VALUE)as GG  fromSYS_PROPERTIESaGROUP byA.GROUP_ID,A.KEY
LISTAGG可以將group後的結果會總顯示於一個欄位 上述SQL原本A.VALUE會是一個row一個row的排列 使用LISTAGG之後,可以將A.VALUE顯示在同一個row中 並且可以指定間隔符號(在此設定為';') 針對某一些報表查詢非常有用唷 :D

Flashback Query

感謝我的好友提供的: 在Oracle好用的指令...
Flashback Query: 讓異動過的資料表回到歷史時間點 Step 1. è
show parameter undo_retention  PS: 這個參數顯示系統上設定歷史保留時間(ex: 900 = 900秒) Step 2. è CREATETABLE XXXXX_0815 ASSELECT *  FROM XXXXXASOFTIMESTAMPTO_TIMESTAMP('2011-08-15 12:00:00','YYYY-MM-DD HH24:MI:SS');
PS: 透過as of timestamp方式取出該時間點的表格資料

Xoom 3.2 Upgrade

雖然Xoom早已經更新到3.2 但是台灣遲遲沒有更新版本@@ 終於按奈不住好奇心了... XDA上找到給604(台版Xoom)的3.2版本ROM(美版的) 動手刷拉~~ 出奇的順利阿... 下面是兩個相關網址:
http://forum.xda-developers.com/showthread.php?t=1165998
http://forum.xda-developers.com/showthread.php?t=1080963
我這邊是刷第二各網址下載的3.2 ROM(http://www.mediafire.com/?adsa081m3z8uykd) 3.1的就沒去測拉... 也沒有再去破解(遲早的ㄅ:P)
使用心得: 1. 順~比3.0操頻還順 2. 原生就支援micro-sd 3. 目前看是不支援USB...缺憾@@ 4. 因為美版的關係,所以有Books跟Videos兩個APP,但是因為網路地區的關係不能用就是了... 5. 沒有原生中文輸入法,不過對我沒差...我都用Google拼音 :D