|
|
(2 intermediate revisions by 2 users not shown) |
Line 1: |
Line 1: |
| ==Lecture 3:SPARQL==
| |
|
| |
|
| The KG4News knowledge graph can be accessed [http://bg.newsangler.uib.no here (namespace ''kb'')] (read-only). To test updates, you can run your own Blazegraph server or try the [http://sandbox.i2s.uib.no I2S sandbox].
| |
|
| |
| ===Limit===
| |
| <syntaxhighlight lang="SPARQL">
| |
| SELECT ?p WHERE {
| |
| ?s ?p ?o .
| |
| }
| |
| LIMIT 10</syntaxhighlight>
| |
|
| |
| ===List distinct properties only (with limit)===
| |
| <syntaxhighlight lang="SPARQL">
| |
| SELECT DISTINCT ?p WHERE {
| |
| ?s ?p ?o .
| |
| }
| |
| LIMIT 10
| |
| </syntaxhighlight>
| |
|
| |
| ===Limit with offset===
| |
| <syntaxhighlight lang="SPARQL">
| |
| SELECT DISTINCT ?p WHERE {
| |
| ?s ?p ?o .
| |
| }
| |
| LIMIT 10 OFFSET 9
| |
| </syntaxhighlight>
| |
|
| |
| ===List rdf:types===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
| |
|
| |
| SELECT DISTINCT ?t WHERE {
| |
| ?s rdf:type ?t .
| |
| }
| |
| LIMIT 50
| |
| </syntaxhighlight>
| |
|
| |
| ===URI for Tim Berners-Lee===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/>
| |
|
| |
| SELECT DISTINCT ?person WHERE {
| |
| ?person foaf:name ?name .
| |
|
| |
| FILTER(CONTAINS(?name, "Berners-Lee"))
| |
| }
| |
| LIMIT 10 # best to use limit if something goes wrong
| |
| </syntaxhighlight>
| |
|
| |
| ===Describe Tim Berners-Lee===
| |
| <syntaxhighlight lang="SPARQL">
| |
| DESCRIBE <http://i2s.uib.no/kg4news/author/1432678629>
| |
| </syntaxhighlight>
| |
| DESCRIBE returns a new RDF graph, whereas SELECT returns a table of rows.
| |
|
| |
| ===Papers that mention "Semantic Web" in the title===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX ss: <http://semanticscholar.org/>
| |
|
| |
| SELECT DISTINCT ?paper ?title WHERE {
| |
| ?paper ss:title ?title .
| |
|
| |
| FILTER(CONTAINS(STR(?title), "Semantic Web"))
| |
| }
| |
| LIMIT 50
| |
| </syntaxhighlight>
| |
|
| |
| ===Alternative filter that ignores capitalisation (lower/upper case)===
| |
| <syntaxhighlight lang="SPARQL">
| |
| FILTER(REGEX(STR(?title), "Semantic Web", "i"))
| |
| </syntaxhighlight>
| |
|
| |
| ===Authors sorted by name===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/>
| |
|
| |
| SELECT DISTINCT * WHERE {
| |
| ?author foaf:name ?name .
| |
| }
| |
| ORDER BY ?name
| |
| LIMIT 10
| |
| </syntaxhighlight>
| |
|
| |
| ===Sorted by descending name instead===
| |
| <syntaxhighlight lang="SPARQL">
| |
| ORDER BY DESC(?name)
| |
| </syntaxhighlight>
| |
|
| |
| ===Count papers by author===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
| |
| PREFIX dct: <http://purl.org/dc/terms/>
| |
| PREFIX ss: <http://semanticscholar.org/>
| |
|
| |
| SELECT DISTINCT ?author (COUNT(?paper) AS ?count) WHERE {
| |
| ?author rdf:type ss:Author .
| |
| ?paper rdf:type ss:Paper ;
| |
| dct:contributor ?author .
| |
| }
| |
| GROUP BY ?author
| |
| LIMIT 10
| |
| </syntaxhighlight>
| |
|
| |
| ===Only list the most prolific authors===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
| |
| PREFIX dct: <http://purl.org/dc/terms/>
| |
| PREFIX ss: <http://semanticscholar.org/>
| |
|
| |
| SELECT DISTINCT ?author (COUNT(?paper) AS ?count) WHERE {
| |
| ?author rdf:type ss:Author .
| |
| ?paper rdf:type ss:Paper ;
| |
| dct:contributor ?author .
| |
| }
| |
| GROUP BY ?author
| |
| HAVING (?count >= 10) # similar to a filter expression
| |
| LIMIT 10 # include limit when you test
| |
| </syntaxhighlight>
| |
|
| |
| ===Order by descending paper count===
| |
| <syntaxhighlight lang="SPARQL">
| |
| SELECT ... {
| |
| ...
| |
| }
| |
| GROUP BY ?person
| |
| HAVING (?count > 10)
| |
| ORDER BY DESC(?count)
| |
| LIMIT 10
| |
| </syntaxhighlight>
| |
|
| |
| ===Order by descending paper count and then by author name===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
| |
| PREFIX dct: <http://purl.org/dc/terms/>
| |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/>
| |
| PREFIX ss: <http://semanticscholar.org/>
| |
|
| |
| SELECT DISTINCT ?person (SAMPLE(?name) AS ?name) (COUNT(?paper) AS ?count) WHERE {
| |
| ?person rdf:type ss:Author ;
| |
| foaf:name ?name .
| |
| ?paper rdf:type ss:Paper ;
| |
| ss:title ?title ;
| |
| dct:contributor ?person .
| |
| }
| |
| GROUP BY ?person
| |
| HAVING (?count > 10)
| |
| ORDER BY DESC(?count)
| |
| LIMIT 10
| |
| </syntaxhighlight>
| |
|
| |
| ===Embedded Wikidata query===
| |
| This is a toy example only. Embedded queries like these are better suited for situations where the same URIs are used in more than one triple store and you want to combine data. But Wikidata and KG4News do not use the same URIs. So instead, the example searches for similar labels, and this is something graph databases may not be optimised for. Moreover, Wikidata uses language-tagged strings whereas KG4News uses plain strings, so the labels cannot even be directly compared.
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/>
| |
| PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
| |
|
| |
| SELECT DISTINCT ?enname ?person ?wdperson WHERE {
| |
| BIND("T. Berners-Lee"@en AS ?enname) # language-tagger label
| |
|
| |
| SERVICE <https://query.wikidata.org/bigdata/namespace/wdq/sparql> {
| |
| # return a Wikidata identifier (URI) with this label as alternative
| |
| SELECT ?wdperson ?enname WHERE {
| |
| ?wdperson skos:altLabel ?enname .
| |
| }
| |
| LIMIT 10 # we use limit in case the label does not match
| |
| }
| |
|
| |
| BIND(STR(?enname) AS ?name) # the same label, but with language tag removed
| |
| # return a KG4News identifier (URI) with this label as name
| |
| ?person foaf:name ?name .
| |
|
| |
| }
| |
| LIMIT 10
| |
| </syntaxhighlight>
| |
|
| |
| ===Add one or more triples===
| |
| From now on you need a Blazegraph that allows writing, for example the [http://sandbox.i2s.uib.no I2S sandbox]. Remember to ''create a new namespace'' first and make sure you ''use'' it afterwards.
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
| |
| PREFIX dct: <http://purl.org/dc/terms/>
| |
| PREFIX kg: <http://i2s.uib.no/kg4news/>
| |
| PREFIX ss: <http://semanticscholar.org/>
| |
|
| |
| INSERT DATA { # note the Turtle-like syntax
| |
| kg:paper_123 rdf:type ss:Paper ;
| |
| ss:title "Semantic Knowledge Graphs for the News: A Review"@en ;
| |
| kg:year 2022 ;
| |
| dct:contributor kg:auth_456, kg:auth_789 .
| |
| }
| |
| </syntaxhighlight>
| |
|
| |
| ===Remove one or more triples===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX kg: <http://i2s.uib.no/kg4news/>
| |
|
| |
| DELETE DATA
| |
| {
| |
| kg:paper_123 kg:year 2022 .
| |
| }
| |
| </syntaxhighlight>
| |
|
| |
| ===Pattern-based addition and or removal of triples===
| |
| <syntaxhighlight lang="SPARQL">
| |
| PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
| |
| PREFIX dct: <http://purl.org/dc/terms/>
| |
| PREFIX kg: <http://i2s.uib.no/kg4news/>
| |
| PREFIX ss: <http://semanticscholar.org/>
| |
|
| |
| DELETE DATA {
| |
| ?paper dct:contributor kg:auth_456
| |
| }
| |
| INSERT DATA {
| |
| ?paper dct:contributor kg:auth_654
| |
| }
| |
| WHERE { # the patterns are similar to SELECT patterns
| |
| ?paper dct:contributor kg:auth_456
| |
| }
| |
| </syntaxhighlight>
| |