gpt4 book ai didi

java - 在 Neo4j Web 界面上看不到使用 EmbeddedJava 程序创建的节点

转载 作者:太空宇宙 更新时间:2023-11-04 15:08:37 25 4
gpt4 key购买 nike

我能够成功运行Embeddedjava程序。但是当我通过localhost打开neo4j界面时,我的节点没有得到反射(reflect)。供您引用

我对 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 程序时不会生成数据/ keystore 文件。只有当我通过 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
}

最佳答案

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

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

关于java - 在 Neo4j Web 界面上看不到使用 EmbeddedJava 程序创建的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com