Difference between revisions of "Lab: SPARQL Programming"

Line 18: Line 18:
 
When it comes to how to do some queries and updates I recommend scrolling down on this page for help: https://github.com/RDFLib/sparqlwrapper. There are also some examples on our example page.
 
When it comes to how to do some queries and updates I recommend scrolling down on this page for help: https://github.com/RDFLib/sparqlwrapper. 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  [https://wiki.uib.no/info216/index.php/Lab:_SPARQL Lab 4].  
+
Remember, before you can program with Blazegraph you have to make sure its running like we did in  [https://wiki.uib.no/info216/index.php/Lab:_SPARQL 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.
 
Now you will be able to program queries and updates.
  

Revision as of 09:57, 21 February 2020

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". The different namespaces for blazegraph acts as seperate graphs/databases.

If you want to add other namespaces you can do it from the web-interface if Blazegraph. This is especially useful if you are using the UiB link to blazegraph: "i2s.uib.no:8888/bigdata/#splash", because with your own namespace, only you can select and update your data.

When it comes to how to do some queries and updates I recommend scrolling down on this page for help: https://github.com/RDFLib/sparqlwrapper. 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")

sparql.setQuery("""
    PREFIX ex: <http://example.org/>
    SELECT * WHERE {
    ex:Cade ex:interest ?interest.
    }
""")
sparql.setReturnFormat(JSON)

results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["interest"]["value"])

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 yet,


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

For help, look at the link below:

Querying with Sparql


Useful Readings