我正在尝试在嵌入式jave Neo4j api上使用scala。我无法打开数据库以便随后阅读。下面的代码应该在每次运行时创建两个节点和一个边,但是在每次运行开始时返回所有节点。所以,0个节点第一次,2个节点第二次,4个第三次等。
import org.neo4j.tooling.GlobalGraphOperations
import org.neo4j.graphdb.factory.GraphDatabaseFactory
import org.neo4j.graphdb.RelationshipType
object tester extends App{
val DB_PATH = "data/neo4j"
object KNOWS extends RelationshipType {
override def name(): String = "KNOWS"
}
val graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH) //seems to reset the whole directory
println(graphDb)
try {
println("Begin")
val tx = graphDb.beginTx() // Database operations go here
println(GlobalGraphOperations.at(graphDb).getAllNodes.iterator)
val nodes = GlobalGraphOperations.at(graphDb).getAllNodes.iterator
while (nodes.hasNext()) {
println(nodes.next())
}
nodes.close()
val relT = GlobalGraphOperations.at(graphDb).getAllRelationships.iterator
while (relT.hasNext()) {
println(relT.next())
}
println("Success - Begin")
tx.success()
}
try {
val tx = graphDb.beginTx() // Database operations go here
val firstNode = graphDb.createNode
val secondNode = graphDb.createNode
val relationship = firstNode.createRelationshipTo(secondNode, KNOWS)
println(firstNode)
println(secondNode)
println(relationship)
println(relationship.getType.name)
tx.success()
println("Success")
}
println("End")
try {
val tx = graphDb.beginTx() // Database operations go here
println(GlobalGraphOperations.at(graphDb).getAllNodes.iterator)
val nodes = GlobalGraphOperations.at(graphDb).getAllNodes.iterator
while (nodes.hasNext()) {
println(nodes.next())
}
nodes.close()
val relT = GlobalGraphOperations.at(graphDb).getAllRelationships.iterator
while (relT.hasNext()) {
println(relT.next())
}
println("Success - End")
tx.success()
}
graphDb.shutdown()
}
然而,每次它似乎只是给出一个空数据库,然后是2个新节点。这里发生了什么?
EmbeddedGraphDatabase [data/neo4j]
Begin
org.neo4j.tooling.GlobalGraphOperations$1$1@74c49a90
Success - Begin
Node[2]
Node[3]
Relationship[1]
KNOWS
Success
End
org.neo4j.tooling.GlobalGraphOperations$1$1@2ec0df08
Node[2]
Node[3]
Relationship[1]
Success - End
Process finished with exit code 0
答案 0 :(得分:2)
这种情况正在发生,因为你不是closing the transaction。您可以致电tx.close()
来完成此操作。另外我认为在tx
内实例化try
并不完全是应该如何。这是您的程序的工作版本:
import org.neo4j.tooling.GlobalGraphOperations
import org.neo4j.graphdb.factory.GraphDatabaseFactory
import org.neo4j.graphdb.RelationshipType
object tester extends App{
val DB_PATH = "data/neo4j"
object KNOWS extends RelationshipType {
override def name(): String = "KNOWS"
}
val graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH)
println(graphDb)
val tx1 = graphDb.beginTx() // Database operations go here
try {
println("Will list all nodes")
println("1 - Begin")
println("GlobalGraphOperations.at(graphDb).getAllNodes.iterator")
val nodes = GlobalGraphOperations.at(graphDb).getAllNodes.iterator
while (nodes.hasNext()) {
println(nodes.next())
}
nodes.close()
val relT = GlobalGraphOperations.at(graphDb).getAllRelationships.iterator
while (relT.hasNext()) {
println(relT.next())
}
println("1 - Success - Begin")
tx1.success()
}
finally {
tx1.close()
}
val tx2 = graphDb.beginTx() // Database operations go here
try {
val firstNode = graphDb.createNode
val secondNode = graphDb.createNode
val relationship = firstNode.createRelationshipTo(secondNode, KNOWS)
println(firstNode)
println(secondNode)
println(relationship)
println(relationship.getType.name)
tx2.success()
println("2 - Success")
}
finally {
tx2.close()
}
println("2 - End")
val tx3 = graphDb.beginTx() // Database operations go here
try {
println(GlobalGraphOperations.at(graphDb).getAllNodes.iterator)
val nodes = GlobalGraphOperations.at(graphDb).getAllNodes.iterator
while (nodes.hasNext()) {
println(nodes.next())
}
nodes.close()
val relT = GlobalGraphOperations.at(graphDb).getAllRelationships.iterator
while (relT.hasNext()) {
println(relT.next())
}
println("3 - Success - End")
tx3.success()
}
finally {
tx3.close()
}
graphDb.shutdown()
}
我试图让你的程序更接近“scala-style”。此外,我试图删除样板和重复的代码。要做到这一点我:
JavaConverters
处理Java集合和Iterables,就像我们在Scala中处理它们一样。withTransaction
以获取automatic resource management。结果如下:
import org.neo4j.tooling.GlobalGraphOperations
import org.neo4j.graphdb.factory.GraphDatabaseFactory
import org.neo4j.graphdb.RelationshipType
import org.neo4j.graphdb.Transaction
import scala.collection.JavaConverters._
object tester extends App{
val DB_PATH = "data/neo4j"
object KNOWS extends RelationshipType {
override def name(): String = "KNOWS"
}
def withTransaction (doWithTransaction: Transaction => Unit) {
val tempTx = graphDb.beginTx()
try {
doWithTransaction(tempTx)
}
finally {
tempTx.close()
}
}
val graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH)
println(graphDb)
withTransaction { tx =>
println("1 - Begin")
val nodes = GlobalGraphOperations.at(graphDb).getAllNodes
for (node <- nodes.asScala)
println(node)
val relTs = GlobalGraphOperations.at(graphDb).getAllRelationships
for (irelT <- relTs.asScala)
println(irelT)
println("1 - Success - Begin")
tx.success()
}
withTransaction { tx =>
val firstNode = graphDb.createNode
val secondNode = graphDb.createNode
val relationship = firstNode.createRelationshipTo(secondNode, KNOWS)
println(firstNode)
println(secondNode)
println(relationship)
println(relationship.getType.name)
tx.success()
println("2 - Success")
}
println("2 - End")
withTransaction { tx =>
println(GlobalGraphOperations.at(graphDb).getAllNodes.iterator)
val nodes = GlobalGraphOperations.at(graphDb).getAllNodes
for (node <- nodes.asScala)
println(node)
val relTs = GlobalGraphOperations.at(graphDb).getAllRelationships
for (irelT <- relTs.asScala)
println(irelT)
println("3 - Success - End")
tx.success()
}
graphDb.shutdown()
}
答案 1 :(得分:1)
问题是您正在指定相对路径。可能是每次你运行一个干净并建立你清空你的目标目录(或dist,或任何你的ide,开发框架用作分发目录)所以数据库是空的,因为它是从头开始每次创建。尝试使用绝对路径。