Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the 'GRAPH' keyword in SPARQL to fetch remote graphs

I'm looking to use SPARQL for a relatively basic task: Given a FOAF graph, I'd like to parse the elements I find in there, get their tags (if they exist) and then, use those as new graphs from which to find information about those people.

So for instance, you could imagine a simple use case where I want to run a SPARQL query to list all of my favorite foods (as per my FOAF file), and also the favorite foods of all my friends.

Here is what this looks like at the moment. Note that for testing purposes, at the moment all I'm trying to do with the query below is fetch the name of the friend, through the ?name3 variable. Running this query doesn't return any results for ?graph and ?name3, even though I know that the rdfs:seeAlso link to some valid RDF files, of which at least two should have a name attribute. Thanks for any input you might have!

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name1 ?name2 ?graph ?name3
FROM <my-rdf-file>
WHERE { 
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
    OPTIONAL { 
        ?person2 rdfs:seeAlso ?graph .
        GRAPH ?graph {
            ?person3 foaf:name ?name3 .         
        }   
    }       
}
like image 266
Geoffroy Avatar asked Feb 11 '11 13:02

Geoffroy


People also ask

What is graph in SPARQL?

SPARQL has several query forms. The SELECT query form returns variable bindings. The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.

How does SPARQL query work?

SPARQL sees your data as a directed, labeled graph, that is internally expressed as triples consisting of subject, predicate and object. Correspondingly, a SPARQL query consists of a set of triple patterns in which each element (the subject, predicate and object) can be a variable (wildcard).

What types of queries does SPARQL support?

SPARQL allows for a query to consist of triple patterns, conjunctions, disjunctions, and optional patterns. Implementations for multiple programming languages exist. There exist tools that allow one to connect and semi-automatically construct a SPARQL query for a SPARQL endpoint, for example ViziQuer.

Is SPARQL a graph database?

SPARQL is a set of standards for graph databases published by the W3C, but the name is most often used to refer to the query language. As a query language, SPARQL can be used to add, remove and retrieve data from RDF-style graph databases.


2 Answers

GRAPH doesn't implicitly fetch remote data into the store, that would be too much of a security risk. There may be some systems where you can enable this, but it's non-standard.

However, in SPARQL 1.1 Update, there's a keyword LOAD, which does this though, you can write:

LOAD <uri>

Which will fetch the graph into the store, so you could write:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?graph
FROM <my-rdf-file>
WHERE { 
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
    OPTIONAL { 
        ?person2 rdfs:seeAlso ?graph .   
    }       
}

Feed the bindings for ?graph into a set of LOAD statements, and then run your original query.

N.B. in some systems, e.g. 4store you need to enable LOAD, it's not allowed by default, so check the documentation of the store you're using.

like image 144
Steve Harris Avatar answered Oct 01 '22 02:10

Steve Harris


Instead of using LOAD, once you have discovered the graphs that you need to include, as per Steve's example above, wouldn't it be reasonable to alternatively use FROM NAMED , stating all the relevant graphs in the resulting query i.e

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name1 ?name2 ?name3
FROM <my-rdf-file>
FROM NAMED <discovered-graph-URI-1>
FROM NAMED <discovered-graph-URI-n>
WHERE { 
GRAPH <my-rdf-file> {
    ?person1 foaf:knows ?person2 .
    ?person1 foaf:name ?name1 .
    ?person2 foaf:name  ?name2 .
 }
    OPTIONAL { 
        ?person2 rdfs:seeAlso <discovered-graph-URI-1> .
        GRAPH <discovered-graph-URI-1> {
            ?person3 foaf:name ?name3 .         
        }   
        ?person2 rdfs:seeAlso <discovered-graph-URI-n> .
        GRAPH <discovered-graph-URI-n> {
            ?person3 foaf:name ?name3 .         
        }  
    }       
}

That way you get round the security issues and you don't have to maintain the data in your own triplestore.

like image 31
William Greenly Avatar answered Oct 01 '22 01:10

William Greenly