SPARQL Examples: Difference between revisions

From info216
Line 1: Line 1:
==Lecture 3:SPARQL==
==Lecture 3:SPARQL==


The KG4News knowledge graph can be accessed [[http://bg.i2s.uib.no/bigdata/ here]].
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===
===Limit===
Line 9: Line 9:
}
}
LIMIT 10</syntaxhighlight>
LIMIT 10</syntaxhighlight>
===List distinct properties only (with limit)===
<syntaxhighlight>
SELECT DISTINCT ?p WHERE {
?s ?p ?o .
}
LIMIT 10
</syntaxhighlight>
===Limit with offset===
<syntaxhighlight>
SELECT DISTINCT ?p WHERE {
?s ?p ?o .
}
LIMIT 10 OFFSET 9
</syntaxhighlight>
===List rdf:types===
<syntaxhighlight>
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>
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>
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>
PREFIX ss: <http://semanticscholar.org/>
SELECT DISTINCT ?paper ?title WHERE {
    ?paper ss:title ?title . 
    FILTER(CONTAINS(STR(?title), "Semantic Web"))
}
LIMIT 50
</syntaxhighlight>
===Similar filter that ignores capitalisation (lower/upper case)===
<syntaxhighlight>
    FILTER(REGEX(STR(?title), "Semantic Web", "i"))
</syntaxhighlight>
===Authors sorted by name===
<syntaxhighlight>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT * WHERE {
    ?author foaf:name ?name . 
}
ORDER BY ?name
LIMIT 10
</syntaxhighlight>


===Limit===
===Limit===
<syntaxhighlight>
<syntaxhighlight>
</syntaxhighlight>
</syntaxhighlight>

Revision as of 14:24, 31 January 2023

Lecture 3:SPARQL

The KG4News knowledge graph can be accessed here (namespace kb) (read-only). To test updates, you can run your own Blazegraph server or try the I2S sandbox.

Limit

SELECT ?p WHERE {
?s ?p ?o .
}
LIMIT 10

List distinct properties only (with limit)

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

Limit with offset

SELECT DISTINCT ?p WHERE {
?s ?p ?o .
}
LIMIT 10 OFFSET 9

List rdf:types

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

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

URI for Tim Berners-Lee

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

Describe Tim Berners-Lee

DESCRIBE <http://i2s.uib.no/kg4news/author/1432678629>

DESCRIBE returns a new RDF graph, whereas SELECT returns a table of rows.

Papers that mention "Semantic Web" in the title

PREFIX ss: <http://semanticscholar.org/>

SELECT DISTINCT ?paper ?title WHERE {
    ?paper ss:title ?title .  
 
    FILTER(CONTAINS(STR(?title), "Semantic Web"))
}
LIMIT 50

Similar filter that ignores capitalisation (lower/upper case)

    FILTER(REGEX(STR(?title), "Semantic Web", "i"))

Authors sorted by name

PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT DISTINCT * WHERE {
    ?author foaf:name ?name .  
}
ORDER BY ?name
LIMIT 10

Limit