Showing posts with label neo4j. Show all posts
Showing posts with label neo4j. Show all posts

Wednesday, December 24, 2014

Using Neo4j using core java API

Follow these steps to create a simple graph

Install neo4j  from here

Add the neo4j jar file neo4j-desktop-2.1.6 to the project from the folder Program Files (x86)\Neo4j Community\bin

Sample code

package blog.neo4j;
import java.io.File;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;

import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.Schema;
/**
 *
 * @author sony
 */
public class EmbeddedNeo4j {
    private static final String DB_PATH = "target/neo4j-hello-db";

  

    // START SNIPPET: vars
    GraphDatabaseService graphDb;

    // END SNIPPET: vars

    // START SNIPPET: createReltype
    private static enum RelTypes implements RelationshipType
    {
        FOLLOWEDBY,
        FOLLOWEROF,
        TWEETED,
        TWEETED_BY
    }
    // END SNIPPET: createReltype
    private static enum LabelTypes implements Label
    {
        USER,
        TWEET
    }


    void deletePreviousDatabase()
    {
        deleteFileOrDirectory( new File( DB_PATH ) );
    }

    void startDb()
    {
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
        registerShutdownHook( graphDb );
    }
    void createIndex()
    {
        IndexDefinition indexDefinition,indexDefinition1;
        try ( Transaction tx = graphDb.beginTx() )
        {
            Schema schema = graphDb.schema();
            indexDefinition = schema.indexFor( DynamicLabel.label( "USER" ) )
                    .on( "userid" )
                    .create();
            indexDefinition1 = schema.indexFor( DynamicLabel.label( "TWEET" ) )
                    .on( "tweetid" )
                    .create();
            tx.success();
        }
        try ( Transaction tx = graphDb.beginTx() )
            {
                Schema schema = graphDb.schema();
                schema.awaitIndexOnline( indexDefinition1, 10, TimeUnit.SECONDS );
            }
    }
    
    
    void create_nodes_relationships() 
    {
        try ( Transaction tx = graphDb.beginTx() )
        {
                   //Create nodes
            Node user1=graphDb.createNode(LabelTypes.USER);
            user1.setProperty("Userid", 1);
            user1.setProperty("Name","Siva");
            Node user2=graphDb.createNode(LabelTypes.USER);
            user2.setProperty("Userid", 2);
            user2.setProperty("Name", "Kumar");
            Node user3=graphDb.createNode(LabelTypes.USER);
            user3.setProperty("Userid", 3);
            user3.setProperty("Name", "Priya");

            Node tweet1=graphDb.createNode(LabelTypes.TWEET);
            tweet1.setProperty("Tweetid", 1);
            tweet1.setProperty("Text","Hello");
            Node tweet2=graphDb.createNode(LabelTypes.TWEET);
            tweet2.setProperty("Tweetid", 2);
            tweet2.setProperty("Text", "Hai");
            Node tweet3=graphDb.createNode(LabelTypes.TWEET);
            tweet3.setProperty("Tweetid", 3);
            tweet3.setProperty("Text", "Our lives are not our own. From womb to tomb, we are bound to others. Past and present. And by each crime and every kindness, we birth our future.");
            System.out.println("Nodes created");

            user1.createRelationshipTo(user2, RelTypes.FOLLOWEDBY);
            user1.createRelationshipTo(user2, RelTypes.FOLLOWEROF);
            user1.createRelationshipTo(user3, RelTypes.FOLLOWEDBY);
            user1.createRelationshipTo(user3, RelTypes.FOLLOWEROF);

            user2.createRelationshipTo(user1, RelTypes.FOLLOWEDBY);
            user2.createRelationshipTo(user1, RelTypes.FOLLOWEROF);
            user2.createRelationshipTo(user3, RelTypes.FOLLOWEDBY);
            user2.createRelationshipTo(user3, RelTypes.FOLLOWEROF);

            user3.createRelationshipTo(user2, RelTypes.FOLLOWEDBY);
            user3.createRelationshipTo(user2, RelTypes.FOLLOWEROF);
            user3.createRelationshipTo(user1, RelTypes.FOLLOWEDBY);
            user3.createRelationshipTo(user1, RelTypes.FOLLOWEROF);

            System.out.println("relationship created");

            //select nodes using index
            for(int i=1;i<=3;i++)
            {
                Node user,tweet;
                ResourceIterator nodelist1 = graphDb.findNodesByLabelAndProperty( DynamicLabel.label( "USER" ), "Userid", i).iterator();
                ResourceIterator nodelist2 = graphDb.findNodesByLabelAndProperty( DynamicLabel.label( "TWEET" ), "Tweetid", i).iterator();
                user=nodelist1.next();
                tweet=nodelist2.next();
                user.createRelationshipTo(tweet, RelTypes.TWEETED);
                tweet.createRelationshipTo(user, RelTypes.TWEETED_BY);
            }   
            tx.success();
        }
               
        try ( Transaction tx = graphDb.beginTx() )
        {
               
               ExecutionEngine engine = new ExecutionEngine(graphDb);
               for(int i=1;i<=3;i++)
               {
                ExecutionResult  result=engine.execute( "match (n {Userid: '"+i+"'}) return n" );
                Iterator n_column = result.columnAs( "n" );
                for ( Node node : IteratorUtil.asIterable( n_column ) )
                        {
                            node.setProperty("type", "user");
                        }
               }
               for(int i=1;i<=3;i++)
               {
                ExecutionResult  result=engine.execute( "match (n {Tweetid: '"+i+"'}) return n" );
                Iterator n_column = result.columnAs( "n" );
                for ( Node node : IteratorUtil.asIterable( n_column ) )
                        {
                            node.setProperty("type", "tweet");
                        }
                           
            
                }
               tx.success();
        }             
    }
        
    void removeData()
    {
        try ( Transaction tx = graphDb.beginTx() )
        {         
            for(int i=1;i<=3;i++)
                   {
                       Node user,tweet;
                       ResourceIterator nodelist1 = graphDb.findNodesByLabelAndProperty( DynamicLabel.label( "USER" ), "Userid", i).iterator();
                       ResourceIterator nodelist2 = graphDb.findNodesByLabelAndProperty( DynamicLabel.label( "TWEET" ), "Tweetid", i).iterator();
                       user=nodelist1.next();
                       tweet=nodelist2.next();
                       user.delete();
                       tweet.delete();
                   }

            tx.success();
        }
    }

    void shutDown()
    {
        System.out.println();
        System.out.println( "Shutting down database ..." );
        // START SNIPPET: shutdownServer
        graphDb.shutdown();
        // END SNIPPET: shutdownServer
    }

    // START SNIPPET: shutdownHook
    private static void registerShutdownHook( final GraphDatabaseService graphDb )
    {
        // Registers a shutdown hook for the Neo4j instance so that it
        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
        // running application).
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
            @Override
            public void run()
            {
                graphDb.shutdown();
            }
        } );
    }
    // END SNIPPET: shutdownHook

    private static void deleteFileOrDirectory( File file )
    {
        if ( file.exists() )
        {
            if ( file.isDirectory() )
            {
                for ( File child : file.listFiles() )
                {
                    deleteFileOrDirectory( child );
                }
            }
            file.delete();
        }
    }
    
    private  String[] parseDataRow (String row,int len)
    {
        String properties[] = new String[len];
        row = row.substring(1, row.length()-1);
        //System.out.println(row);
        for(int i=0;i
        {
            //System.out.println(i);
            if(row.charAt(0)=='u')
            {
                if(i!=len-1)
                {
                    boolean flag=false;
                    if(flag=(row.charAt(2)==','))
                    {
                        row = "u"+row.charAt(1)+row.substring(3);
                    }
                    properties[i]=row.substring(2, row.indexOf(row.charAt(1)+","));
                    if(flag)
                    {
                        properties[i] = ","+properties[i];
                    }
                    //System.out.println(properties[i]);
                    row=row.substring(row.indexOf(row.charAt(1)+",")+3);
                    //System.out.println(row);
                }
                else
                {
                    properties[i]=row.substring(2,row.length()-1);
                    //System.out.println(properties[i]);
                }
                
            }
            else
            {
                if(i!=len-1)
                {
                    properties[i]=row.substring(0, row.indexOf(","));
                   //System.out.println(properties[i]);
                    row=row.substring(row.indexOf(",")+2);
                    //System.out.println(row);
                }
                else               
                {
                    properties[i]=row;
                    //System.out.println(properties[i]);
                }
                
            }
        }
        return properties;
    }
    
    
}


Net-beans project implementing the source code can be found  here

The output of the project will be created in folder blog neo4j\target\neo4j-hello-db 
the data base can be viewed in  neo4j browser

sample output