Wednesday, September 21, 2011

My First Example of OrientDB.!




Really it is fun to be working with NoSQL .. !! :) Had a great time.


package com.varra.nosql.db;

import java.io.IOException;

import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.db.object.ODatabaseObjectTx;
import com.orientechnologies.orient.core.iterator.OObjectIteratorMultiCluster;

/**
 * The Class Inserter.
 *
 * @author Rajakrishna V. Reddy
 *
 *         TODO description go here.
 */
public class Inserter
{
   
    /** The db. */
    private ODatabaseObjectTx db;
   
    /**
     * Instantiates a new inserter.
     * @throws IOException
     */
    public Inserter() throws IOException
    {
        //Orient.instance().registerEngine(new OEngineRemote());
        ensureOpen();
    }
   
    /**
     * @throws IOException
     *
     */
    private void ensureOpen() throws IOException
    {
        db = new ODatabaseObjectTx("remote:localhost/rapid");
        final OServerAdmin serverAdmin = new OServerAdmin("remote:localhost/rapid");
        serverAdmin.connect("root", "D4D38DD44E5264275CA74C61CACD9CB36634E2DDBC1EDC6129F99C72B519EEC9");
        if (!serverAdmin.existsDatabase())
        {
            serverAdmin.createDatabase("local");
        }
        serverAdmin.close();
       
        db.open("admin", "admin");
        db.getEntityManager().registerEntityClasses("com.varra.nosql.pojo");
    }

    /**
     * Save.
     *
     * @param object
     *            the object
     */
    public void save(Object object)
    {
        System.out.println("Saving: "+object);
        try
        {
            db.save(object);
            // db.commit();
        }
        finally
        {
            db.close();
        }
    }

/**
     * The main method.
     *
     * @param args
     *            the arguments
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void main(String[] args) throws IOException
    {
        final Inserter inserter = new Inserter();
        final Animal animal = new Animal();
        animal.setName("CAT");
        animal.setAlive(true);
        animal.setNoOfLegs(4);
       
        inserter.save(animal);
        OObjectIteratorMultiCluster<Object> oObjectIteratorMultiCluster = inserter.get(CAT.class.getSimpleName());
        for (Object object : oObjectIteratorMultiCluster)
        {
            System.out.println(object);
        }
    }
}



/**
 *
 */
package com.varra.nosql.pojo;

import javax.persistence.Id;

/**
 * The Class Animal.
 */
public class Animal
{
    @Id
    /** The id. */
    private Object id;
   
    /** The name. */
    String name;
   
    /** The alive. */
    boolean alive;
   
    /** The no of legs. */
    int noOfLegs;
   
    /** The color. */
    String color;
   
    /**
     * Instantiates a new animal.
     */
    public Animal()
    {
        // TODO Auto-generated constructor stub
    }
   
    /**
     * Gets the name.
     *
     * @return the name
     */
    public String getName()
    {
        return name;
    }
   
    /**
     * Sets the name.
     *
     * @param name
     *            the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }
   
    /**
     * Checks if is alive.
     *
     * @return the alive
     */
    public boolean isAlive()
    {
        return alive;
    }
   
    /**
     * Sets the alive.
     *
     * @param alive
     *            the alive to set
     */
    public void setAlive(boolean alive)
    {
        this.alive = alive;
    }
   
    /**
     * Gets the no of legs.
     *
     * @return the noOfLegs
     */
    public int getNoOfLegs()
    {
        return noOfLegs;
    }
   
    /**
     * Sets the no of legs.
     *
     * @param noOfLegs
     *            the noOfLegs to set
     */
    public void setNoOfLegs(int noOfLegs)
    {
        this.noOfLegs = noOfLegs;
    }
   
    /**
     * Gets the color.
     *
     * @return the color
     */
    public String getColor()
    {
        return color;
    }
   
    /**
     * Sets the color.
     *
     * @param color
     *            the color to set
     */
    public void setColor(String color)
    {
        this.color = color;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append("Animal [alive=");
        builder.append(alive);
        builder.append(", color=");
        builder.append(color);
        builder.append(", name=");
        builder.append(name);
        builder.append(", noOfLegs=");
        builder.append(noOfLegs);
        builder.append(", id=");
        builder.append(id);
        builder.append("]");
        return builder.toString();
    }
}

Output:

Saving: CAT [super=Animal [alive=true, color=null, name=CAT, noOfLegs=4, id=null]]
CAT [super=Animal [alive=true, color=null, name=CAT, noOfLegs=0, id=#5:0]]




No comments:

Post a Comment