engHvordan bruke Greps SPARQL tjeneste - Utdanningsdirektoratet/Grep_SPARQL GitHub Wiki

How to use Grep's SPARQL service


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 for receiving (and manipulating) data stored in the Resource Description Framework (RDF) format. Such databases are often called "Tripple store" because all data is available as statements consisting of the three semantic components subject, predicate and object (and in that order).

Read the article "What is SPARQL?" (external link) from Ontotext, to learn more basics about SPARQL.

What can I find in Greps SPARQL service?

In principle, you will find the same data as in the "regular" Grep API (REST / json), but here they are adapted to the RDF format. In the article Forflatning-av-objekter-og-oppløfting-av-attributter,-JSON-VS-JSON-LD ("Flattening" of objects and "lifting" of attributes,-JSON-VS-JSON-LD) we have described this in more detail (for those who are particularly interested :nerd_face:). You will also find more information about the "regular" API at https://www.udir.no/om-udir/data/kl06-grep/ (Norwegian only).

Two interfaces to access the contents of Grep's SPARQL service

There are two interfaces to "SPARQLing" the Grep content. One for humans (GraphDB Workbench), and one for machines (separate SPARQL endpoint):

  1. GraphDB Workbench (for humans)
  2. End point for machines

GraphDB Workbench (for humans)

Before you plan to use SPARQL in your own code / service, it may be useful to test your queries more exploratively using the GraphDB's Workbench interface. Here you can check if your queries return an expected result or not, or you can share the result with others who can read the result in a table.

This is what you do:

Go to https://sparql-data.udir.no/sparql if you want to use the production environment, or https://sparql-beta-data.udir.no/sparql (preferably use Beta for testing)

Select repository, then click on "SPARQL" Select the last repo at the top right of the page, and then click "SPARQL" in the left menu (you do not need to log in).

Then you get this picture: Default query page ("give me everything you have but only 100 instances") Write the query by replacing the text in the large edit window with the text of the sample query in the box below.

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

It should now look like this (norwegian version of the query): Paste the query and click "Run" Press the "Run" button and you will get this result:

Return after sparql query Scroll up to the query window again. Note that there is a "brøk" (fraction) in the regex filter. Replace the word "brøk" with another word you want to search for (for example "polynomer") and press "Run" again.

You can also get the result in other formats, eg json (called "Raw response" in Workbench) Select "Raw response" or another format Then you see what a json result will look like (see the section below on endpoints for machines)

End point for machines

First some main principles: The URL for the machine sparql endpoint is https://sparql-data.udir.no/repositories/201906 in the production environment, and https://sparql-beta-data.udir.no/repositories/201906 in the beta environment (preferably use Beta for testing) 2. URL Request must have header Accept: application / json (in our previous solution we could format as parameters directly in the url)

Beta Testing - Examples:

Via Postman:

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

Via Insomnia:

  • Create a new request (call it whatever you want) and select GET as the method
  • GET: Enter URL: "https://sparql-beta-data.udir.no/repositories/201906"
  • Query tab: add "query", click on the arrow to the right of "value" and select "Text Multi-line". Then click "Click to Edit", and paste a query you have copied from Workbench (not encoded)
  • Header tab: Enter
  • Accept and the value "application / json"
  • Content-Type and the value "application / x-www-form-urlencoded"
  • Click "Submit" (to the top of the GET URL)

Via Curl:

The principle is the same as for the Postman example above; send URL request with URL-coded query, with header "Accept: application / json", eg as follows:

 $ 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