Category Archives: SPARQL

Discover Linked Data in an endpoint

In this post I descibe a quick and easy way to discover what is available in a SPARQL endpoint.

For some people, SPARQL seems a little strange. It is actually very simple when you remember that the SPARQL query engine evaluates triple patterns. So, when you write a query the SPARQL engine responds with triples that match that pattern.
SPARQL does not only query data, it can also query the datamodel at the same time. A good approach to discover what is in a particular SPARQL endpoint is to query the datamodel first. Then you have an idea of the content of the repository.

To query what main topics there are in a repository you can execute this query. Be nice to the endpoint and LIMIT your query with a reasonable value for the amout of rows returned (I assume that the OWL schema is used. Some repositories also use the RDFS schema, so querying rdfs:Class might -also- be necessary) :

SELECT * 
WHERE {
       ?topic a owl:Class 
      }
LIMIT 10

Next you would perhaps like to know what data is associated with the classes. You extend the previous SPARQL query with a triple like this:

?data a ?topic .

So the total query is:

SELECT * 
WHERE {
      ?topic a owl:Class .
      ?data a ?topic .
      } 
LIMIT 100

Oops..you might get a lot of information now…For example when you are querying DBpedia...So perhaps it is time to use an actual value that you retrieved in your query, which is now for example only this statement:

SELECT * 
     WHERE {
             ?data a <http://dbpedia.org/ontology/Artist> .
           }

To find out what more is described about our data in property relations add:

 ?data ?prop ?val .

Or..using  real values in our DBpedia example, use only this statement in the WHERE clause:

<http://nl.dbpedia.org/resource/Sam_Most> ?prop ?val .

So the total general query is:

SELECT * 
WHERE {
       ?topic a owl:Class .
       ?data a ?topic .  
       ?data ?prop ?val .
      }

..not quite..there might be more! Since relations have a direction, it is possible that there are some incoming relations that we miss. So add the incoming relations as follows. This general query, as we have seen, is as a whole only useful in small repositories.

SELECT * 
WHERE {
      {?topic a owl:Class .
       ?data a ?topic .  
       ?data ?prop ?val .
      }
UNION {
      ?val ?prop ?data . 
      }
      }