无法在使用嵌入式Java程序创建的neo4j Web界面中查看节点

时间:2014-02-08 07:09:29

标签: java neo4j cypher

我能够成功运行Embeddedjava程序。但是当我通过localhost打开neo4j接口时,我的节点没有得到反映。供您参考

我对neo4j-server.properties

进行了以下更改
org.neo4j.server.database.location=C:/neo4j-community-1.9.6/data/graph.db/
org.neo4j.server.webadmin.data.uri=C:/neo4j-community-1.9.6/data/graph.db/

这与我在代码中使用的DB_path相同。

每次运行程序时,仪表板上的计数增加2(我在程序中创建了2个节点)。但是当我运行查询时

START root=node(*)
 return count(root)

它给出了答案为0。

另外,我注意到在运行java程序时没有生成数据/密钥库文件。只有当我通过localhost:7474启动接口时才会生成。这有什么办法吗?

Java代码

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;
import  org.neo4j.kernel.StoreLocker;


public class EmbeddedNeo4j
{
private static final String DB_PATH = "C://neo4j-community-1.9.6//data//graph.db";

public 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=null;
    try
    {
         tx= graphDb.beginTx();
         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" ) );
                   tx.success();
    }
    catch(Exception e)
    {
    tx.failure();
    }


    finally
    {
        // Database operations go here
        // END SNIPPET: transaction
        // START SNIPPET: addData


        // START SNIPPET: transaction

        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=null;
    try 
    {

     tx = graphDb.beginTx() ;
     firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
        firstNode.delete();
        secondNode.delete();
         tx.success();
}
catch(Exception e)
{
tx.failure();
}

finally

    {
        // START SNIPPET: removingData
        // let's remove the data

        // END SNIPPET: removingData


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

2 个答案:

答案 0 :(得分:0)

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

您正在创建数据库,然后调用removeData。

答案 1 :(得分:0)

如果您的问题是关于webadmin显示当计数所有节点的查询显示0时增加2个节点,那么您应该忽略webadmin节点计数。它不是真正的节点数,而是使用中最高节点ID的排序。查询

START root=node(*)
 return count(root)

将正确显示节点数。这是0,这是预期的,因为你的java程序创建节点然后删除它们。