Lab: SPARQL
Topics
- Setting up the Blazegraph graph database. Previously we have only stored our triples in memory, which is not persistent.
- SPARQL queries and updates. We use SPARQL to retrieve of update triples in our databases/graphs of triples
Useful materials
Blazegraph:
SPARQL:
Tasks
Running Blazegraph
You can either run Blazegraph locally on your own machine (best) or online at a local server (also ok).
Installing the Blazegraph database on your own computer: Download Blazegraph (blazegraph.jar) from here: https://blazegraph.com/ You can place blazegraph.jar in the same folder of your python project for the labs. Navigate to the folder of blazegraph.jar in your commandline/terminal using cd. (cd C:\Users\marti\info216 for me as an example). Now run this command:
java -server -Xmx4g -jar blazegraph.jar
You might have to install java 64-bit JDK if you have problems running Blazegraph. You can do it from this link. If you get an "Address already in use" error, this is likely because Blazegraph has been terminated improperly. Either restart the terminal-session or try to run this command instead:
java -server -Xmx4g -Djetty.port=19999 -jar blazegraph.jar
This changes the port of the Blazegraph server.
Running Blazegraph online: If you have trouble installing Blazegraph, you can use a shared local server for now. This is the same Blazegraph interface, but its stored in the cloud and only be used from the UiB network. You may be able to access it without connecting to the UiB Network, but if you are unable to access the endpoint try connecting via the VPN. Instructions here. If it works it should now display an URL like: "http://10.0.0.13:9999/blazegraph/". Open this in a browser.
Using Blazegraph:
- Uploading data: In the Blazegraph interface, go to the UPDATE tab and use the Browse... and Update buttons to load the file into Blazegraph.
- You can use the data in the Turtle file File:Russia investigation kg.txt. Make sure you save it with the correct extension, as russia_investigation_kg.ttl (not .txt).
- You can also use the Turtle file you saved after exercises 1 and 2.
- Querying and updating: In the Blazegraph interface, go to the QUERY and UPDATE tabs to enter queries and updates.
SPARQL tasks
Task: Using the data in russia_investigation_kg.ttl, write the following SPARQL queries:
- 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.
This page explains the Russian investigation KG a bit more.
Task: Load the RDF graph you created in exercises 1 and 2. Use INSERT DATA to add these triples to your graph:
- George Papadopoulos was adviser to the Trump campaign.
- He pleaded guilty to lying to the FBI.
- He was sentenced to prison.
- Roger Stone is a Republican.
- He was adviser to Trump.
- He was an official in the Trump campaign.
- He interacted with Wikileaks.
- He was indicted for making false statements, witness tampering, and obstruction of justice.
- He made a testimony for the House Intelligence Committee.
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.
Use INSERT DATA to add information about Sergio Pastor, who lives in 4 Carrer del Serpis, 46021 Valencia, Spain. he has a M.Sc. in computer from the University of Valencia from 2008. His areas of expertise include big data, semantic technologies and machine learning.
Write a SPARQL DELETE/INSERT update to change the name of "University of Valencia" to "Universidad de Valencia" whereever it occurs.
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.
If you have more time
Task: Try to program some of the queries/updates in a Python program (this will be the topic of later labs). You have two options:
Using rdflib: Read the Turtle file into an rdflib Graph and use the query() method.
g = Graph() g.parse(..., format='ttl') r = g.query(...your_query_string...)
The hard part is picking the results out of the object r...
Using SPARQLwrapper: You can use SPARQLwrapper (another Python API) to connect to your running Blazegraph endpoint. See the Python example page for how to do this.
Task: If you want to explore more, try out Wikidata Query Service (WDQS)
WDQS tutorials: