Becher
Becher

Reputation: 31

Scala Sparql query with Apache Jena

I am trying to build a sparql query in scala. I have issues using the apache arq library which is meant for java. The dependencies seem to be all fine but Intellij cannot resolve the ParameterizedSparqlString constructor and resulting the QueryEngineHTTP constructor. Im running jdk 1.8. Any ideas? Appreciated!!

val sparqlentities = namedEntitiesByDocument
  .mapPartitions(iter => {
    val sparqlEndpoint = "localhost:1643";
    iter.map( t => {
      t._1, t._2.map(namedEntity => {
        val sparqlQuery = "" + "SELECT  ?s { \n ?s rdfs:label" + namedEntity.surfaceForm + " . \n }"
        val query = QueryFactory.create(sparqlQuery, Syntax.syntaxARQ)
        // val querySolutionMap = new QuerySolutionMap()
        val parameterizedSparqlString = new ParameterizedSparqlString(query.toString(), new QuerySolutionMap())
        val httpQuery = new QueryEngineHTTP(sparqlEndpoint,parameterizedSparqlString.asQuery())
        val results = httpQuery.execSelect()
        while (results.hasNext()) {
          val solution = results.next()
          val fin_result = solution.get("s").asLiteral().getLexicalForm()
          (namedEntity.surfaceForm, fin_result)
        }
      })
    })
  })

Upvotes: 0

Views: 1155

Answers (2)

Ion Freeman
Ion Freeman

Reputation: 540

@Becher, per @AKSW's question, we need to know how you're trying to resolve dependencies. IntelliJ gives you a few options -- I use SBT, which I think is pretty common, with Ivy (through the IvyIDEA plug-in.) But, you can use Maven directly or just add jars to a library.

This is a pretty helpful page in any case https://mvnrepository.com/artifact/org.apache.jena/jena-arq/3.1.1

Upvotes: 1

AndyS
AndyS

Reputation: 16700

(comments do not work for complex text, hence this answer)

You do not need a ParameterizedSparqlString if you are doing string building yourself. Creating a query string, parsing it, then calling .toString is a bit convoluted. You can use QueryFactory then QueryExecutionFactory.

Note that at

val sparqlQuery = "" + "SELECT ?s { \n ?s rdfs:label" + namedEntity.surfaceForm + " . \n }" The namedEntity.surfaceForm must be in SPARQL syntax, e.g a string needs "" around it, otherwise you will get a parse error in SPARQL.

Upvotes: 3

Related Questions