Neo4j初步测试

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.kernel.EmbeddedGraphDatabase;

public class Test{
/**
 * 关系枚举
 * @author zhangk
 *
 */
	public enum FirstRelationshipTypes implements RelationshipType {
		FIRST
	}


    /**
     * 获得数据库管理类
     * @param graphPath
     * @return
     */
    public static GraphDatabaseService getDbService(String graphPath) {
        return new EmbeddedGraphDatabase(graphPath);
    }

    public static void createData(String graphPath) {
        GraphDatabaseService db = getDbService(graphPath);
        registerShutdownHook(db);  
        Transaction tx = db.beginTx();  
        try {
            Node firstNode = db.createNode();
            Node secondNode = db.createNode();
            firstNode.setProperty("name", "zhang");
            secondNode.setProperty("name", "kai");
            // 连接两个节点
            firstNode.createRelationshipTo(secondNode, FirstRelationshipTypes.FIRST);
            String zhangkai = firstNode.getProperty("name") + " " + secondNode.getProperty("name");
            System.out.println(zhangkai);
            //Relationship relationship = firstNode.getSingleRelationship(FirstRelationshipTypes.FIRST, Direction.OUTGOING);
            Iterable<Relationship> rls = firstNode.getRelationships();
            for (Relationship relationship : rls) {
                System.out.println(relationship.getId());  
                System.out.println(relationship.getEndNode().getProperty("name"));  
                System.out.println(relationship.getStartNode().getProperty("name"));  
                relationship.delete(); 
            }
             
//            firstNode.delete();  
//            secondNode.delete();  
            tx.success();  
        } catch(Exception e) {
            tx.finish();
        }
        System.out.println("Shutting down database ...");  
        db.shutdown();
    }


    /**
     *  实时监测服务状态
     *  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 example before it's completed)  
     *  @param graphDb
     */
    private static void registerShutdownHook(final GraphDatabaseService graphDb) {  

        Runtime.getRuntime().addShutdownHook(new Thread() {  
            @Override  
            public void run() {  
                graphDb.shutdown();  
            }  
        });  
    }  


    private static final String GPATH = "graph.db"; // 数据库地址

    public static void main(String[] args) {
        createData(GPATH);
    }
}



import java.io.File;
import java.io.IOException;
import org.neo4j.graphdb.Direction;
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.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.FileUtils;


public class EmbeddedNeo4j
{
    private static final String DB_PATH = "target/neo4j-hello-db";
    String greeting;
    // START SNIPPET: vars
    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship;
    // END SNIPPET: vars


    // START SNIPPET: createReltype
    private static enum RelTypes implements RelationshipType
    {
        KNOWS
    }
    // END SNIPPET: createReltype


    public static void main( final String[] args )
    {
        EmbeddedNeo4j hello = new EmbeddedNeo4j();
        hello.createDb();
        hello.removeData();
        hello.shutDown();
    }


    void createDb()
    {
        clearDb();
        // START SNIPPET: startDb
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
        registerShutdownHook( graphDb );
        // END SNIPPET: startDb


        // START SNIPPET: transaction
        Transaction tx = graphDb.beginTx();
        try
        {
            // Mutating operations go here
            // END SNIPPET: transaction
            // START SNIPPET: addData
            firstNode = graphDb.createNode();
            firstNode.setProperty( "message", "Hello, " );
            secondNode = graphDb.createNode();
            secondNode.setProperty( "message", "World!" );


            relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
            relationship.setProperty( "message", "brave Neo4j " );
            // END SNIPPET: addData


            // START SNIPPET: readData
            System.out.print( firstNode.getProperty( "message" ) );
            System.out.print( relationship.getProperty( "message" ) );
            System.out.print( secondNode.getProperty( "message" ) );
            // END SNIPPET: readData


            greeting = ( (String) firstNode.getProperty( "message" ) )
                       + ( (String) relationship.getProperty( "message" ) )
                       + ( (String) secondNode.getProperty( "message" ) );


            // START SNIPPET: transaction
            tx.success();
        }
        finally
        {
            tx.finish();
        }
        // END SNIPPET: transaction
    }


    private void clearDb()
    {
        try
        {
            FileUtils.deleteRecursively( new File( DB_PATH ) );
        }
        catch ( IOException e )
        {
            throw new RuntimeException( e );
        }
    }


    void removeData()
    {
        Transaction tx = graphDb.beginTx();
        try
        {
            // START SNIPPET: removingData
            // let's remove the data
            firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
            firstNode.delete();
            secondNode.delete();
            // END SNIPPET: removingData


            tx.success();
        }
        finally
        {
            tx.finish();
        }
    }


    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 example before it's completed)
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
            @Override
            public void run()
            {
                graphDb.shutdown();
            }
        } );
    }
    // END SNIPPET: shutdownHook
}


你可能感兴趣的:(Neo4j初步测试)