yet another sparql 1.1 brief introduction

31
Yet another SPARQL 1.1 brief introduction Victor Saquicela Galarza, Almudena Gonzalez, and Boris Villazón-Terrazas [email protected] @boricles Slides available at: http://www.slideshare.net/boricles/

Upload: boris-villazon-terrazas

Post on 18-Jul-2015

584 views

Category:

Internet


2 download

TRANSCRIPT

Yet another SPARQL 1.1 brief introduction

Victor Saquicela Galarza, Almudena Gonzalez, and Boris Villazón-Terrazas

[email protected]

@boricles

Slides available at: http://www.slideshare.net/boricles/

SPARQL 1.1 • W3C Recommendation 21 March 2013

• New features in Query language: - Aggregate functions: COUNT, SUM, MIN, MAX, AVG,

GROUP_CONCAT, and SAMPLE.

- Subqueries: Nest the results of a query within another query

- Negation: Check the absence of triples in a graph

- Expressions in SELECT: Introduce new variables in the SELECT clause

- Property paths: Search graphs through structures that involve arbitrary-length paths

- Assignment: BIND keyword and expressions in SELECT and GROUP_BY

- Short form for CONSTRUCT

- Expanded functions and operators: EXISTS, NOT EXISTS, SUBSTR, etc.

2

Other SPARQL 1.1 specifications • Update

Language extension to express updates to an RDF graph/store

• Protocol

Changes related to the update operation

• Service description

Discover a SPARQL endpoint's capabilities and summary information of its data

• Graph Store HTTP Protocol

Update and fetch RDF graph content from a Graph Store over HTTP in the REST style

• Entailment Regimes

Define the semantics of SPARQL queries for some entailment frameworks: OWL flavors, RDFS, RIF

• Federation Extensions

Take a query and provide solutions based on information from many different sources

• Query Result JSON, CSV, TSV

3

ToC

• Update

• Federated queries

• Basic property paths

4

Some preliminary useful queries

SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o } }

Query to get the current graphs of my triplestore:

5

SELECT *

FROM <http://mygraph/>

WHERE {

?s ?p ?o

}

Query to get everything from a particular graph

A simple SPARQL INSERT query

#graph http://linkeddata.ec/graph/tutorial/example1

PREFIX lde: <http://linkeddata.ec/resource/>

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

WITH <http://linkeddata.ec/graph/tutorial/example1>

INSERT { lde:myname foaf:name "boráclito" }

Data before:

Query:

6

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://linkeddata.ec/resource/myname> foaf:name "boráclito" .

Data after:

A simple SPARQL DELETE query

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://linkeddata.ec/resource/myname> foaf:name "boráclito" .

PREFIX lde: <http://linkeddata.ec/resource/>

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

WITH <http://linkeddata.ec/graph/tutorial/example1>

DELETE { lde:myname foaf:name "boráclito" }

Data before:

Query:

7

#graph http://linkeddata.ec/graph/tutorial/example1

Data after:

Another way to INSERT a triple

#graph http://linkeddata.ec/graph/tutorial/example1

PREFIX lde: <http://linkeddata.ec/resource/>

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

INSERT DATA {

GRAPH <http://linkeddata.ec/graph/tutorial/example1>

{<http://linkeddata.ec/resource/myname> foaf:name "boráclito" . }

}

Data before:

Query:

8

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://linkeddata.ec/resource/myname> foaf:name "boráclito" .

Data after:

Another way to DELETE a triple

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://linkeddata.ec/resource/myname> foaf:name "boráclito" .

PREFIX lde: <http://linkeddata.ec/resource/>

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

DELETE DATA {

GRAPH <http://linkeddata.ec/graph/tutorial/example1>

{<http://linkeddata.ec/resource/myname> foaf:name "boráclito" . }

}

Data before:

Query:

9

#graph http://linkeddata.ec/graph/tutorial/example1

Data after:

Inserting triples

#graph http://linkeddata.ec/graph/tutorial/example1

PREFIX lde: <http://linkeddata.ec/resource/>

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

INSERT DATA {

GRAPH <http://linkeddata.ec/graph/tutorial/example1>

{<http://linkeddata.ec/resource/team1> foaf:name "Ecuador" .

<http://linkeddata.ec/resource/team2> foaf:name "Bolivia" .

<http://linkeddata.ec/resource/team3> foaf:name "España" .

}

}

Data before:

Query:

10

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://linkeddata.ec/resource/team1> foaf:name "Ecuador" .

<http://linkeddata.ec/resource/team2> foaf:name "Bolivia" .

<http://linkeddata.ec/resource/team3> foaf:name "España" .

Data after:

DELETE with WHERE clause

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://linkeddata.ec/resource/team1> foaf:name "Ecuador" .

<http://linkeddata.ec/resource/team2> foaf:name "Bolivia" .

<http://linkeddata.ec/resource/team3> foaf:name "España" .

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

WITH <http://linkeddata.ec/graph/tutorial/example1>

DELETE

{ ?s foaf:name ?name .

}

WHERE {

?s foaf:name ?name .

FILTER(REGEX(?name,"E"))

}

Data before:

Query:

11

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://linkeddata.ec/resource/team2> foaf:name "Bolivia" .

Data after:

Renaming a particular object

12

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://linkeddata.ec/resource/team2> foaf:name "Bolivia" .

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

WITH <http://linkeddata.ec/graph/tutorial/example1>

DELETE { ?s foaf:name "Bolivia" }

INSERT { ?s foaf:name "Ecuador" }

WHERE

{

?s foaf:name "Bolivia"

}

Data before:

Query:

#graph http://linkeddata.ec/graph/tutorial/example1

@prefix foaf: <http://xmlns.com/foaf/0.1/> . <http://linkeddata.ec/resource/team2> foaf:name "Ecuador" .

Data after:

Inserting triples #graph http://linkeddata.ec/graph/tutorial/people

PREFIX lde: <http://linkeddata.ec/resource/>

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

INSERT DATA {

GRAPH <http://linkeddata.ec/graph/tutorial/people>

{ lde:victor a foaf:Person .

lde:victor foaf:name "Victor Saquicela" .

lde:victor foaf:mbox <mailto:[email protected]> .

lde:mauri a foaf:Person .

lde:mauri foaf:name "Mauricio Espinoza" .}

}

Data before:

Query:

13

#graph http://linkeddata.ec/graph/tutorial/people

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

@prefix lde: <http://linkeddata.ec/resource/> .

lde:victor a foaf:Person .

lde:victor foaf:name "Victor Saquicela" .

lde:victor foaf:mbox <mailto:[email protected]> .

lde:mauri a foaf:Person .

lde:mauri foaf:name "Mauricio Espinoza" .

Data after:

Copy triples of name and email from one named graph to another

#graph http://linkeddata.ec/graph/tutorial/addresses

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

INSERT

{ GRAPH <http://linkeddata.ec/graph/tutorial/addresses>

{

?person foaf:name ?name .

?person foaf:mbox ?email

} }

WHERE

{ GRAPH <http://linkeddata.ec/graph/tutorial/people>

{

?person foaf:name ?name .

OPTIONAL { ?person foaf:mbox ?email }

} }

Data before:

Query:

14

#graph http://linkeddata.ec/graph/tutorial/addresses

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

@prefix lde: <http://linkeddata.ec/resource/> .

lde:victor foaf:name "Victor Saquicela" .

lde:victor foaf:mbox <mailto:[email protected]> .

lde:mauri foaf:name "Mauricio Espinoza" .

Data after:

Some operations over graphs

LOAD

<http://terrazas.name/villazon/boris/data/TwitterAccounts.nt>

INTO GRAPH <http://linkeddata.ec/graph/data-test>

Load

15

CLEAR GRAPH <http://linkeddata.ec/graph/data-test>

Clear

ToC

• Update

• Federated queries

• Basic property paths

16

Federated Queries - Motivation

17

DBpedia

LinkedMDB

Query both datasets in an integrated way

What is exactly Federated SPARQL Query?

18 Image taken from Flick https://www.flickr.com/photos/fadirra/5915787567/

Federated SQL queries within DBMS?

19

• Not so difficult if we are using the same DBMS vendor - MySQL to MySQL

- Oracle to Oracle

- PostgreSQL to PostgreSQL

• If we are dealing with different DBMS vendor - Build wrappers

Federated SPARQL Query Syntax

20

SPARQL_ENDPOINT_URL_A

SELECT *

FROM <http://example.com/graph/mygraph>

WHERE {

?s ?q ?w .

SERVICE <SPARQL_ENDPOINT_URL_B> {

SELECT *

FROM <http://localhost:8890/isoco> {

?t ?q ?o .

}

}

}

SPARQL_ENDPOINT_URL_A

SERVICE <….> { SELECT * FROM …. WHERE { ….} }

SPARQL_ENDPOINT_URL_B

Results

Federated Queries examples

21

SPARQL endpoint 1 lab-1-1.ttl

SPARQL endpoint 2 lab-1-2.ttl

SPARQL endpoint 3 DBpedia

Federated query example 1 SELECT *

FROM <http://linkeddata.ec/graph/federado>

WHERE {

?s ?q ?w .

SERVICE <http://172.16.147.92:8890/sparql/> {

SELECT * FROM <http://localhost:8890/isoco2> {

?t ?q ?o .

}

}

}

Query:

22

Federated query example 2

SELECT *

FROM <http://linkeddata.ec/graph/federado>

WHERE {

?s ?q ?w .

SERVICE <http://192.168.1.100:8890/sparql/> {

SELECT * FROM <http://localhost:8890/isoco> {

?t ?q ?o .

}

}

}

Query:

23

Federated query example 3

SELECT *

FROM <http://linkeddata.ec/graph/federado>

WHERE {

?s ?q ?w .

SERVICE <http://192.168.1.101:8890/sparql/> {

SELECT * FROM <http://localhost:8890/isoco2> {

?t ?q ?o .

}

}

SERVICE <http://dbpedia.org/sparql/> {

SELECT * FROM <http://dbpedia.org>{

<http://dbpedia.org/resource/Cochabamba> ?h ?k .

}

}

}

Query:

24

Federated query example 4

SELECT *

FROM <http://linkeddata.ec/graph/federado>

WHERE {

<http://lab.isoco.net/resource/agonzalez>

<http://vocab.isoco.net/lab/birthPlace> ?c .

<http://lab.isoco.net/resource/agonzalez> foaf:currentProject ?p

SERVICE <http://172.16.147.144:8890/sparql> {

SELECT * FROM <http://localhost:8890/isoco2> {

<http://lab.isoco.net/resource/agonzalez> foaf:currentProject ?o .

}

}

SERVICE <http://dbpedia.org/sparql/> {

SELECT * FROM <http://dbpedia.org>{

?c ?h ?k .

}

}

}

Query:

25

Federated query example 5

SELECT *

FROM <http://linkeddata.ec/graph/federado>

WHERE {

?s <http://vocab.isoco.net/lab/birthPlace> ?c .

?s foaf:currentProject ?p

SERVICE <http://192.168.1.101:8890/sparql/> {

SELECT * FROM <http://localhost:8890/isoco2> {

?s foaf:currentProject ?o .

}

}

SERVICE <http://dbpedia.org/sparql/> {

SELECT * FROM <http://dbpedia.org>{

?c ?h ?k .

}

}

}

Query:

26

ToC

• Update

• Federated queries

• Basic property paths

27

Example 1

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

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

PREFIX vocab: <http://vocab.isoco.net/>

INSERT DATA {

GRAPH <http://data.isoco.net/graph/test-subclassof> {

vocab:Person rdf:type rdfs:Class .

vocab:Professor rdf:type rdfs:Class .

vocab:AssistantProfessor rdf:type rdfs:Class .

vocab:Professor rdfs:subClassOf vocab:Person .

vocab:AssistantProfessor rdfs:subClassOf vocab:Professor .

<http://data.isoco.net/people/Penny> rdf:type vocab:Professor .

<http://data.isoco.net/people/Leonard> rdf:type vocab:AssistantProfessor .

}

}

Query:

28

INSERT

Example 1

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

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

SELECT *

FROM <http://data.isoco.net/graph/test-subclassof>

WHERE {

<http://vocab.isoco.net/AssistantProfessor> rdfs:subClassOf+ ?s }

Query:

29

SUPERCLASSES

s

http://vocab.isoco.net/Professor

http://vocab.isoco.net/Person

Results:

Example 1

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

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

SELECT *

FROM <http://data.isoco.net/graph/test-subclassof>

WHERE {

?a rdf:type ?type .

?type rdfs:subClassOf* <http://vocab.isoco.net/Person> }

Query:

30

INSTANCES

a type

http://data.isoco.net/people/Leonard http://vocab.isoco.net/AssistantProfessor

http://data.isoco.net/people/Penny http://vocab.isoco.net/Professor

Results: