Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Thursday, 18 August 2016

Installing MongoDB in Mac

Background

Sometime back I had written a post on MongoDB. Installation and basic syntax in Windows - Getting Started with MongoDB . This post is starting of series of posts on MongoDB. In this post we will see installation and configuration of MongoDB in Mac.

Installing MongoDB

  • Go to MongoDb download center and download the tgz of the latest build. Your OS should already be selected and you should see the download link.
  • You can also install MongoDB using homebrew
    • brew install mongodb  (To install the MongoDB binaries)
    • brew install mongodb --with-openssl (To install the MongoDB Binaries with TLS/SSL Support)
    • brew install mongodb --devel  (To install the latest development release)
  • Once you have downloaded that tarball you can unpack it using following command - 
    • tar xvf mongodb-osx-ssl-x86_64-3.2.9.tgz
  • Once you have unpacked it navigate to that folder and then into bin folder inside it. Here you will see list of programs. There are two programs here of out interest - 
    • mongo : This is the mongo shell used to connect to the mongo DB.
    • mongod : This is the actual server.
  • That's it your mongo db is installed and ready to be run.
NOTE : MongoDB by default stores its data in directory "/data/db". So make sure this directory is created with necessary permissions. 
  • sudo mkdir -p /data/db
  • sudo chmod -R 777 /data/

If you want to give a custom path you can give it when you start mongodb - 
  --dbpath arg                          directory for datafiles - defaults to "/data/db"
Eg.
  • mongod --dbpath /data/mydb

Running and connecting MongoDB

  • To run MongoDB server go to bin directory and start mongod program.
    • ./mongod
  • This should start your mongodb server listening on default port 27017.
  • To see all mongod configuration options you can type - 
    • ./mongod --help



Once mongodb is up you can try connecting to it using mongo shell.
You can start mongo shell by
  • ./mongo
You can also see incoming connection in your server console -
  • 2016-08-18T22:34:27.315+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:56356 #1 (1 connection now open)
     
Once you are connected you can try following operations - 
  • use mydb
  • db
  • show dbs
  • db.testCollection.insert({"Name":"John"})
  • show collections
  • db.testCollection.find()
 That's it for the basic of installing Mongo DB on your Mac. To avoid always going till bin to execute your programs you can do either of of the follwing -
  1. Add you path to bin to your PATH and export it. OR
  2. Add all these binaries to /usr/local/bin/ by
    • cp * /usr/local/bin
  3. You can verify it by wither running - 
    1. which mongod OR
    2. mongod --help

Related Links

Saturday, 15 November 2014

Creating a simple JSON parser for parsing various similar response types.

Background

Many a times it so happens that a particular service or APIs return a set of similar resources. So how do we handle it in a client that uses these APIs or services. One way is that you check the incoming response against all the exhaustive possible list of responses . One would match and you will have your result. But that's very bad programming. So many if-else statements. Plus so much computing time wastage. Surely not a way to go for production code. In this post I will show how can write a simple parser that parses such requests.


Assumption and Understanding of task

Before we go to the code let me explain the problem which we are going to solve with the parser. Assume there is a Service or an API that returns an Animal. For example you want a pet you may ask for Dog or a Cat. If you want to participate in horse racing you may ask for a Horse or a Hen for poultry etc. Point is we have to build a client that can parse the incoming requests. So if someone asks for a Dog we should parse the response for a Dog and prove it is indeed a Dog.

For simplicity of code and understanding I am just going to work with Class Dog and Class Cat. To differentiate between them I am adding bark() method in Dog class and drink() method in Cat class.
Also we have a super class called Animal. Both Dog and Cat class extend Animal Class. We will leverage the concept of polymorphism

Also I am assuming that the response we get has a basic structure 

{
     "type":type of animal
}

It may have additional attributes depending on the type of animal. For Cat and Dog I have added another attribute called isPet. What will be more important is the type which is common to all the responses.

To the Code

As usual I am using Eclipse with Ivy dependency manager. We need GSON library to handle json data. My Ivy file looks like -



<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info
        organisation="OpenSourceForGeeks"
        module="JsonParser"
        status="integration">
    </info>
    
    <dependencies>
        <dependency org="com.google.code.gson" name="gson" rev="2.2.4"/>
    </dependencies>
</ivy-module>

Next lets create our model classes. We will have three model classes - Animal, Dog and Cat. Overall my project structure looks like follows -



Animal.java

package opensourceForGeeks.models;

public class Animal {
    
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

Dog.java

package opensourceForGeeks.models;

public class Dog extends Animal {
    
    boolean isPet;
    
    public boolean isPet() {
        return isPet;
    }

    public void setPet(boolean isPet) {
        this.isPet = isPet;
    }

    public void bark()
    {
        System.out.println("Dog is barking...");
    }

}

Cat.java

package opensourceForGeeks.models;

public class Cat extends Animal {
    
    boolean isPet;
    
    public boolean isPet() {
        return isPet;
    }

    public void setPet(boolean isPet) {
        this.isPet = isPet;
    }

    public void drink()
    {
        System.out.println("Cat is drinking milk...");
    }
    
    
}

Next lets write our actual parser logic - JsonParser.java

package opensourceForGeeks.parser;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;

import opensourceForGeeks.models.Animal;
import opensourceForGeeks.models.Cat;
import opensourceForGeeks.models.Dog;

public class JsonParser {
    
    private static final Map<String, Class> ANIMAL_MAPPING = new HashMap<String, Class>();
    private static final Gson gson = new Gson();
    
    static
    {
        ANIMAL_MAPPING.put("dog", Dog.class);
        ANIMAL_MAPPING.put("cat", Cat.class);
    }
    
    public Animal parseAnimal(String jsonString)
    {
        try
        {
            Animal animal = gson.fromJson(jsonString, Animal.class);
            return gson.fromJson(jsonString, ANIMAL_MAPPING.get(animal.getType()));
        }
        catch (Exception ex)
        {
            System.out.println("Exception occured while parsing Animal json string");
            return null;
        }
        
    }

}

and finally lets test it - JSONParserTest.java

package opensourceForGeeks.test;

import opensourceForGeeks.models.Cat;
import opensourceForGeeks.models.Dog;

public class JSONParserTest {
    
     private static final String DOG_JSON = "{\"type\": dog,\"isPet\": true}";
     private static final String CAT_JSON = "{\"type\": cat,\"isPet\": true}";
    
    public static void main(String args[])
    {
        opensourceForGeeks.parser.JsonParser parser = new opensourceForGeeks.parser.JsonParser();
        ((Dog) parser.parseAnimal(DOG_JSON)).bark();
        ((Cat) parser.parseAnimal(CAT_JSON)).drink();
    }

}


and the output is as expected - 

Dog is barking...
Cat is drinking milk...

Understanding the logic

What we have done in our JsonParser class is as follows -

  • We first create a map of type(exhaustive list of possible types of Animals) and their model classes.
  • All type of Animals extend Class Animal which has the type that will be common to all the JSON responses.
  • So we first convert the json string into Animal object which in turn provides us the information about what type of Animal it is.
  • Then we use this information to get the model class used to describe that type of Animal and again parse the json String to covert it into that specific type of Animal and finally return it.
  • Yes we have used polymorphism here to make our task easier!




Wednesday, 13 August 2014

Using HTTP POST and GET using cURL

Background

cURL is a command line utility to perform GET and POST requests. It comes really handy for testing REST APIs. For debian based Linux systems you can simply install it

  • sudo apt-get install curl

But you can install  it in windows as well. You can use it from cygwin as well (select the appropriate package while installing). For installing cURL in windows I had written a post some time back. You can refer the same -


That's about installation. In this post we will see the usage if cURL command.



Using cURL command

As we know there are two majorly used REST APIs - GET and POST. Lets see how to perform these actions using cURL - 

GET

  1. With json :

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

  2. With XML :

    curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST


  1. For posting data :

    curl --data "param1=value1&param2=value2" http://hostname/resource

  2. For File Upload :

    curl --form "fileupload=@filename.txt" http://hostname/resource

  3. RESTful HTTP post :

    curl -X POST -d @filename http://hostname/resource

  4. For logging into a site (auth):

    curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
    curl -L -b headers http://localhost/

POST with some JSON data

You can use -H to set the header while using cURL command 

Example  - 
  • -H "Content-Type: application/json"
So your complete command would look something like -

curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

Difference between GET and POST


Related Links


Tuesday, 1 July 2014

Getting Started with MongoDB

Introduction

From Wiki

MongoDB (from "humongous") is a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster. Released under a combination of the GNU Affero General Public License and the Apache License, MongoDB is free and open-source software.


Getting Started

  1. Download MongoDB from their website and install it. You have installer file for windows or zip file. Either way you should get MongoDB 2.6 Standard  folder installed. My folder location is C:\Program Files\MongoDB 2.6 Standard. You can find a bin file inside this folder. Add it to the %PATH% environment variable (windows) or export it by adding in .bashrc file(Linux).
  2. Next open command prompt and run MongoDB server. To do so execute command

    mongod --dbpath C:\Users\athakur\Softwares\MongoDB

    Replace the path after --dbpath with your path. It basically says where your db files will be stored. If you do not provide db path your server will not start and will show that dbpath was missing.


    After starting the server with --dbpath


    You may be asked to allow network access to mongo db. Provide the required access.

  3. Next start the client by typing

    mongoThat should open the Mongo shell.


    By default, mongo looks for a database server listening on port 27017 on the localhost interface. To connect to a server on a different port or interface, use the --port and --host options.
  4. Lets create a new database. Type the following command

    use mydb

    and then type

    db

    This should show mydb database. Now try doing

    show dbs

    Nothing? Surprised? Don't be. It is the way Mongo DB behaves. MongoDB will not permanently create a database until you insert data into that database.

    Note :
    Unlike MySQL database you do not create tables and then insert data. Infact there is no relational model here. You simply switch db using use dbName syntax and add data. When you actually add the data the database is created.

  5. Lets go ahead and insert some data in our database. First create documents doc1 and doc2 using

    doc1 = {name : "Aniket"}
    doc2 = {org : "openSourceForGeeks"}


    Then add these documents to the collection (lets call it testDataCollection) database using

    db.testDataCollection.insert( doc1 )
    db.testDataCollection.insert( doc2 )


        When you insert the first document, the mongod will create both the mydb database and the testDataCollection collection.

    You can view your collections by

    show collections

    Confirm that your testDataCollection collection is created.

    The mongo shell will return the list of the collections in the current (i.e. mydb) database. At this point, the only collection is testDataCollection . All mongod databases also have a system.indexes collection.

    Also confirm the database is created ( by show dbs).

  6. Data Stored in MongoDB is essentially JSON. If you wish to see the data in the collection you can use command

    db.testDataCollection.find()

  7. That;s all for basics of mongo db. We say how to create database, collections and insert data in it. We also saw how can we retrieve the data.


Important commands

  1. db - shows current db in use
  2. help - show help
  3. cls - clear screen
  4. exit - exit Mongo shell
  5. show dbs - Show list of databases
  6. use mydb - use some other database
  7. show collections - Show all collection for the db. 

Important Links

Wednesday, 25 June 2014

How to Use JSON Objects With Twitter Bootstrap Typeahead

Background

Bootstrap is a free collection of tools for creating websites and web applications. It contains HTML and CSS-based design templates for typography, forms, buttons, navigation and other interface components, as well as optional JavaScript extensions. (More on Wiki).

You can visit their official site for more details. You need to download it and add the css file and javascript file to your project. In this post i am going to cover how can we use JSON Object to autocomplete text fields.




To the Code.....

Add the following in your java script. You can put all typeahead calls in a single function and then call that function from your javascript in the HTML (or JSP) page.

var states = [];
var    map = {};
$('#search').typeahead({

    minLength: 0,

    source : function(query, process) {        

        states = [];

        map = {};
        //hardcoding this JSON but this would technically come from server
        var data = [
        {"stateCode": "CA", "stateName": "California"},
        {"stateCode": "AZ", "stateName": "Arizona"},
        {"stateCode": "NY", "stateName": "New York"},
        {"stateCode": "NV", "stateName": "Nevada"},
        {"stateCode": "OH", "stateName": "Ohio"}
    ];

    $.each(data, function (i, state) {
        map[state.stateName] = state;
        states.push(state.stateName);
    });

    process(states);
    },
    matcher: function (item) {
        //If user enters * show all possible entries
        if(this.query.trim().toLowerCase() == "*") {
            return true;
        }
        if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) {
            return true;
        }
    },
    updater: function (item) {
        //If you want to do something prior to updating value 
        selectedState = map[item].stateCode;
        return item;
    },
    sorter: function (items) {
        return items.sort();
    },
    highlighter: function (item) {
       // implementation
    },

});

Also you can override your typeahead class as follows for better looks

.typeahead {
max-height: 200px;
overflow-y: auto;
overflow-x: hidden;
}


Thats it for the autocomplete feature of twitter bootstrap. You can make ajax call to get the JSON data as follows

function getData(url, data, successCallback, errorCallback) {
    var requestType = "POST";
    var dataType = "json";
    make_ajax_call(url, data, requestType, dataType, successCallback, errorCallback);
}

function make_ajax_call(url, data, requestType, dataType, successCallback, errorCallback) {
    
    var request = {
        url : url,
        type : requestType,
        data : data,
        dataType : dataType,
        success : successCallback,
        error : errorCallback,
        async : false
    };
    jQuery.ajax(request);

}
You can import jquery using

<script src="http://code.jquery.com/jquery.js"></script>

Sunday, 19 May 2013

Accesing private variables using Reflection API

Reflection API in Java comes very handy to test class variables and methods at runtime. You can call it a type of hack as it violates the principle of Data Encapsulation. Let us see a code that demonstrates how can we access private variables using Reflection API and even edit it.

Code :


public class Someclass {
    private String name = "John";

}

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String args[]) throws Exception
    {
       
Someclass myClass = new Someclass();
        Field fs = myClass.getClass().getDeclaredField("name");
        fs.setAccessible(true);
        System.out.println("Variable is " + fs.getName() + " and value is " + fs.get(myClass));
        fs.set(myClass, "Sam");
        System.out.println("Variable is " + fs.getName() + " and value is " + fs.get(myClass));
    }
}

Output :  



Explanation : 

          Note that the private variable name is in the Someclass class and our main method is in the Test class. According to OOP principles we should not be able to access the private variable name. Using the instance of the class myClass we get the declared field with name name. Then we set it accessible property to true. Then we can access it and print it(as shown in the output). Moreover we can also change it's value using Reflection API. fs.set(myClass, "Sam") method does that.

For more details you can refer to it's documentation.
  
t> UA-39527780-1 back to top