跳到主要內容

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

使用GCP Cloud Builder建置CI/CD Flow

服務的建置通常是持續性的作業,而部署則一般是專案初期建置一次,未來可以沿用該部署設定... 這樣的流程走向自動化,在Container的環境又更是重要... 本篇介紹一下,在Google雲端,我們可以搭配Source Repository與Build Trigger等服務來完成服務的自動建置與部屬,讓封裝Container與部署到Container Engine的動作可以一氣呵成...
首先幾單瞭解一下一個Container Engine服務的建置與部屬過程...
使先,建立Container Engine Cluster,透過GCP Winzard可以很快速地開立您的GKE Cluster…

假設您的cluster是叫做demo-cluster,則可以透過下面的指令來跟GKE建立連線
$ gcloud container clusters get-credentials demo-cluster --zone asia-east1-a
這串指令不用記ㄛ~可以在Cluster的頁面找到他...

點選複製,即可貼到您的Terminal執行...

跟GKE建立鏈結後,接下來可以部署您的城市,這邊我們以我的一個範例程式Demoweb (https://github.com/peihsinsu/demoweb) 為例,

這個專案中,包含幾個重要結構:
app/ : 放置您的程式,在Dockerfile中會將該資料匣複製到Docker Image中 k8s/ : 放置k8s的deployment與service描述檔 Dockerfile : 封裝docker的描述檔,會以node.js的image為基礎來建置執行環境 cloudbuild.yaml : Google Cloud Build Trigger的步驟描述檔

使用 minikube 輕鬆上手 kubernetes

安裝minikube
macOS只需要透過brew即可快速安裝...
brew cask install minikube
Linux環境可以直接下載執行檔,放到環境變數可以吃到的路徑即可...
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && \ chmod +x minikube && \
sudo mv minikube /usr/local/bin/
Windows的下載網址如下: https://storage.googleapis.com/minikube/releases/latest/minikube-windows-amd64.exe
如果您的kubectl尚未安裝,可以直接使用google cloud sdk來安裝:
curl https://sdk.cloud.google.com | bash
gcloud components install kubectl
安裝完成後,原則上minikube會在本地端加入minikube的k8s context,我們可以透過下面指令來使用該context…
kubectl config use-context minikube
然後,可檢查一下您的minikube node是否正常運作....