Sparql Result to Java

188 views
Skip to first unread message

rdf 4j

unread,
May 10, 2021, 5:58:02 AM5/10/21
to RDF4J Users
Hi there, is there a library or a sample, that interpretes sparql results and converts them back to java ?

Thanks
Fredy

Jeen Broekstra

unread,
May 10, 2021, 7:41:41 AM5/10/21
to RDF4J Users
Yes, that library is (probably) RDF4J.

Can you explain in a bit more detail what you're trying to do? There are lots of different ways to "interpret sparql results", so we need a bit of background before we can give you advice on the best approach.

Jeen
--
You received this message because you are subscribed to the Google Groups "RDF4J Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdf4j-users...@googlegroups.com.

rdf 4j

unread,
May 11, 2021, 8:25:11 AM5/11/21
to rdf4j...@googlegroups.com
Hi,
i added a sparql endpoint to my spring-boot-service, which accepts SELECTs. The result is json, see attached file.
No i have to interprete the result in a library, that is using the sparql endpoint.

And while i'm writing this lines, i think that should be a HTTPRepository ;-) ;-)

I'l give that a try.

Thanks.

result.json

rdf 4j

unread,
May 11, 2021, 8:31:31 AM5/11/21
to rdf4j...@googlegroups.com
It has to be a SPARQLRepository. but i need to pass a jwt. At first glance, it looks like SPARQLRepository "only" supports username/password.
Fredy

bart.h...@bosa.fgov.be

unread,
May 11, 2021, 12:55:02 PM5/11/21
to RDF4J Users
Hi,

not entirely sure if I understood your question correctly, 
but the QueryResultIO offers a few convenient methods to parse the results (parseTuple) into a TupleQueryResult,
and then your application can simply iterate over it


Best regards

Bart

rdf 4j

unread,
May 12, 2021, 4:01:56 AM5/12/21
to rdf4j...@googlegroups.com
Ok, as already noted by Jeen it is a good idea to provide the code in a usable way ;-)
So i took the trouble to extract the essentials from our project. I had to make minimal modifications, but that shouldn't be a problem.

You can find this here

Please read my short problem description.

Jeen Broekstra

unread,
May 12, 2021, 4:49:52 AM5/12/21
to RDF4J Users
So, your problem description says this:

No it should be possible to use a SPARQLRepository on the client side to get data from my SPARQL endpoint.

This is done in the test spring-boot-sparql-controller/src/test/java/de/naturzukunft/rdf4j/sparql/spring/controller/TestSparqlRepository.java


But there are no bindiungs in the response.


To be honest, I still have no idea what problem you're trying to solve, exactly. What do you mean with "But there are no bindings in the response"?

I think you were originally asking for a way to parse a JSON SPARQL response in Java, to which Bart gave you a reply, but from this problem description I'm now guessing that you are asking why your client code doing a select query via the SPARQLRepository is not giving a result. Is this a followup question, or was what Bart gave you not what you wanted?

What is it that you need? What is your current code attempting to do, what is the expected result, and what is the actual result you're getting? 

Jeen

rdf 4j

unread,
May 12, 2021, 5:39:08 AM5/12/21
to rdf4j...@googlegroups.com
"I think you were originally asking for a way to parse a JSON SPARQL response in Java"
-> right!

  • 11. Mai, 14:24 I wanted to describe the problem in more detail and it occurred to me that rdf4j supports HTTP and that I can theoretically simply use a HTTP repository.
  • 11. Mai, 14:31 I found that "SPARQLRepository" must be the solution, but it does not support OAuth2 at first sight. I think I already have a solution for that.
  • 11. Mai, 18:55 Bart suggested QueryResultIO
  • 12. Mai ~8:00 I looked at QueryResultIO and I have it in the back of my mind. But I don't think this is the right way to go with my problem.

What you won't know is that in my head I mix this thread with the thread "Differentce / meaning of artifacts rdf4j-http-server[-spring] (query endpoint)".
There it was about the SPARQL endpoint that Jerven Bolleman enables with his QueryResponder.
I provided a zip file in the thread "Differentce / meaning of artifacts rdf4j-http-server[-spring] (query endpoint)"
And jeen has pointed out that a zip file is a bad idea, so i decided to provide the whole code for both threads now as a gitlab repo.

I'll try to describe the problem again.
I am implementing an application that uses rdf4j repositories internally for users (actors). And I need a way to query these repositories. For this I would like to have a SPARQL endpoint. I don't want to use rdf4jserver/rdf4jworkbench.

Now I have a solution for the SPARQL endpoint, see my spring boot app on gitlab. It also looks like a correct result is returned.
A few minutes ago I debugged the client and I also think that the response is parsed correctly. In my Test, however, no bindingSets arrive yet and I don't know why.

Running the test:
  1. git clone https://gitlab.com/naturzukunft.de/public/rdf/spring-boot-sparql.git
  2. cd spring-boot-sparql
  3. mvn clean install -DskipTests
  4. cd spring-boot-sparql-controller
  5. mvn -Dtest=TestSparqlRepository test

@Jeen, we had also talked about the Spring-boot alternative to the rdf4j-server a long time ago. That also has something to do with this topic ;-)

Sorry for the confusion, i hope it is clearer now.
Fredy


Jeen Broekstra

unread,
May 14, 2021, 10:28:20 AM5/14/21
to RDF4J Users
I had a glance at your code and I'm immediately spotting anything obviously wrong. Using SPARQLRepository to communicate with a remote SPARQL endpoint is the right approach I think. 

If you are getting back an empty result, the most likely causes are that the target repository is empty or the query you're sending is not matching anything. I suggest that you focus on those two possibilities first when debugging things. 

Cheers,

Jeen

rdf 4j

unread,
May 27, 2021, 6:27:18 AM5/27/21
to RDF4J Users
Hi Jeen,
sorry for the late reply.

You wrote: "I had a glance at your code and I'm immediately spotting anything obviously wrong."
Can you please descibe what you mean, what is wrong?

If i start the spring-boot app, i'm able to call the endpoint with:

curl -H 'accept: application/json' "http://localhost:8080/actors/0815/sparql?query=SELECT+*+WHERE+%7B+%3Fs+%3Fp+%3Fo+%7D"

and get a json result (see below), thats looks fine for me.

Maybe the way i interprete the result is wrong ? (https://gitlab.com/naturzukunft.de/public/rdf/spring-boot-sparql/-/blob/develop/spring-boot-sparql-controller/src/test/java/de/naturzukunft/rdf4j/sparql/spring/controller/TestSparqlRepository.java#L28)



Response:
{
  "head" : {
    "vars" : [
      "s",
      "p",
      "o"
    ]
  },
  "results" : {
    "bindings" : [
      {
        "p" : {
          "type" : "uri",
          "value" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
        },
        "s" : {
          "type" : "uri",
          "value" : "kvm:V8_cd1ac0d81679479fb85acdf59ce69a01"
        },
        "o" : {
          "type" : "uri",
          "value" : "http://www.w3.org/ns/activitystreams#Object"
        }
      },
      {
        "p" : {
          "type" : "uri",
          "value" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
        },
...
...

Jeen Broekstra

unread,
May 27, 2021, 6:40:27 AM5/27/21
to RDF4J Users


On Thu, 27 May 2021, at 20:27, rdf 4j wrote:
Hi Jeen,
sorry for the late reply.

You wrote: "I had a glance at your code and I'm immediately spotting anything obviously wrong."
Can you please descibe what you mean, what is wrong?

Sorry, that was a typo, I meant to say I'm NOT immediately spotting anything wrong.

rdf 4j

unread,
May 27, 2021, 9:30:16 AM5/27/21
to RDF4J Users
ok, now another more simple sample

https://gist.github.com/naturzukunft/8bad67a2b44d66ff13f80c9e44e18635

It's using https://dbpedia.org/sparql with their standard query, but this SPARQLRepository thing ist not working.

i just upgraded to 3.7.0, should i report a bug ?

Fredy

Jeen Broekstra

unread,
May 27, 2021, 7:30:28 PM5/27/21
to RDF4J Users
On Thu, 27 May 2021, at 23:30, rdf 4j wrote:
ok, now another more simple sample


It's using https://dbpedia.org/sparql with their standard query, but this SPARQLRepository thing ist not working.

i just upgraded to 3.7.0, should i report a bug ?

No, you should fix your code :)

The problem is that you are trying to iterate over the result iterator outside the try-with-resource block, in other words: after you've already closed the connection to the endpoint. That won't work as in RDF4J, query results are evaluated lazily, and need the connection open as long as you're working with the result iterator.

I've added a comment on your gist with a corrected code example, and a couple of other suggestions.

Cheers,

Jeen
 

rdf 4j

unread,
May 28, 2021, 4:08:20 AM5/28/21
to RDF4J Users
ok, this is embarrassing. too many parallel tasks and topics. Thank you for the detailed tips!
i updated the spring-boot sample: https://gitlab.com/naturzukunft.de/public/rdf/spring-boot-sparql.git
i should leave another comment here: https://github.com/eclipse/rdf4j/pull/2905 would be nice if the separation of servlet and rdf4j is better kept.

I was happy to read your comment "rdf4j-server implementation is getting old and hard to maintain".
I was also very confused about the implementation.

Fredy (linkedopenactors.org)
Reply all
Reply to author
Forward
0 new messages