In the last post of this series I told you about the RDF data model and I presented the four formats that we can use to represent RDF, I explained about the knowledge graph, I told about how easy is add and remove elements form the graph without compromise the graph structure, at the end of the post, I left a couple of questions about how to make question to the graph, how to extract the knowledge from the graph knowledge. The short answer for those question is SPARQL, and how SPARQL can help us with those question is what I will show you on this post.

The first crazy thing about the SPARQL is that SPARQL is one of that recursive acronyms. SPARQL means “SPARQL Protocol and RDF Query Language”, I know is it weird, but if you have any complain about it, please do ask to W3C, because SPARQL is a W3C Standard. SPARQL is a query language, and a query language is what allow us to formulate the question to the knowledge graph, SPARQL is also protocol, that allows us push the queries to the data.

Let me stop the talk and jump into the action, below you can see a fake SPARQL query where a I just defined the basics structure of it. In the first line you see prefix, I will not talk that now, but I will explain it later in another post about ontology. In the second line you have the command SELECT and  if you are familiar with SQL, you must be able to figure out what the select does, the select selects the variables that I want as output of my query. The statement start in the line 3 “WHERE {” and it finish in the line 5 “}”, it is where I will put a set of restrictions that will submit against the data to give me back the answer that I want.

PREFIX (Namespace prefixes.)
SELECT <variables>
    WHERE{   
     <GRAPH PATTERN>
    }


The first real SPARQL that I will show you is the query below, in this query is easy to notice that the all variables correspond to a restriction in the graph pattern. It is a very weird query, it is the same of say, tell me everything about everything, it is not a good query to do and you know…still be friend of the Poolparty administrator!

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


Poolparty have a nice interface that allow you to run your SPARQL query without problems.

sparql-1

Now adding a restriction to our query. I am using the URI that represent the groups “http://localhost:8086/OlympicSoccerMansTournamentRio2016/1” as a subject to my query, hence in the query below it return the predicates and objects from the triples of concept group.

SELECT DISTINCT ?p ?o
   WHERE{   
      <http://localhost:8086/OlympicSoccerMansTournamentRio2016/1> ?p ?o     
   }


The previous query return a lot of information that we don’t want all of them, because Poolparty store admin informations like who created the concept, when the concept was created in triples as well, but what I want is get the preference labels, so I add another restriction, the predicate that determine the relation between subject and object, so I get the object that I want, in this case preference labels.

SELECT DISTINCT  ?o
   WHERE{   
<http://localhost:8086/OlympicSoccerMansTournamentRio2016/1>  
<http://www.w3.org/2004/02/skos/core#prefLabel>
?o. 
   }


One thing that helps a lot when I need to create a query is add some semantic on it, so in the query below I just change one little thing, I replaced the ?o by ?label. Now it is clean that the return values are labels.

SELECT DISTINCT  ?label
   WHERE{   
<http://localhost:8086/OlympicSoccerMansTournamentRio2016/1>        
<http://www.w3.org/2004/02/skos/core#prefLabel>
?label. 
   }


This last query is a little bit complex that the others, but it is a good example to show how the complexity of the query can help us to navigate through the knowledge graph and get the information that we want. The idea of the query is given a term, get all labels from the narrows terms. So the restrictions of this query are saying: first get me all labels for a concept, after that get all narrow concept of the concept that you got first, then all labels of the narrow concept and finally print out the label of the first and the labels of the narrow concept.

SELECT DISTINCT  ?label ?narrowlabel
   WHERE{   
<http://localhost:8086/OlympicSoccerMansTournamentRio2016/1>
<http://www.w3.org/2004/02/skos/core#prefLabel>
?label ;   
<http://www.w3.org/2004/02/skos/core#narrower>
 ?narrow.
      ?narrow    
<http://www.w3.org/2004/02/skos/core#prefLabel>
?narrowlabel.
   }




As I said, it is just a little bit about the SPARQL queries, if you want learn more about you can check this website out. Be prepared, because in the next post I will talk about SPARQL vocabularies.

Leave a Reply

Your email address will not be published. Required fields are marked *