Query your data

Query your data

Query data through the Workbench

Tip

SPARQL is a SQL-like query language for RDF graph databases with the following types:

  • SELECT: returns tabular results;

  • CONSTRUCT: creates a new RDF graph based on query results;

  • ASK: returns YES if the query has a solution, otherwise “NO”;

  • DESCRIBE: returns RDF data about a resource; useful when you do not know the RDF data structure in the data source;

  • INSERT: inserts triples into a graph;

  • DELETE: deletes triples from a graph.

For more information, see The SPARQL query language.

Now it is time to delve into your data. The following is one possible scenario for querying it.

  1. Select the repository you want to work with, in this example News, and click the SPARQL menu tab.

  2. Let’s say you are interested in people. Paste the query below into the query field, and click Run to find all people mentioned in the documents from this news articles dataset.

    PREFIX pub: <http://ontology.ontotext.com/taxonomy/>
    PREFIX pub-old: <http://ontology.ontotext.com/publishing#>
    select distinct ?x ?Person  where {
    ?x a pub:Person .
    ?x pub:preferredLabel ?Person .
    ?doc pub-old:containsMention / pub-old:hasInstance ?x .
    }
    
    _images/news-scenario-all-people.png
  3. Run a query to calculate the RDF rank of the instances based on their interconnectedness.

    PREFIX rank: <http://www.ontotext.com/owlim/RDFRank#>
    INSERT DATA { _:b1 rank:compute _:b2. }
    
  4. Find all people mentioned in the documents, ordered by popularity in the repository.

    PREFIX pub: <http://ontology.ontotext.com/taxonomy/>
    PREFIX pub-old: <http://ontology.ontotext.com/publishing#>
    PREFIX rank: <http://www.ontotext.com/owlim/RDFRank#>
    select distinct ?x ?PersonLabel ?rank where {
        ?x a pub:Person .
        ?x pub:preferredLabel ?PersonLabel .
        ?doc pub-old:containsMention / pub-old:hasInstance ?x .
        ?x rank:hasRDFRank ?rank .
    } ORDER by DESC (?rank)
    
    _images/news-scenario-ordered-by-popularity.png
  5. Find all people who are mentioned together with their political parties.

    PREFIX pub-old: <http://ontology.ontotext.com/publishing#>
    PREFIX pub: <http://ontology.ontotext.com/taxonomy/>
    select distinct ?personLabel ?partyLabel where {
        ?document pub-old:containsMention ?mention .
        ?mention pub-old:hasInstance ?person .
        ?person pub:preferredLabel ?personLabel .
        ?person pub:memberOfPoliticalParty ?party .
        ?party pub:hasValue ?value .
        ?value pub:preferredLabel ?partyLabel .
    }
    
    _images/news-scenario-people-and-political-parties.png
  6. Did you know that Marlon Brando was from the Democratic Party? Find what other mentions occur together with Marlon Brando in the given news article.

    PREFIX pub: <http://ontology.ontotext.com/taxonomy/>
    PREFIX pub-old: <http://ontology.ontotext.com/publishing#>
    select distinct ?Mentions where {
    <http://www.reuters.com/article/2014/10/06/us-art-auction-idUSKCN0HV21B20141006> pub-old:containsMention / pub-old:hasInstance ?x .
    ?x pub:preferredLabel ?Mentions .
    
    }
    
    _images/news-scenario-MB-and-other-mentions.png
  7. Find everything available about Marlon Brando in the database.

    PREFIX pub: <http://ontology.ontotext.com/taxonomy/>
    PREFIX pub-old: <http://ontology.ontotext.com/publishing#>
    select distinct ?p ?objectLabel where {
    <http://ontology.ontotext.com/resource/tsk78dfdet4w> ?p ?o .
        {
    ?o pub:hasValue ?value .
        ?value pub:preferredLabel ?objectLabel .
        } union {
            ?o pub:hasValue ?objectLabel .
            filter (isLiteral(?objectLabel)) .
         }
    }
    
    _images/news-scenario-MB-data.png
  8. Find all documents that mention members of the Democratic Party and the names of these people.

    PREFIX pub-old: <http://ontology.ontotext.com/publishing#>
    PREFIX pub: <http://ontology.ontotext.com/taxonomy/>
    select distinct ?document ?personLabel where {
        ?document pub-old:containsMention ?mention .
        ?mention pub-old:hasInstance ?person .
        ?person pub:preferredLabel ?personLabel .
        ?person pub:memberOfPoliticalParty ?party .
        ?party pub:hasValue ?value .
        ?value pub:preferredLabel "Democratic Party"@en .
    }
    
    _images/news-scenario-all-DP-members-and-names.png
  9. Find when these people were born and died.

    PREFIX pub-old: <http://ontology.ontotext.com/publishing#>
    PREFIX pub: <http://ontology.ontotext.com/taxonomy/>
    select distinct ?person ?personLabel ?dateOfbirth ?dateOfDeath where {
        ?document pub-old:containsMention / pub-old:hasInstance ?person .
        ?person pub:preferredLabel ?personLabel .
        OPTIONAL {
           ?person pub:dateOfBirth / pub:hasValue ?dateOfbirth .
        }
        OPTIONAL {
           ?person pub:dateOfDeath / pub:hasValue ?dateOfDeath .
        }
        ?person pub:memberOfPoliticalParty / pub:hasValue / pub:preferredLabel "Democratic Party"@en .
    } order by ?dateOfbirth
    
    _images/news-scenario-dob-dod.png

Tip

You can play with more example queries from the Example_queries.rtf file provided in the examples/data/news directory of the GraphDB distribution.

Query data programmatically

SPARQL is not only a standard query language, but also a protocol for communicating with RDF databases. GraphDB stays compliant with the protocol specification, and allows querying data with standard HTTP requests.

Execute the example query with an HTTP GET request:

curl -G -H "Accept:application/x-trig" \
  -d query=CONSTRUCT+%7B%3Fs+%3Fp+%3Fo%7D+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10 \
  http://localhost:7200/repositories/yourrepository

Execute the example query with a POST operation:

curl -X POST --data-binary @file.sparql -H "Accept: application/rdf+xml" \
  -H "Content-type: application/x-www-form-urlencoded" \
  http://localhost:7200/repositories/yourrepository

where file.sparql contains an encoded query:

query=CONSTRUCT+%7B%3Fs+%3Fp+%3Fo%7D+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

Tip

For more information on how to interact with GraphDB APIs, refer to the RDF4J and SPARQL protocols or the Linked Data Platform specifications.