To jest stara wersja strony!
Querying the Semantic Web with SPARQL
Last verification: | 20180912 |
Tools required for this lab: | – |
Before the lab
Video [minimum!]:
Reading:
Lab instructions
1 Introduction [5 minutes]
What can we do with our RDF models? In this section some „magic” will happen on
Periodic Table saved in
RDF!
Open
SPARQLer (a general purpose SPARQL query processor).
Paste
http://www.daml.org/2003/01/periodictable/PeriodicTable.owl
into „Target graph
URI (or use FROM in the query)” field, select
Text
in „Output” dropdown list.
Run the following two queries (paste code in text field and click
Get Results
):
- select.rq
PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?element ?name
WHERE {
?element table:group ?group .
?group table:name "Noble gas"^^xsd:string .
?element table:name ?name .
}
ORDER BY ASC(?name)
- construct.rq
PREFIX table: <http://www.daml.org/2003/01/periodictable/PeriodicTable#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?element rdfs:label ?name .
}
WHERE {
?element table:group ?group .
?group table:name "Noble gas"^^xsd:string .
?element table:name ?name .
}
ORDER BY ASC(?name)
What do
SELECT
queries do?
What do
CONSTRUCT
queries do?
2 SPARQL = Pattern matching [10 minutes]
General Idea: SPARQL is an RDF graph pattern matching system.
E.g.: there is a triple saved in RDF:
:JamesDean :playedIn :Giant .
Now we can simply replace part of the triple with a question word (with a question mark at the start) and we get simple queries, e.g.:
Query: :JamesDean :playedIn ?what .
Answer: :Giant
Query: ?who :playedIn :Giant .
Answer: :JamesDean
Query: :JamesDean ?what :Giant .
Answer: :playedIn
Let's get back to our
FOAF files. Do you have yours?
Execute queries on your foaf file (or on http://krzysztof.kutt.pl/foaf.rdf
file) to retrieve:
friends who have name and e-mail defined
friends who have name and e-mail defined and optional homepage
friends who have name and e-mail defined and optional homepage, sorted by name descending
Put the constructed queries in the report.
Hints:
-
-
Type in the location of your file in the
Target graph URI
OR use
FROM
construct to define your data source, e.g.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name
FROM <http://krzysztof.kutt.pl/foaf.rdf>
WHERE {
?x rdf:type foaf:Person .
?x foaf:name ?name
}
LIMIT 10
3 Constraints: FILTER [10 minutes]
4 SPARQL Endpoint [20 minutes]
DBpedia is a dump of Wikipedia annotated using RDF. So, like Wikipedia, DBpedia should contain some information about Poland. What we can do?
We don't know what
URI Poland has in DBpedia, but we know the name Poland, and from previous lab we know rdfs:label property. Maybe this will help us? Let's try!
Open the
SPARQLer. What we know so far? There should be some
URI (
?country
) that probably has a relation
rdfs:label
with object
„Polska”@pl
. This can be easily translated into SPARQL WHERE clause:
?country rdfs:label "Polska"@pl .
To execute this query properly we also have to specify that we are asking
http://dbpedia.org/sparql
Endpoint. We can't use FROM clause, because this one is designated for RDF graphs. We should use SERVICE instead. Final query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?country
WHERE {
SERVICE <http://dbpedia.org/sparql> {
?country rdfs:label "Polska"@pl .
}
}
Note 1: There must be something in Target graph URI
field (this can be FOAF URI from previous section) – this URI will not be used in query but SPARQLer needs it to execute query…
Note 2: You can use more than one SERVICE
clauses in SPARQL query – it gives you a possibility to combine results from different SPARQL Endpoints.
Success! There is something that has rdfs:label
„Polska”@pl
!
Now expand this query to find information about Poland population:
Put the final query in the report.
Hint: result should look like this:
--------------
| population |
==============
| 38483957 |
--------------
Prepare a query that returns a list of 10 countries in Europe with the biggest population. Put the query in the report.
5 Aggregation [15 minutes]
Poland is divided into 16 voivodeships (PL: województwo), and then into 380 counties (PL: powiat). In this task, we will examine it closer.
Prepare a query (in
SPARQLer, against DBpedia) which returns list of voivodeships and number of counties inside them. List should consist only of voivodeships with 7 or more counties and should be ordered by number of counties.
Results should look like that:
------------------------------------------------
| voivodeship | counties |
================================================
| "Masovian Voivodeship"@en | 15 |
| "Greater Poland Voivodeship"@en | 12 |
| "Lesser Poland Voivodeship"@en | 11 |
| "Podkarpackie Voivodeship"@en | 10 |
| "Pomeranian Voivodeship"@en | 9 |
| "Warmian-Masurian Voivodeship"@en | 9 |
| "West Pomeranian Voivodeship"@en | 9 |
| "Opole Voivodeship"@en | 8 |
------------------------------------------------
or in Polish:
--------------------------------------------------
| wojewodztwo | powiaty |
==================================================
| "Województwo mazowieckie"@pl | 15 |
| "Województwo wielkopolskie"@pl | 12 |
| "Województwo małopolskie"@pl | 11 |
| "Województwo podkarpackie"@pl | 10 |
| "Województwo pomorskie"@pl | 9 |
| "Województwo warmińsko-mazurskie"@pl | 9 |
| "Województwo zachodniopomorskie"@pl | 9 |
| "Województwo opolskie"@pl | 8 |
--------------------------------------------------
Hint – useful URIs:
Put the query in the report.
6 SPARQL as rule language [10 minutes]
So far, we have seen that the answers to questions in SPARQL can take the form of a table. In this section we will take a look at CONSTRUCT queries which answers take the form of an RDF graph. You have already seen one such example in
Introduction.
OK, we created some new RDF triples using CONSTRUCT query. What now? Depending on your plans, you can:
And then simply execute queries against this new knowledge.
7 ASK and DESCRIBE queries [10 minutes]
SPARQL also provides two more query types: ASK and DESCRIBE.
DESCRIBE queries return all knowledge associated with given Subject
URI(s).
The simplest DESCRIBE query specifies only the
URI that should be described:
DESCRIBE <http://krzysztof.kutt.pl/foaf.rdf#me>
(it should be executed against http://krzysztof.kutt.pl/foaf.rdf
file)
There is also possibility to select
URI(s) from data set using constraints defined in WHERE clause. Read about it in
SPARQL 1.1 documentation.
Prepare query that describes all
foaf:Person
items from your
model (if you don't have a
foaf:Person
class in your library, use analogous class that you have).
8 "Negation" under Open World Assumption [5 minutes]
Control questions
If you want to know more...
SPARQL:
Sample queries in SPARQL:
Tools:
SPARQLer – general purpose tool for executing SPARQL queries
-
YASGUI – online visual tool for querying SPARQL Endpoints
-
-
GeoSPARQL – standard for representing and querying the geospatial data using RDF
Open Data Sets:
DB2RDF (RDF and Relational Databases):