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: You can use the data in the Turtle file [File:russia_investigation_kg.ttl russia_investigation_kg.ttl].
You can now run SPARQL queries and updates and load RDF graphs from your file into Blazegraph. In the update tab, load RDF data (select type below) and then paste the contents of your turtle/.txt file to add them all at once to the database. If you have not serialized your graph from lab 2 yet, you can use the triples on the bottom of the page instead. Just copy and paste them into the Update section.
Tasks
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.
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.
Insert these triples into your RDF graph, if you have not done so before:
- George Papadopoulos was adviser to the Trump campaign.
- He pleaded guilty to lying to the FBI.
- He was sentenced to prison.
- Michael Flynn was adviser to Donald Trump.
- He pleaded guilty for lying to the FBI.
- He negotiated a plea agreement.
- Michael Cohen was Donald Trump's attorney.
- He pleaded guilty for lying to Congress.
- 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.
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:
- 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...
- 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: