Skip to content

Instantly share code, notes, and snippets.

@ennisa-ire
Last active February 15, 2018 15:40
Show Gist options
  • Save ennisa-ire/2bc081ddb7c798d2fcdfcbd43ce137fd to your computer and use it in GitHub Desktop.
Save ennisa-ire/2bc081ddb7c798d2fcdfcbd43ce137fd to your computer and use it in GitHub Desktop.
00 - IoT Tech Databases Corner

DB - deciding to use an ORM, add to tech stack!!!

So you have installed, and verified you couch DB instance, you might even have securied it, but updating unix permissions, and adding an admin account :) Next step create a doc. A doc is a json schema of one form or another.

Create A Document

curl -X POST \
-d '{"_id":"random_task", "todo":"task 2", "done":false}' \
http://localhost:5984/new_database 
-H "Content-Type:application/json"

Note

  • The ID is set by user, and not left to couch to create, if so it would be long!!!

Create Multiple Documents

curl -X POST \
-d '{"docs": [{"todo":"task 3", "done":false}, {"todo":"task 4", "done":false}]}' \
http://localhost:5984/new_database/_bulk_docs \
-H "Content-Type:application/json"

Response
[
{"ok":true,"id":"093d8761607160e987eb5a2594000dd3","rev":"1-778fd61f8f460d0c1df1bb174279489d"},{"ok":true,"id":"093d8761607160e987eb5a25940013b5","rev":"1-dc9e84861bba58e5cfefeed8f5133636"}
]

Here we add two docs in the one action. whoah!!!

Read the Docs back

Create a doc identified by id 1

ubuntu@artifact-db:~$ curl -X POST -d '{"_id":"1", "todo":"task 2", "done":false}' http://localhost:5984/new_database -H "Content-Type:application/json"
{"ok":true,"id":"1","rev":"1-bceeae3c4a9154c87db1649473316e44"}

Now read it back, based on its ID

ubuntu@artifact-db:~$ curl -X GET http://localhost:5984/new_database/1 -H "Content-Type:application/json"
{"_id":"1","_rev":"1-bceeae3c4a9154c87db1649473316e44","todo":"task 2","done":false}
ubuntu@artifact-db:~$ 

Tutorials
https://www.tutorialspoint.com/couchdb/index.htm

Couchbase HelloWorld
https://developer.couchbase.com/documentation/server/3.x/developer/java-2.0/hello-couchbase.html

#CouchDB Articial
http://www.drdobbs.com/jvm/apache-couchdb-and-java/223100116

Installation / Unix

https://www.digitalocean.com/community/tutorials/how-to-install-couchdb-and-futon-on-ubuntu-14-04

https://github.com/couchbase/couchbase-java-client

Remove existing Couch

sudo apt-get remove couchdb couchdb-bin couchdb-common -yf | Remove existing version

Install Couch DB

sudo add-apt-repository ppa:couchdb/stable -y sudo apt-get install couchdb -y

Verify

Deployed Artifacts

ubuntu@artifact-db:~$ couch
couch-config  couchdb       couchjs       
ubuntu@artifact-db:~$ couch

Is Couch Running

ubuntu@artifact-db:~$ curl localhost:5984
{"couchdb":"Welcome","uuid":"7eb307460de129b3b47b5db31c374d84","version":"1.6.1","vendor":{"version":"16.04","name":"Ubuntu"}}
ubuntu@artifact-db:~$ 

Create new DB

ubuntu@artifact-db:~$ curl -X PUT localhost:5984/new_database
{"ok":true}
ubuntu@artifact-db:~$ 

Operation CouchDB

Stop/Start

sudo stop couchdb

Contenders

Apache CouchDB, like Redis, Cassandra, and MongoDB, is a NoSQL databas

Connectivity

To ensure your couch db is accessible to the outside world, ensure the bind address is set to 0.0.0.0 in default.ini

Next Restart the couchdb service, or kill the process.

ubuntu@artifact-db:~$ netstat -nltp 
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:5984          0.0.0.0:*               LISTEN      -               
ubuntu@artifact-db:~$ 

To 

ubuntu@artifact-db:~$ netstat -nltp 
tcp        0      0 0.0.0.0:5984            0.0.0.0:*               LISTEN      -               
ubuntu@artifact-db:~$ netstat -nltp 

Config Files

/etc/couchdb/default.ini
/etc/couchdb/local.ini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment