tags:

views:

147

answers:

3

whenever i start using sql i tend to throw a couple of exploratory statements at the database in order to understand what is avaliable, and what form the data takes.

eg.

show tables

describe table

select * from table

could anyone help me understand the way to complete a similar exploration of an rdf datastore using a SPARQL endpoint?

Thanks :)

+6  A: 

Well, the obvious one is to look at classes and properties.

So, you should probably start by looking for the classes being used:

SELECT DISTINCT ?class
WHERE {
  ?s a ?class .
}
LIMIT 25
OFFSET 0

(LIMIT and OFFSET are there for paging. It is worth getting used to these especially if you are sending your query over the Internet. I'll omit them in the other examples.)

a is a special SPARQL (and Notation3/Turtle) syntax to represent the rdf:type predicate - this links individual instances to owl:Class/rdfs:Class types (roughly equivalent to tables in SQL RDBMSes).

Secondly, you want to look at the properties. You can do this either by using the classes you've searched for or just looking for properties. Let's just get all the properties out of the store:

SELECT DISTINCT ?property
WHERE {
  ?s ?property ?o .
}

This will get all the properties, which you probably aren't interested in. This is equivalent to a list of all the row columns in SQL, but without any grouping by the table.

More useful is to see what properties are being used by instances that declare a particular class:

SELECT DISTINCT ?property
WHERE {
  ?s a <http://xmlns.com/foaf/0.1/Person&gt;;
     ?property ?o .
}

This will get you back the properties used on any instances that satisfy the first triple - namely, that have the rdf:type of http://xmlns.com/foaf/0.1/Person.

Remember, because a rdf:Resource can have multiple rdf:type properties - classes if you will - because RDF's data model is additive, you don't have a diamond problem. The type is just another property - it's just a useful social agreement to say that some things are persons or dogs or football teams. It doesn't mean that the data store is going to contain those properties. You need to familiarise yourself with the data model and the use of SPARQL's UNION and OPTIONAL syntax. The rough mapping of rdf:type to SQL tables is just that - rough.

You might want to know what kind of entity the property is pointing to. Firstly, you probably want to know about datatype properties - equivalent to literals or primitives. You know, strings, integers, etc. RDF defines these literals as all basically inheriting from string. We can filter out just those properties that are literals using the SPARQL filter method isLiteral:

SELECT DISTINCT ?property
WHERE {
  ?s a <http://xmlns.com/foaf/0.1/Person&gt;;
     ?property ?o .
  FILTER isLiteral(?o)
}

We are here only going to get properties that have as their object a literal - a string, date-time, boolean, or one of the other XSD datatypes.

But what about the non-literal objects. Consider this Java class definition:

public class Person {
    int age;
    Person marriedTo;
}

Using the above query, we would get back the URI that would represent age if the age property is bound. But marriedTo isn't a literal - it's a reference to another object - in RDF/OWL terminology, that's an object property. But we don't know what sort of objects are being referred to by those properties (predicates). This query will get you back properties with the accompanying types.

SELECT DISTINCT ?property, ?class
WHERE {
  ?s a <http://xmlns.com/foaf/0.1/Person&gt;;
     ?property ?o .
  ? a ?class .
  FILTER(!isLiteral(?o))
}

That should be enough to orient yourself in a particular dataset. Of course, I'd also recommend that you just pull out some individual resources and inspect them. You can do that using the DESCRIBE query:

DESCRIBE <http://example.org/resource&gt;

There are some SPARQL tools - SNORQL, for instance - that let you do this in a browser. The SNORQL instance I've linked to has a sample query for exploring the possible named graphs, which I haven't covered here.

Tom Morris
flat out awesome answer - thanks very much Tom!
significance
A: 

I often refer to this list of queries from the voiD project. They are mainly of a statistical nature, but not only. It shouldn't be hard to remove the COUNTs from some statements to get the actual values.

Thomas Kappler
A: 

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

significance