How to Use Grep's SPARQL Service - Utdanningsdirektoratet/Grep_SPARQL GitHub Wiki

This is the translated version of "Hvordan bruke Greps SPARQL-tjeneste"

What is SPARQL?

The term SPARQL is an acronym for "SPARQL Protocol and RDF Query Language". In other words, it is a query language for RDF, or more precisely, a semantic query language for databases to retrieve (and manipulate) data stored in the Resource Description Framework (RDF) format. Such databases are often called "Triple Stores" because all data is represented as statements consisting of three semantic components: subject, predicate, and object (in that order).

Check out the article "What is SPARQL?" (external link) from Ontotext to learn more about SPARQL basics. You can also refer to the introductory tips in the SPARQL collection or the page "Some Selected SPARQL Tips and Tricks" for starting help and in-depth reading about SPARQL. We’ve also created a SPARQL course: Search in Grep Using SPARQL.

What can I find in Grep’s SPARQL service?

In principle, you will find the same data as in the "regular" Grep API (REST/JSON), but here it is adapted to the RDF format. In the article Flattening Objects and Elevating Attributes, JSON vs JSON-LD@nob, we describe this further (for the particularly curious 🤓).
You can also find more information about the "regular" API at https://github.com/Utdanningsdirektoratet/KL06-LK20-public/wiki.

Where can I find Grep’s SPARQL service?

See the article Access to Environments@nob.

Two Interfaces to Access Content in Grep’s SPARQL Service

We offer two interfaces for querying Grep content using SPARQL: one for humans (GraphDB Workbench) and one for machines (dedicated SPARQL endpoint):

  1. GraphDB Workbench (for Humans)
  2. Endpoint for Machines

Additionally, there is an endpoint where you can download a full dump of JSON-LD files collected in a daily-updated ZIP file (link to REST wiki), which can be imported into your own SPARQL endpoint.

GraphDB Workbench (for Humans)

Before using SPARQL in your own code or service, it might be useful to test your queries in a more exploratory way using the Workbench interface in GraphDB. Here, you can verify whether your queries return the expected results and share the output with others in a table format.

Steps:

Go to https://sparql-data.udir.no/sparql for the production environment or https://sparql-beta-data.udir.no/sparql (prefer Beta for testing). See the article on Environments for more details.

Select repository, then click "SPARQL"
Select the repository "201906" at the top right of the page, then click "SPARQL" in the left menu (no login required).

You’ll then see this interface: Page with default query ("give me everything, but only 100 instances")
Write your query by replacing the text in the large editor with the example query below.

prefix u: <http://psi.udir.no/ontologi/kl06/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?kmkode ?kmtekst ?laereplantittel ?laereplan WHERE {
?kompetansemaal rdf:type u:kompetansemaal_lk20 ;
    u:tittel ?kmtekst ;
    u:kode ?kmkode ;
    u:tilhoerer-kompetansemaalsett ?kms .
?kms u:etter-fag ?opplaeringsfag .
?opplaeringsfag u:laereplan-referanse ?laereplan .
?laereplan u:tittel ?laereplantittel .
    FILTER regex(?kmtekst, "brøk", "i")
    FILTER (lang(?kmtekst) = "default")
    FILTER (lang(?laereplantittel) = "default")
 } ORDER BY ?laereplan ?kmkode

It should now look like this: Lin inn spørringen og klikk "Run"
Click the "Run" button, and you’ll see this result:

Return after SPARQL query
Go back to the query window. Notice the word "brøk" in the regex filter. Replace it with another word you’d like to search for and click "Run" again.

You can also get results in other formats, like JSON (called "Raw Response" in Workbench):
Select "Raw respons" or another format
This shows what a JSON result looks like (see the section below on endpoints for machines).

Endpoint for Machines

Key Principles:

  1. URL for the machine-readable SPARQL endpoint is https://sparql-data.udir.no/repositories/201906 in production and, https://sparql-beta-data.udir.no/repositories/201906 in beta (prefer Beta for testing). See Environments for more details.
  2. URL requests must include the header Accept:application/json (in the previous solution, you could specify the format directly as a parameter in the URL).

Testing in Beta - Examples:

Via Postman:

  • Use the GET method and enter the request URL: "https://sparql-beta-data.udir.no/repositories/201906"
  • In the Params tab: Add the key "query," where the value is a URL-encoded version of the query itself (e.g., copy the query you tested in Workbench).
  • In the Headers tab: Add the key "Accept" with the value "application/json".

Via Insomnia:

  • Create a new request (name it as you like) and select GET as the method.
  • GET: Enter the URL: "https://sparql-beta-data.udir.no/repositories/201906".
  • In the Query tab: Add "query," click the arrow to the right of "value," and select "Text Multi-line." Then click "Click to Edit" and paste a query you copied from Workbench (not encoded).
  • In the Headers tab: Add:
    • Accept with the value "application/json".
    • Content-Type with the value "application/x-www-form-urlencoded".
  • Click "Send" (to the right of the GET URL).

Via Curl:

The principle is the same as in the Postman example above; send a URL request with a URL-encoded query and the header "Accept:application/json", for example:

 $ curl -H "Accept:application/json" https://sparql-beta-data.udir.no/repositories/201906?query=prefix%20u%3A%20%3Chttp%3A%2F%2Fpsi.udir.no%2Fontologi%2Fkl06%2F%3E%20%0Aprefix%20rdf%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0ASELECT%20%20%2A%20WHERE%20%7B%0A%3Furi%20rdf%3Atype%20u%3Alaereplan_lk20%20%0A%7D%20LIMIT%2010



Also, check out this YouTube video by Prof. Dr. Harald Sack, where he discusses various "SPARQL Output Formats" (1:12–7:10), not just JSON...

⚠️ **GitHub.com Fallback** ⚠️