2

I have simple RDF data set with following values: user, url, time and ip. I can list all the data using:

SELECT DISTINCT * WHERE { ?s ?p ?o }

But now I need to list all the data with specific user. I would very much appreciate if the data from the query would be in the same format as the data from above query but it is not necessary.
I tried this and some modifications to this but none of it seems to work:

SELECT DISTINCT * WHERE { ?s ?p ?o. ?u foaf:user username. }

I am just starting to work with Jena and SPARQL and any help will be much appreciated. Thanks in advance.

EDIT: @AKSW foaf:user works because foaf is my own prefix I defined. I struggled with getting the queries working and had no clue what I was doing. This is something I hadn't clean up yet.
I ended up using query SELECT DISTINCT * WHERE {?u foaf:name USER_NAME_LITERAL . ?u ?p ?o . } which was the one you suggested, so thank you. If you post it as answer I will mark it as accepted.

lsrom
  • 608
  • 8
  • 22
  • 1
    Resources in RDF are identified by URIs, and therefore also your users are. `SELECT DISTINCT * WHERE { ?p ?o }`. – UninformedUser Sep 28 '16 at 01:09
  • 1
    SPARQL queries re-use the variable names such that they triple patterns are connected, and in general build a graph. Your second query consists of two unconnected triple patterns. – UninformedUser Sep 28 '16 at 01:09
  • 1
    Don't know what `foaf:user` should be, but in FOAF there is no such property. `foaf:name` is a common property: http://xmlns.com/foaf/spec/ – UninformedUser Sep 28 '16 at 01:13
  • 1
    If you want to identify the user by the username: `SELECT DISTINCT * WHERE {?u foaf:name USER_NAME_LITERAL . ?u ?p ?o . }` Might not be appropriate, if you allow for multiple users with the same name. I.e. more that one user with all its information is returned. – UninformedUser Sep 28 '16 at 01:15

1 Answers1

1

Assuming your data set consists of records of class :Record with fields :user, :url, :time and :ip:

SELECT * { ?r   ?p      ?o;
                 a      :Record;
                :user   ?u.
           ?u   :name "your user name".
          }

Or, assuming that you know that the URI of the user is :user123:

SELECT * { ?r   ?p      ?o;
                 a      :Record;
                :user   :user123.
          }

Notes: This is a summarization of the comments of AKSW. I use the default prefix : instead of foaf:, as this could lead to others assuming it was part of the already existing foaf "friend of a friend" vocabulary. The WHERE keyword can always be left out and the DISTINCT keyword should not be necessary in this case, barring unusual circumstances such as the same triple being contained in multiple graphs.

Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • Thank you for your answer, it was very helpful as it provided me with some more insight. The whole `foaf` thing in my example is just a mess I did when I started learning about the SPARQL. In current version I have it's been fixed. As I used AKSW solution I will wait if he decides to post his comments as answer but if he doesn't I will accept your answer as it was helpful as well, even if the problem I had was already solved. Than you once again. – lsrom Sep 28 '16 at 18:53