Lab: SPARQL Programming

From info216
Revision as of 07:15, 21 February 2020 by Say004 (talk | contribs)

Topics

SPARQL programming in python with SPARQLWrapper and Blazegraph, or alternatively RDFlib. These tasks are about programming SPARQL queries and inserts in a python program. Last week we added triples manually from the web interface.However, sometimes we would rather want to program the insertion or updates of triples for our graphs/databases.

Tasks

  • Redo all the SPARQL queries and updates from Lab 4, this time writing a Python program.


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". If you want to add other namespaces you can do it from the web-interface if Blazegraph.

When it comes to how to do some queries I recommend scrolling down on this page for help: https://github.com/RDFLib/sparqlwrapper

Remember, before you can program with Blazegraph you have to make sure its running like we did in Lab 4. Now you will be able to program queries and updates.

# How to establish connection to Blazegraph endpoint. 

from SPARQLWrapper import SPARQLWrapper, JSON, POST, DIGEST

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


Without Blazegraph

If you have not been able to run Blazegraph yet, you can instead program SPARQL queries directly with RDFlib.

For help, look at the link below:

Querying with Sparql


Useful Readings