Difference between revisions of "SPARQL Examples"

From Info216
Line 14: Line 14:
 
===List types===
 
===List types===
  
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 
+
SELECT DISTINCT ?t WHERE {
+
SELECT DISTINCT ?t WHERE {
  ?s rdf:type ?t .
+
  ?s rdf:type ?t .
} LIMIT 100
+
} LIMIT 100
  
 
===List authors===
 
===List authors===
  
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
 
+
SELECT DISTINCT ?p WHERE {
+
SELECT DISTINCT ?p WHERE {
  ?s rdf:type foaf:Person .
+
  ?s rdf:type foaf:Person .
  ?s ?p ?o .
+
  ?s ?p ?o .
} LIMIT 100
+
} LIMIT 100
  
 
===Prefrixes used===
 
===Prefrixes used===
Line 34: Line 34:
 
The examples below will assume that these are in place.
 
The examples below will assume that these are in place.
  
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
+
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
+
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dc: <http://purl.org/dc/terms/>
+
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX bibo: <http://purl.org/ontology/bibo/>
+
PREFIX bibo: <http://purl.org/ontology/bibo/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
+
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ss: <http://semanticscholar.org/>
+
PREFIX ss: <http://semanticscholar.org/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
+
PREFIX kg: <http://i2s.uib.no/kg4news/>
PREFIX sp: <http://i2s.uib.no/kg4news/science-parse/>
+
PREFIX sp: <http://i2s.uib.no/kg4news/science-parse/>
PREFIX th: <http://i2s.uib.no/kg4news/theme/>
+
PREFIX th: <http://i2s.uib.no/kg4news/theme/>
 
+
SELECT DISTINCT ?p WHERE {
+
SELECT DISTINCT ?p WHERE {
  ?s rdf:type ss:Paper .
+
  ?s rdf:type ss:Paper .
  ?s ?p ?o .
+
  ?s ?p ?o .
} LIMIT 100
+
} LIMIT 100
  
 
===Explain all types and properties===
 
===Explain all types and properties===

Revision as of 11:18, 1 February 2021

SPARQL Examples from Lecture 03

The data are available in this Blazegraph triple store: [1] .

List properties

SELECT DISTINCT ?p WHERE {
  ?s ?p ?o .
} LIMIT 100


List types

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT DISTINCT ?t WHERE {
  ?s rdf:type ?t .
} LIMIT 100

List authors

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT DISTINCT ?p WHERE {
  ?s rdf:type foaf:Person .
  ?s ?p ?o .
} LIMIT 100

Prefrixes used

The examples below will assume that these are in place.

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX bibo: <http://purl.org/ontology/bibo/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ss: <http://semanticscholar.org/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
PREFIX sp: <http://i2s.uib.no/kg4news/science-parse/>
PREFIX th: <http://i2s.uib.no/kg4news/theme/>

SELECT DISTINCT ?p WHERE {
  ?s rdf:type ss:Paper .
  ?s ?p ?o .
} LIMIT 100

Explain all types and properties

SELECT ?pt ?e WHERE {

 ?pt rdfs:comment ?e .

} LIMIT 100

List main papers

SELECT * WHERE {

 ?paper rdf:type kg:MainPaper .
 ?paper dc:date ?year .

}

Add this to show datatypes!

 BIND ( DATATYPE(?year) AS ?type )

Add this to only show years with the right type.

 FILTER ( DATATYPE(?year) = xsd:gYear )

Group and count main papers by year

SELECT ?year (COUNT(?paper) AS ?count) WHERE {

 ?paper rdf:type kg:MainPaper .
 ?paper dc:date ?year .
 FILTER ( DATATYPE(?year) = xsd:gYear )

} GROUP BY ?year

Add this to order the results ORDER BY ?year

Add this to order and only show years with more than 5 papers. HAVING (?count > 5) ORDER BY DESC(?count)

===Show papers

SELECT ?paper ?year WHERE {

 ?paper rdf:type kg:MainPaper .
 ?paper dc:date ?year .
 FILTER ( DATATYPE(?year) = xsd:gYear )

}

 OPTIONAL {
     ?paper dc:date ?year .
     FILTER ( DATATYPE(?year) = xsd:gYear )
 }
      • VALUES

SELECT ?p ?n ?year WHERE {

 ?p rdf:type kg:MainPaper .
 ?p dc:contributor ?a .
 ?a foaf:name ?n .
 ?p dc:date ?year .
 FILTER ( CONTAINS( ?n, ?str ) )
 FILTER ( CONTAINS( STR(?year), ?yr) )

 VALUES ?str { "Andreas" "David" }
 VALUES ?yr { "2020" "2019" }

}

      • PROPERTY PATH

SELECT ?p ?n WHERE {

 ?p rdf:type kg:MainPaper .
 ?p dc:contributor / foaf:name ?n .

}

SELECT ?p ?n WHERE {

 kg:MainPaper ^rdf:type ?p .
 ?p dc:contributor / foaf:name ?n .

}

SELECT ?n WHERE {

 kg:MainPaper ^rdf:type / dc:contributor / foaf:name ?n .

}


SELECT ?t WHERE {

 ?t ^dc:title / dc:subject / skos:prefLabel "SPARQL" .

}

SELECT ?t WHERE {

 ?t ^dc:title / dc:subject / th:theme / skos:prefLabel "SPARQL" .

}

SELECT ?t WHERE {

 ?t ^dc:title / dc:subject / th:theme? / skos:prefLabel "SPARQL" .

}

--

SELECT ?a ?n ?r WHERE {

 ?a rdf:type ss:Topic .
 ?a skos:prefLabel ?n .
 FILTER ( ?n = "SPARQL" )
 BIND ( STRLANG( ?n, "en" ) AS ?n2 )
   SERVICE <https://dbpedia.org/sparql> {
   ?r rdfs:label ?n2 .
   }

} LIMIT 1

--

SELECT * WHERE {

 ?p rdf:type kg:MainPaper .
 ?p dc:date ?d .
 FILTER ( DATATYPE(?d) = xsd:gYear )

}

--

SELECT * WHERE {

 ?p rdf:type kg:MainPaper .
 ?p dc:date ?d .
 FILTER ( DATATYPE(?d) = xsd:dateTime )
 BIND ( year( ?d ) AS ?dt )

}