Februari 14, 2012

Using OrientDB - Tinkerpop Blueprints from NetBeans

Using OrientDB from NetBeans is pretty easy. Here I will give you some of the steps needed for the uninitiated. NetBeans version is not so important, although in this posting I use NetBeans 7.1. To follow this article, you need OrientDB (as this is about Tinkerpop Blueprints, of course you need orientdb-graphed package - mine is 1.0RC8), JDK (mine is JDK7_u2), and NetBeans (mine is 7.1). This article assume that we will use remote mode URL from OrientDB to connect to database. Here I just use tinkerpop database which already exist in $ORIENTDB_HOME/database/tinkerpop. So, first time is fire up your server:
$ server.sh

Open NetBeans and create your project (File - New Project - Java - Java Application) and put this:


Our project structure looks like this:


Next, we should manage OrientDB library. Choose Tools - Library Manager and put all of the jar files in $ORIENTDB_HOME/lib into one container like this (mine is called OrientDB-Tinkerpop):


After that, we put the library into our project by choosing File - Project Properties (FirstOrientDB) - Libraries then add OrientDB-Tinkerpop library into our project:


That's all the steps needed to prepare our project. For the source code part, it won't be too difficult also since we have the example from Tinkerpop Blueprints wiki (although they use Neo4J as an example). The class for OrientDB is OrientGraph. So, put this source code inside FirstOrientDB.java:
package firstorientdb;

import com.orientechnologies.common.exception.OException;
import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph;

/**
 *
 * @author bpdp
 */
public class FirstOrientDB {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

 OrientGraph graph = null;

        try {

            graph = new OrientGraph("remote:localhost/tinkerpop", "admin", "admin");
            System.out.println("Success");

        } catch (OException e) {
  
            System.out.println("Not succeed - " + e.getMessage());
  
        } finally {

            if( graph != null )
                graph.shutdown();

        }

    }
    
}
In the firstime, usually you have error from NetBeans. This error comes from library which should be imported. Instead of writing import statement by myself, I let the error happened and then resolve it by using Ctrl-Shift-I. From the source code, you may see that the connection was done by OrientGraph class. Its parameters are URL, username, password. We can see also that we can catch error by catching OException, the base Exception class in OrientDB. Here is the result when you press F6 or run the project:


If you can make it until this step, then you may now use Tinkerpop with your OrientDB inside your NetBeans project. Happy hacking!

0 comments:

Posting Komentar