繁体   English   中英

打开现有的嵌入式neo4j数据库

[英]Opening existing embedded neo4j database

我试图在嵌入式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()
}

但是,每次似乎只是给一个空数据库,然后是两个新节点。 这里发生了什么?

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

发生这种情况是因为您没有关闭事务 您可以通过调用tx.close() 我也认为在try中实例化tx并不完全应该如此。 这是程序的工作版本:

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风格”。 另外,我尝试删除样板并重复代码。 为此,我:

  • 使用JavaConverters来处理Java集合和Iterables,就像我们在Scala中处理它们一样
  • 创建了一个withTransaction的方法来为我们在Scala中的交易获取自动资源管理

结果如下:

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()
}

问题是您要指定相对路径。 可能是因为每次运行清理并构建时都清空目标目录(或dist,或任何其他想法,开发框架用作分发目录),所以数据库为空,因为它是从头开始创建的。 尝试使用绝对路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM