Wednesday, November 23, 2016

Postgres cheatsheet



psql -h <hostname> -U <username> -d <database name> -p 8192
\l - lists all available databases
\c <database name> - example, \c mydb will switch to the database 'mydb'
\d - this will show list of relations including procedures/view, tables, sequence in default schema
\dt - this will show the list of tables in a default schema
\dn - show list of schemas
\dt <schema_name>.* - list tables in a the schema
\df+ <schema_name>.* - list out all procedures

http://alvinalexander.com/blog/post/postgresql/list-postgresql-slash-commands

Sunday, September 20, 2015

MongoDB CRUD with examples


MongoDB is a document-based database where there are no tables or SQL queries. Records are stored as documents JSON format

MongoDB CRUD Operations Introduction:

In MongoDB, records are stored as JSON format, that is like key-value pairs. This structure of storing as key-value pairs is similar to dictionaries, hashes, maps or associative-arrays in programming languages like Python, Perl, Java, Go etc. CRUD stands for Create, Read, Update and Delete operations in Mongo. Install MongoDB and Mongo Shell in your workstation to play with the query operations explained below.

Jump into Mongo shell,
Connect to mongo shell from your workstation(I have a datacenter cluster that has mongo to store lot of data about the cluster),
root@mycluster:~# mongo
MongoDB shell version: 2.0.4
connecting to: test
>
> show dbs  (equivalent to show databases in mysql, this command will list all databases)
school    0.078GB
students  0.078GB
test    (empty)

Here 'school', 'students' and 'test' are the two databases or call in as documents in mongo way.
Now connect to 'events' document and list out all the tables(collections) in the 'events' database(document)

> use students  (equivalent to use '<database_name>' in mysql)
switched to db events
>
> show collections (equivalent to 'show tables' in mysql)
grades
system.indexes
>

In the 'grades' collections, there are 20 documents as given below. Please read the documents with more attention, we will run all below explained query on these document,
> db.grades.find()
{ "_id" : ObjectId("50906d7fa3c412bb040eb577"), "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb578"), "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb579"), "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57a"), "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57b"), "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57d"), "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57e"), "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57f"), "student_id" : 2, "type" : "exam", "score" : 19.88180838833524 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb580"), "student_id" : 2, "type" : "quiz", "score" : 1.528220212203968 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb581"), "student_id" : 2, "type" : "homework", "score" : 60.9750047106029 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb582"), "student_id" : 2, "type" : "homework", "score" : 97.75889721343528 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb583"), "student_id" : 3, "type" : "exam", "score" : 92.6244233936537 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb584"), "student_id" : 3, "type" : "quiz", "score" : 82.59760859306996 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb585"), "student_id" : 3, "type" : "homework", "score" : 50.81577033538815 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb586"), "student_id" : 3, "type" : "homework", "score" : 92.71871597581605 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb587"), "student_id" : 4, "type" : "exam", "score" : 87.89071881934647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb588"), "student_id" : 4, "type" : "quiz", "score" : 27.29006335059361 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb589"), "student_id" : 4, "type" : "homework", "score" : 5.244452510818443 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb58a"), "student_id" : 4, "type" : "homework", "score" : 28.656451042441 }
>



Queries covered in this blog are,

db.grades.find()
  •    Finding documents with operations like greater than, lesser than etc as below
    • $gt, $gte, $lt, $lte (greater than, greater than equals, lesser than, lesser than equals)
    • $exists ( if any document with given attribute exists)
    • $type (BSON type-Binary of JSON type, imagine as data-type in programming language),
      • String type is 2
      • Array type is 4
      • Date 5
db.grades.findOne()
db.grades.insert()
db.grades.update()
db.grades.remove()

Above queries with equivalent SQL queries,
> db.grades.find({}, {score:1}) //select score from grades;
{ "_id" : ObjectId("50906d7fa3c412bb040eb577"), "score" : 54.6535436362647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb578"), "score" : 31.95004496742112 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb579"), "score" : 14.8504576811645 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57a"), "score" : 63.98402553675503 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57b"), "score" : 74.20010837299897 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57d"), "score" : 21.33260810416115 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57e"), "score" : 44.31667452616328 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb57f"), "score" : 19.88180838833524 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb580"), "score" : 1.528220212203968 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb581"), "score" : 60.9750047106029 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb582"), "score" : 97.75889721343528 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb583"), "score" : 92.6244233936537 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb584"), "score" : 82.59760859306996 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb585"), "score" : 50.81577033538815 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb586"), "score" : 92.71871597581605 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb587"), "score" : 87.89071881934647 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb588"), "score" : 27.29006335059361 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb589"), "score" : 5.244452510818443 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb58a"), "score" : 28.656451042441 }
> db.grades.find({score: {$gt: 95}}) //select * form grades where score > 95
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb582"), "student_id" : 2, "type" : "homework", "score" : 97.75889721343528 }
>
> db.grades.find({type: "quiz", score:{$gt:60}}) 
//select * form grades where type='quiz' and score > 60
{ "_id" : ObjectId("50906d7fa3c412bb040eb57c"), "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : ObjectId("50906d7fa3c412bb040eb584"), "student_id" : 3, "type" : "quiz", "score" : 82.59760859306996 }
>




Tuesday, September 15, 2015

Git for Newbies


Git Quick Start

I summarized the commonly used git commands from my experience, this will help to certain extend to get quick hands-on,

Creating a Branch:

Use following commands to create a branch locally and push it to remote server

  • git checkout -b <newbranchname> - This will create you a branch locally from a master branch
  • git push origin <newbranchname> -This command will push your local branch to remote server  
  • git clone <url> (for example: git clone https://github.com:kathir2903/gpibcontroller.git) - This command will download the entire project and its version history
Deleting a Branch:

Branch deletion is two step process, first delete the local branch and push the changes to remote, which will delete the remote branch as well.

  • git branch -D <branchname> - This command will delete the branch locally
  • git push origin <branchname> - This command will delete remote branch
Add, Commit, Push code to a Branch:
  • git add <filename>  This command will add file to the local branch in preparation for version
  • git commit -m <Commit message> This command will commit your changes that were added for tacking 
  • git push origin <branchname> This command will push your changes from local to remote
  • git branch -r  This command will list remote branches
Undo commits:
  • git reset <commit_id>  This command will undoes all commits after <commit_id> given.
  • git reset --soft <commit_id> This command will keep your local changes in the working directory
  • git reset --hard <commit_id> This command will reset to the commit_id with wiping of the changes you have made locally.

Stashing and Switching between branches:

There will be a case that you are working in  a branch and still have lot more to work on that branch, but  you want to switch to another branch. In such case, use 'git stash' command to cache the changes you have made so far to the branch. Use these commands,
  • git stash - This command will save your changes in the branch you are work on
  • git stash list - This will return the list of files that you have made changes and stashed.
Now you are ready to switch to another branch without messing up with the current working branch,
  • git checkout <anotherbranchname> - This command is to checkout a different branch (note: -b option will create you a local branch for you which you don't want here)
You can now work on this 'anotherbranchname', once you are done with it and want to switch to the previous branch simply do this,
  • git checkout <oldbranchname>
  • git stash pop - This will pop up to the latest stash list that you were working on before switching to another branch
Commit logs and version history reviews:

Two important commands that will be used on daily basis to review the commit logs and history versions are 'show ' and 'log'

  • git log - This command will list the history of current branch
  • git log <filename> - This command will list the version history for a file
  • git show - This command will output the list of files and internal changes(line-by-line code) you have made the files.
  • git diff <filename> - This command will show the differences between your local changes to the file and the branch
Merging and Cherry-picking:

When you want to merge your changes in your private branch to the master branch. First, checkout master branch, update the local branch with origin/master, then do merge your private branch

  • git checkout master
  • git pull
  • git merge <you_private_branch>
  • git push origin master
Another way to do is with cherry-pick, this will apply changes to the master introduced by your any existing commits(it could be from any branch),
  • git checkout master
  • git pull
  • git cherry-pick <commit_id>
  • git push origin master
This is good link to read on cherry-pick, http://git-scm.com/docs/git-cherry-pick

To read more git internals, here is the ebook which is very good,

https://drive.google.com/file/d/0B-TqU5Xu5JToVnljNlN6VGxvME0/view?usp=sharing