Lab: SPARQL Programming

Revision as of 10:02, 21 February 2020 by Say004 (talk | contribs)



Last week we added triples manually from the web interface.




  • SELECT all triples in your graph.
  • SELECT all the interests of Cade.
  • SELECT the city and country of where Emma lives.
  • SELECT only people who are older than 26.
  • SELECT Everyone who graduated with a Bachelor Degree.
  • Use SPARQL Update's DELETE DATA to delete that fact that Cade is interested in Photography. Run your SPARQL query again to check that the graph has changed.
  • Write a SPARQL DESCRIBE query to get basic information about Sergio.
  • Write a SPARQL CONSTRUCT query that returns that: any city in an address is a cityOf the country of the same address.

With Blazegraph

The most important part is that we need to import a SPARQLWrapper in order to connect to the SPARQL endpoint of Blazegraph. The default namespace for blazegraph is "kb". The different namespaces for blazegraph acts as seperate graphs/databases.


When it comes to how to do some queries and updates I recommend scrolling down on this page for help: There are also some examples on our example page.

Remember, before you can program with Blazegraph you have to make sure its running like we did in Lab 4. Make sure that the URL you use with SPARQLWrapper has the same address and port as the one you get from running it. Now you will be able to program queries and updates.

# How to establish connection to Blazegraph endpoint. Also a quick select example.

from SPARQLWrapper import SPARQLWrapper, JSON, POST, DIGEST

namespace = "kb"
sparql = SPARQLWrapper("http://localhost:19999/blazegraph/namespace/"+ namespace + "/sparql")

    PREFIX ex: <>
    ex:Cade ex:interest ?interest.

results = sparql.query().convert()

for result in results["results"]["bindings"]:

The different types of queries requires different return formats:

  • SELECT and ASK: a SPARQL Results Document in XML, JSON, or CSV/TSV format.
  • DESCRIBE and CONSTRUCT: an RDF graph serialized, for example, in the TURTLE or RDF/XML syntax, or an equivalent RDF graph serialization.

Remember to make sure that you can see the changes that take place after your inserts.

Without Blazegraph

If you have not been able to run Blazegraph on your own computer yet, you can use the UiB blazegraph service: Remember to create your own namespace like said above in the web-interface.

Alternatively, you can instead program SPARQL queries directly with RDFlib.

For help, look at the link below:

Querying with Sparql

Useful Readings