我正在尝试编写一个程序来获取数据并将其输入到Neo4j数据库中。该程序似乎运行良好 - 我说它运行没有问题和
System.out.println(node.getProperty("Name"));
行返回我创建的每个节点的值。然而,当我进入Web控制台时,我没有在数据库中看到任何这些节点。我很肯定我正在把它写到正确的目录中。执行ls -l
表示每次运行程序时都会修改数据库:
new-host-4:new_graph.db USER$ ls -l
total 504
-rw-r--r-- 1 USER wheel 11 Mar 25 00:55 active_tx_log
drwxr-xr-x 8 USER wheel 272 Mar 25 01:21 index
-rw-r--r-- 1 USER wheel 111766 Mar 25 01:21 messages.log
-rw-r--r-- 1 USER wheel 78 Mar 25 01:21 neostore
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.id
-rw-r--r-- 1 USER wheel 22 Mar 25 01:21 neostore.labeltokenstore.db
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.labeltokenstore.db.id
-rw-r--r-- 1 USER wheel 64 Mar 25 01:21 neostore.labeltokenstore.db.names
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.labeltokenstore.db.names.id
-rw-r--r-- 1 USER wheel 100 Mar 25 01:21 neostore.nodestore.db
-rw-r--r-- 1 USER wheel 41 Mar 25 01:21 neostore.nodestore.db.id
-rw-r--r-- 1 USER wheel 93 Mar 25 01:21 neostore.nodestore.db.labels
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.nodestore.db.labels.id
-rw-r--r-- 1 USER wheel 635 Mar 25 01:21 neostore.propertystore.db
-rw-r--r-- 1 USER wheel 153 Mar 25 01:21 neostore.propertystore.db.arrays
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.propertystore.db.arrays.id
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.propertystore.db.id
-rw-r--r-- 1 USER wheel 43 Mar 25 01:21 neostore.propertystore.db.index
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.propertystore.db.index.id
-rw-r--r-- 1 USER wheel 140 Mar 25 01:21 neostore.propertystore.db.index.keys
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.propertystore.db.index.keys.id
-rw-r--r-- 1 USER wheel 154 Mar 25 01:21 neostore.propertystore.db.strings
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.propertystore.db.strings.id
-rw-r--r-- 1 USER wheel 57 Mar 25 01:21 neostore.relationshipstore.db
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.relationshipstore.db.id
-rw-r--r-- 1 USER wheel 33 Mar 25 01:21 neostore.relationshiptypestore.db
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.relationshiptypestore.db.id
-rw-r--r-- 1 USER wheel 102 Mar 25 01:21 neostore.relationshiptypestore.db.names
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.relationshiptypestore.db.names.id
-rw-r--r-- 1 USER wheel 82 Mar 25 01:21 neostore.schemastore.db
-rw-r--r-- 1 USER wheel 9 Mar 25 01:21 neostore.schemastore.db.id
-rw-r--r-- 1 USER wheel 4 Mar 25 01:21 nioneo_logical.log.active
-rw-r--r-- 1 USER wheel 856 Mar 25 00:55 nioneo_logical.log.v0
-rw-r--r-- 1 USER wheel 464 Mar 25 00:57 nioneo_logical.log.v1
-rw-r--r-- 1 USER wheel 316 Mar 25 01:05 nioneo_logical.log.v2
-rw-r--r-- 1 USER wheel 316 Mar 25 01:21 nioneo_logical.log.v3
drwxr-xr-x 3 USER wheel 102 Mar 25 00:55 schema
-rw-r--r-- 1 USER wheel 0 Mar 25 00:55 store_lock
-rw-r--r-- 1 USER wheel 500 Mar 25 01:21 tm_tx_log.1
new-host-4:new_graph.db USER$ pwd
/usr/local/Cellar/neo4j/2.0.1/libexec/data/new_graph.db
然而,当我进入
START a = node(*)
RETURN a;
我没有节点。如果我在此数据库上运行EmbeddedNeo4j.java示例,那么这些节点将显示在数据库中,但不会显示在这些节点中。控制台配置设置显示相同的目录
我错过了什么或是否有Neo4J的错误?
供参考,我正在使用的课程如下。主调用测试器,它打开一个流。 Stream获取数据库的句柄,并在将规则应用于输入(未显示)后,使用NeoProcessor创建条目。这些属性会毫无问题地打印到控制台,这让我相信它们已被写入数据库,但我从未在Web控制台中看到它们。
我的主要人物:
public class NeoTest {
public static void main( final String[] args )
{
//Our tester to test the database
Tester myTest = new Tester("Stream");
try{
myTest.fileTester("/Users/me/data/test");
myTest.end();
}
catch (IOException ex)
{
System.out.println("Cannot open file fool");
}
}
}
我的测试员:
public class Tester {
private Streamer myStream;
private String inputType;
public Tester(String inputType)
{
this.inputType = inputType;
if(inputType.toLowerCase().contains("Stream".toLowerCase()))
{
System.out.println("Using Stream");
// instantiate streamer
myStream = new Streamer();
//get DB going
myStream.openStream("calllog");
}
}
public void fileTester(String fileName) throws IOException
{
System.out.println("hi");
BufferedReader in = new BufferedReader(new FileReader(fileName));
while(in.ready())
{
String s = in.readLine();
myStream.streamInput(s);
}
in.close();
}
public void end()
{
if(inputType.toLowerCase().contains("Stream".toLowerCase()))
myStream.closeStream();
}
}
我的处理程序是:
public class NeoHandle {
private static GraphDatabaseService graphDb;
private String DB_PATH = "/usr/local/Cellar/neo4j/2.0.1/libexec/data/new_graph.db";
NeoHandle()
{
this.graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
}
public GraphDatabaseService getNeoHandle()
{
return graphDb;
}
public void createDb()
{
// START SNIPPET: startDb
registerShutdownHook( graphDb );
// END SNIPPET: startDb
}
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();
}
} );
}
public void shutDown()
{
System.out.println();
System.out.println( "Shutting down database ..." );
// START SNIPPET: shutdownServer
graphDb.shutdown();
// END SNIPPET: shutdownServer
}
}
我的流光是:
public class Streamer {
private NeoHandle myHandle;
private String contextType;
Streamer()
{
}
public void openStream(String contextType)
{
myHandle = new NeoHandle();
myHandle.createDb();
}
public void streamInput(String dataLine)
{
LinkedList context = new LinkedList();
/*
* get database instance
* write to database
* check for errors
* report errors & success
*/
System.out.println(dataLine);
//apply rules to data (make ContextRules do this, send type and string of data)
ContextRules contextRules = new ContextRules();
context = contextRules.processContextRules("Calls", dataLine);
//write data (using linked list from contextRules)
NeoProcessor processor = new NeoProcessor(myHandle);
processor.processContextData(context);
}
public void closeStream()
{
/*
* close database instance
*/
myHandle.shutDown();
}
}
我的处理器是:
public class NeoProcessor {
private NeoHandle handle;
public NeoProcessor(NeoHandle handle)
{
this.handle = handle;
}
public void processContextData(LinkedList context)
{
for(Object c : context)
{
if(c instanceof Entity)
{
Node node = addNode((Entity)c);
((Entity)c).setNode(node);
}
}
}
public Node addNode(Entity myNode)
{
//TODO - wrap in transaction
GraphDatabaseService graphDb = this.handle.getNeoHandle();
Node node;
try ( Transaction tx = graphDb.beginTx() )
{
node = graphDb.createNode();
if(myNode.hasName())
{
System.out.println("setting name");
node.setProperty("Name", myNode.getName());
System.out.println(node.getProperty("Name"));
}
}//try transaction
return node;
}
}
答案 0 :(得分:0)
我遗漏了NeoProcessor中的tx.success()
调用。当我在事务中包装它时,我必须提交事务。