Scala RDF4J应用程序不会终止

时间:2017-07-21 21:18:51

标签: scala sparql rdf4j

在过去的几个月里,我一直在写相当数量的Scala + RDF4J。到目前为止一切都很好。

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

我做错了什么?我已经针对几个不同的端点运行它,包括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 + " -> " + valueOf
      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")

1 个答案:

答案 0 :(得分: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 + " -> " + valueOf
  println(toPrint)
}

result.close
con.close

// ADD THIS for proper application termination
SparqlRepo.shutDown

除了JavaDocs AKSW posted之外,RDF4J编程教程还能很好地强调需要正确关闭的许多内容。

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

它没有特别提及关闭SPARQLRepository个实例。