2

在过去的几个月里,我一直在编写大量的 Scala + RDF4J。到现在为止还挺好。

我今天刚写了这个,应用程序不会终止。它打印出请求的三个三元组和单词“done”,但不会返回到 sbt 命令提示符(或者,在 Eclipse 中,停止按钮保持红色并且将鼠标悬停在运行按钮上会显示“已经运行的消息”)

我到底做错了什么? 我已经针对几个不同的端点运行了它,包括 RDF4J 服务器、Virtuoso 和 Blazegraph。

我的来源

import org.eclipse.rdf4j.query.QueryLanguage
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository

object sharedMain {
  def main(args: Array[String]): Unit = {

    val sparqlEndpoint = "https://dbpedia.org/sparql"
    val SparqlRepo = new SPARQLRepository(sparqlEndpoint)
    SparqlRepo.initialize()

    var con = SparqlRepo.getConnection()

    var queryString = "SELECT ?x ?y WHERE { ?x ?p ?y } limit 3 "
    val tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
    var result = tupleQuery.evaluate()

    while (result.hasNext()) { // iterate over the result
      val bindingSet = result.next()
      val valueOfX = bindingSet.getValue("x")
      val valueOfY = bindingSet.getValue("y")
      val toPrint = valueOfX + " -> " + valueOfY
      println(toPrint)
    }

    result.close
    con.close

    println("done")
  }
}

我的 build.sbt

name := "tripleStoreTesting"
version := "0.1"
scalaVersion := "2.12.0"
resolvers += Classpaths.typesafeReleases
libraryDependencies ++= Seq(
    "ch.qos.logback" % "logback-classic" % "1.1.5" % "runtime",
    "org.scalactic" %% "scalactic" % "3.0.1",
    "org.scalatest" %% "scalatest" % "3.0.1" % "test",
    "org.eclipse.rdf4j" % "rdf4j-runtime" % "2.2.2",
    "commons-logging" % "commons-logging" % "1.2"
)
mainClass := Some("sharedMain")
4

1 回答 1

1

正如 AKSW 建议的那样,我没有关闭或关闭我打开、启动、初始化等的所有内容。

// Scala code snippet, 
// starting with my program's last interaction with the triplestore

while (result.hasNext) { // iterate over the result
  val bindingSet = result.next
  val valueOfX = bindingSet.getValue("x")
  val valueOfY = bindingSet.getValue("y")
  val toPrint = valueOfX + " -> " + valueOfY
  println(toPrint)
}

result.close
con.close

// ADD THIS for proper application termination
SparqlRepo.shutDown

除了发布的JavaDocs AKSW之外,RDF4J编程教程在强调需要正确关闭的许多内容方面做得非常好。

  • 连接
  • 结果迭代器
  • 语句迭代器
  • 输入流
  • 自然,文件

它只是没有特别提到关闭SPARQLRepository实例。

于 2017-07-26T13:53:18.900 回答