我目前正在摆弄Neo4j。对于初学者,我想创建一些节点和关系,并在服务器Web界面中显示它们。
我复制&从此处粘贴代码段:http://neo4j.com/docs/java-reference/current/#tutorials-java-embedded-setup
当我在我的代码中创建数据时,我能够在下一次迭代中读取它(参见// debug方法)。所以它被保存了(对吗?)。但我无法查看Neo4j Servers Web-Viewer中的数据。我将Servers DB重新配置到我的嵌入式数据库,它显示了正确的数据库名称。
那为什么我看不到我的数据呢?提前谢谢。
我的代码如下:
public class TestApp2 {
private static enum RelTypes implements RelationshipType
{
KNOWS
}
public static void main(String[] args) {
File file = new File("/home/foo/workspace/Neo4JConnector/db3.db");
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(file);
registerShutdownHook( graphDb );
try ( Transaction tx = graphDb.beginTx() )
{
checkForNode(graphDb);
Node firstNode = graphDb.createNode();
firstNode.setProperty( "message", "Hello, " );
Node secondNode = graphDb.createNode();
secondNode.setProperty( "message", "World!" );
Relationship relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationship.setProperty( "message", "brave Neo4j " );
System.out.print( firstNode.getProperty( "message" ) );
System.out.print( relationship.getProperty( "message" ) );
System.out.println( secondNode.getProperty( "message" ) );
tx.success();
tx.close();
}
System.out.println("got it! Going to sleep now...");
graphDb.shutdown();
}
// debug
private static void checkForNode(GraphDatabaseService graphDb)
{
Node node = null;
try
{
node = graphDb.getNodeById(0);
}
catch (NotFoundException e)
{
System.out.println("initial run");
return;
}
System.out.println("Node found: "+node.getProperty("message"));
}
private static void registerShutdownHook( final GraphDatabaseService graphDb )
{
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}
}
我的Neo4j-Config指向同一个文件夹:
dbms.active_database=/home/foo/workspace/Neo4JConnector/db2.db