SPARQL (RDF) Interpreter

Graph Studio provides a SPARQL (RDF) interpreter which allows you to run SPARQL queries on an RDF graph in a notebook paragraph.

See SPARQL Protocol and RDF Query Language (SPARQL) for more information on W3C SPARQL 1.1 standard.

To use the SPARQL (RDF) interpreter, you must specify %sparql-rdf at the beginning of the notebook paragraph and then input the SPARQL query.

Tip:

You can hover over the bottom part of a notebook paragraph and click the rdf_icon Add RDF Paragraph icon to open an SPARQL (RDF) paragraph instantly in the notebook.

You can run the following types of SPARQL queries:

  • SELECT
  • ASK
  • CONSTRUCT
  • DESCRIBE
  • INSERT, DELETE, CLEAR, and other supported SPARQL queries for graph update operations. See SPARQL 1.1 Update Specification for more information.

Also, note that execution of SPARQL SELECT and ASK queries return a tabular output and execution of SPARQL CONSTRUCT and DESCRIBE queries return a graph view of the resulting output.

If your user account is associated with just one RDF graph, then you can directly run the SPARQL query as shown:

%sparql-rdf
PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX   ms: <http://www.example.com/moviestream/>

SELECT ?title ?revenue
WHERE {
  ?movie ms:actor ?actor .
  ?actor ms:name "Kevin Bacon" .
  ?movie ms:title ?title .
  ?movie ms:grossInUSD ?revenue
}

The preceding SELECT SPARQL query is automatically applied on the default RDF graph existing in the account. The query aims to project the title and revenue in USD of all movies starring "Kevin Bacon", using multiple triple patterns in the WHERE clause. On execution, the query output is displayed in a tabular format as shown:

Description of sparql_query1_result.png follows
Description of the illustration sparql_query1_result.png

In case you have multiple RDF graphs in your account, then a selection box is displayed when you run the first SPARQL query in the notebook. You can select the desired graph and then rerun the paragraph. This selection is automatically applied to all other SPARQL (RDF) paragraphs.

The following example performs a SPARQL update operation. The example uses a SPARQL INSERT query to add new data for a movie.

%sparql-rdf
################################################################
# Insert new data for Minions: The Rise of Gru
################################################################

PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX   ms: <http://www.example.com/moviestream/>

INSERT DATA {
  ms:movie_4004 ms:title "Minions: The Rise of Gru" ;
                ms:year "2022"^^xsd:decimal ;
                ms:openingDate "2022-07-01"^^xsd:date ;
                ms:runtimeInMin "87"^^xsd:decimal ;
                ms:director ms:entity_kyle%20balda ;
                ms:views "100"^^xsd:decimal .
}