Using SPARQL 1.1 INSERT patterns and CONSTRUCT to Enhance Existing Data
What?
Using SPARQL 1.1 to provide new insights into existing data.Why?
There are times when existing data doesn't provide the required insights for a given audience or data analytics pursuit.How?
Use SPARQL 1.1 INSERT patterns to generate new data from existing data. Basically, this is like make a CONSTRUCT query where the output is persisted to a specific Named Graph.
The steps are as follows:
- Assume the following Raw Data Representation in Turtle:
<#rojer> <#contactType> "100 - Sport" . <#alice> <#contactType> "98 - School" . <#bob> <#contactType> "99 - University" . <#john> <#contactType> "101 - Other" . <#carol> <#contactType> "100 - Friends" . <#karine> <#contactType> "102 - Colleagues" . <#ivan> <#contactType> "99 - University" . <#gloria> <#contactType> "101 - Other" . <#kate> <#contactType> "101 - Other" . <#jordan> <#contactType> "99 - University" . <#brigitte> <#contactType> "100 - Sport" .
- Load the sample data using SPARQL 1.1.
INSERT Syntax:
INSERT { GRAPH <urn:sparql:tests:people> { <#rojer> <#contactType> "100 - Sport" . <#alice> <#contactType> "98 - School" . <#bob> <#contactType> "99 - University" . <#john> <#contactType> "101 - Other" . <#carol> <#contactType> "100 - Friends" . <#karine> <#contactType> "102 - Colleagues" . <#ivan> <#contactType> "99 - University" . <#gloria> <#contactType> "101 - Other" . <#kate> <#contactType> "101 - Other" . <#jordan> <#contactType> "99 - University" . <#brigitte> <#contactType> "100 - Sport" . } }
- Let's get all Contact types:
SELECT DISTINCT ?s2 as ?contact_type FROM <urn:sparql:tests:people> WHERE { ?s1 <#contactType> ?s2 . }
- View the SPARQL Query Definition via SPARQL Protocol URL;
- View the SPARQL Query Results via SPARQL Protocol URL
- CONSTRUCT Others: Purpose -- For every entity of the types from above to set to type: <#Friend> [a foaf:knows] when code does not start with '101 -':
CONSTRUCT { ?s1 a <#Friend> } FROM <urn:sparql:tests:people> WHERE { ?s1 <#contactType> ?s2 . FILTER ( !strStarts(?s2, "101 -") ). } # Query result: @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix ns1: <#> . ns1:rojer rdf:type ns1:Friend . ns1:karine rdf:type ns1:Friend . ns1:ivan rdf:type ns1:Friend . ns1:jordan rdf:type ns1:Friend . ns1:brigitte rdf:type ns1:Friend . ns1:alice rdf:type ns1:Friend . ns1:bob rdf:type ns1:Friend . ns1:carol rdf:type ns1:Friend .
- Generate Friends list:
INSERT { GRAPH <urn:sparql:tests:friends> { ?s1 a <#Friend> } } FROM <urn:sparql:tests:people> WHERE { ?s1 <#contactType> ?s2 . FILTER ( !strStarts(?s2, "101 -") ). }
- Let's get all Friends:
SELECT * FROM <urn:sparql:tests:friends> WHERE { ?s ?p ?o }
- View the SPARQL Query Definition via SPARQL Protocol URL;
- View the SPARQL Query Results via SPARQL Protocol URL
Related
- SPARQL 1.1. Features Examples Collection
- SPARQL 1.1. Property Paths
- SPARQL inline data via use of VALUES clause
- Virtuoso SPARQL 1.1 Syntax Tutorial
- SPARQL 1.1 Sub-queries
- SPARQL 1.1 Specification
- SPARQL Protocol (HTTP based Query Service)
- Virtuoso Documentation
- Virtuoso Tips and Tricks Collection