跳到主要內容

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

使用 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是否正常運作....

新一代LB - Traefik

新一代LB-Traefik Traefik突破以往我們對loadbalancer的觀點,他是一套直接與docker整合的loadbalancer套件...透過Traefik,我們可以使用label的方式將後面啟動的dockerinstance掛載到loadbalancer中,且無需重新啟動Traefik,可直接生效... Traefik基本介紹 Traefik是以動態重載新加入的dockerinstance的方式來替有附加相同domainlabel的dockerinstance建立網路附載平衡的關聯...因此,設定上,與一般我們建立reverseproxy的過程剛好相反(一般我們會先建立服務,再建立reverseproxy將服務串連起來)... Step1-建立Traefik服務 下面我們用官方的composefile來說明... File:docker-compose.yaml version: '2' services: proxy: image: traefik command: --api --docker --docker.domain=docker.localhost --logLevel=DEBUG networks: - webgateway ports: - "80:80" - "8080:8080" volumes: - /var/run/docker.sock:/var/run/docker.sock - /dev/null:/traefik.toml networks: webgateway: driver: bridge 其中traefik啟動時候,我們需要指定docker.domain來告訴taefik要聆聽的domain是哪一個,然後要事先開啟對應的port,讓外部服務可以連到traefik...,另外,我們將dockersocket掛載進來,這是必要的設定,讓traefik可以透過dockersocket來操控一些東西...,最後,traefik.toml檔案,我們保留空的,讓treafik自己建立... 啟動: docker-compose -f docker…