Showing posts with label learning. Show all posts
Showing posts with label learning. Show all posts

Wednesday, July 13, 2016

Writing java client for Mongo DB


Following are the steps
1) Create a java project in one of the IDEs. I have used Eclipse.
2) Then add following libraries to the classpath
mongodb-driver-3.2.2.jar
mongodb-driver-3.2.2-javadoc.jar
bson-3.0.2.jar
mongodb-driver-async-3.2.2.jar
mongodb-driver-core-3.2.2.jar
mongo-java-driver-3.2.2.jar
All these libraries can be downloaded from the link here. If you like you can very well use maven instead of downloading.
3) Write a simple client code
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
public class MongoDBClient {
    public static void main(String[] args) throws Exception{
        MongoClient client = new MongoClient("localhost", 27017);
        MongoDatabase testDB = client.getDatabase("test");
     
        System.out.println("Dropping person collection in test database");
        MongoCollection<Document> collection = testDB.getCollection("person");
        collection.drop();
        System.out.println("Adding a person document in the person collection of test database");
        Document person = new Document("name", "Ashok Kumar Chava").append("age", 35).append("job", "none");
       
        collection.insertOne(person);
        person = new Document("name", "Advik Chava").append("age", 7).append("education", "PG");
        collection.insertOne(person);
        System.out.println("Now finding a persons using find");
        FindIterable<Document> persons = collection.find();
        MongoCursor<Document> personList=persons.iterator();
        while(personList.hasNext()){
            person=personList.next();
            System.out.printf("Person found, name is %s and age is %s education is %s job is %s\n", person.get("name"),
                    person.get("age"), person.get("education"), person.get("job"));
        }

        System.out.println("Closing client");
        client.close();
    }

}
4) Following will be the output of the above code
Adding a person document in the person collection of test database
Now finding a persons using find
Person found, name is Ashok Kumar Chava and age is 35 education is null job is none
Person found, name is Advik Chava and age is 7 education is PG job is null
Closing client

5) As you can see in the above code I have created a person Document and added few key values details to the Document. Also if you observe for the first person I completly ignored the eduction key value and for the second one I completely ignored the job key value. Then I am able to retrieve the values.
In the next section I will do some thing little more complicated.

Tuesday, July 12, 2016

Learning MongoDB - 1

 

MongoDB is a document oriented NOSQL DB. MongoDB supports linear scalability.

What is NOSQL DB?

In simple words nosql is nothing but non sql or non relational. Basically nosql DB allows storing and retrieving of non relational or tabular data. So this is quite opposite of what oracle and other RDBMS does.

Why we need NOSQL DB?

You can find lot of information in this page.

What is linear scalability?

Scalability is nothing but capacity of the system,network or process to handle more work. Linear scalability mens increase of the capcity of the syetem,process or network by adding more hardware instead of making changes at the code level.

What are the types of NOSQL DBs?

There are multiple types of NOSQL DBs like key-value stores, document databases, wide-column stores, and graph databases.

Where to download MongoDB for learning?

You can download it from this link.

Installing the mongoDB on windows?

Just double click the downloaded file and follow the instructions.

Starting mongoDB?

  • First create a data folder for mongoDB. I have created mine as D:\MongoDB\data
  • Also create a log folder mine is like D:\MongoDB\logs
  • Create a config file under install directory, simple config file D:\MongoDB\Server\3.2\bin\first.cfg looks like

systemLog:
    destination: file
    path: D:\MongoDB\logs\mongod.log
storage:
    dbPath: D:\MongoDB\data

  • Start MongoDB by going to command prompt and to the directory D:\MongoDB\Server\3.2\bin, there execute the command

mongod –config first.conf

  • Check the log file to see if the mongod started propertly you should be seeing a message like “waiting for connections on port 27017”.

Connecting to MongoDB?

Execute the command “mongo” you will see the follwing prompt

D:\MongoDB\Server\3.2\bin>mongo
2016-07-13T09:44:22.613+0530 I CONTROL  [main] Hotfix KB2731284 or later update is installed, no need to zero-out data files
MongoDB shell version: 3.2.7
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
>

Also the DB can be accessed using http://localhost:27017/test

This is end of part 1.

Next part covers programatic way to connect to MongoDB.

 


 

 

 

Tuesday, May 15, 2012

Scala Learning–Day 1

- REPL - Read-Evaluate-print loop
- Scala uses interpreter, but internally it uses a compiler to convert you code to byte code and run it on JVM.
-IN scala there are two ways of defining variables using "val" and "var". Val is like defining constant for which you cannot change value once assigned.
- IN scala we are encouraged to use Val unless you are really need to change the contents. Note that you need not specify the type of a value or variable.
- in case you need to specify a type then you use the below given way to do the same
val greeting: String=null
val greeting: Any="hello"
- IN scala, the type of a variable or function is always written after the name of the variable or function.
- Scala line does not to end with semi-colon, its only needed when we have multiple statements in a single line.
 
val xmax,ymax=100
var greeting, message:String=null
- Like Java, Scala got 7 numeric types and one Boolean type(Byte, Char,Short, Int,Long, Float, Double and Boolean). The difference is unlike java these types are Classes, which means there are no primitive data types.
With this you can execute methods on numbers
1.toString() which will written "1"

- Scala uses java.lang.String for defining String but it has StringOps which is more power full with hundreds of options. Similarly Scala got RichInt, RichDouble etc. to provide better options on the types.
- IN scala we use methods, not casts, to convert between numeric types
- The operators like +-*/%&|^>><< are actually methods though they just work the way they work in java or c++.
- IN scala static methods need not be called from a class. Lot of mathematical functions are defined in scala.math and we can import this using
import scala.math._ (_ is the wild char in scala like * in java).
- Scala does not have static methods instead it got "Singleton Objects".
- Scala classes got "companion objects" whose methods, whose methods just act like static methods.
- Scala methods with out arguments does not use parentheses. For example StringOps distinct does not have any arguments which you can use like "Ashok".distinct which will written all the distinct char in the string. One important note here is parameter less method that does not modify the object has no parentheses.
- In scala if we use "Ashok"(2) will written 'h'. It just works like "Ashok".charAt(2) in java. You can think this as an overloaded form of () operator.It is implemented with a method name "apply". In this StringOps its defined as given below
def  apply(n:Int):Char

- Downloaded scala from http://www.scala-lang.org/downloads. As I use windows 7 I downloaded the msi file and installed with out any issues.
- After installation check the path in the system variables for some reason I found that the path is like "C:\Program Files (x86)\scala\\bin", remove the extra '\' then start the command prompt and type "scala" which will you REPL. It running slow compared to python console. May be its because of the JVM.

Configuring eclipse  to program in scala


- Got Eclipse Indigo and extracted the files from ZIP file.
- The went to http://scala-ide.org/download/current.html and found the link to download site for scala IDE for eclipse. http://download.scala-ide.org/releases-29/stable/site
- Started eclipse from Help->New Software and downloaded the plug-in and installed it.