27
Down and Dirty with RDF and SPARQL (Part 2) Erich Bremer Stony Brook Semantic Web Meetup April 15, 2015

Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

Down and Dirty with RDF and SPARQL (Part 2)

Erich BremerStony Brook Semantic Web Meetup

April 15, 2015

Page 2: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

SPARQLselect, construct, ask, and describe

Page 3: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

Directors and actorslinked through movies

http://dbpedia.org/sparql

Page 4: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

Star Wars

Harrison Ford

George Lucas

Ender’s Game

Gavin Hood X-Men Origins: Wolverine

Hugh Jackman

Rise of the Guardians

Peter Ramsey

Eye in the Sky

Alan Rickman

Harry Potter

Page 5: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman
Page 6: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

The Anatomy of a SPARQL query

<prefixes><command> <results clause> where {<triple pattern>}<optional query modifier>

“where” is optional

Page 7: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

select SPARQL queryselect * where {

?film <http://dbpedia.org/ontology/director> ?director .?film a <http://dbpedia.org/ontology/Film> .?film <http://dbpedia.org/ontology/starring> ?actor

} limit 100

Page 8: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

results

Page 9: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix dbp: <http://dbpedia.org/ontology/>select * where {

?film dbp:director ?director .?film a dbp:Film .?film dbp:starring ?actor

}limit 100

adding prefix name spaces

Page 10: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix dbp: <http://dbpedia.org/ontology/>select * where {?film dbp:director ?director;

a dbp:Film;dbp:starring ?actor

} limit 100

using predicate lists

Page 11: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix : <http://dbpedia.org/ontology/>select * where {?film :director ?director;

a :Film;:starring ?actor

} limit 100

using default name space “:”

Page 12: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix : <http://dbpedia.org/ontology/>select ?film ?director ?actor where {?film :director ?director;

a :Film;:starring ?actor

} limit 100

specifying variables in select clause

Page 13: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix : <http://dbpedia.org/ontology/>prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>select ?title ?film ?director ?actor where {?film :director ?director;

a :Film;:starring ?actor;rdfs:label ?title

} limit 100

adding additional predicates

Page 14: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix : <http://dbpedia.org/ontology/>prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>select ?title ?film ?director ?actor where {?film :director ?director;

a :Film;:starring ?actor .optional {?film rdfs:label ?title}

} limit 100

optional clause

Page 15: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix : <http://dbpedia.org/ontology/>prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>select ?title ?film ?director ?actor where {?film :director ?director;

a :Film;:starring ?actor;rdfs:label ?titlefilter(lang(?title)="en")

} limit 100

adding a language filter

Page 16: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

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

select ?title ?film ?director ?actor where {?film :director ?director;

a :Film;:starring ?actor;rdfs:label ?titlefilter(lang(?title)="en")

}order by asc(?title)limit 100

ordering the results

Page 17: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

results

Page 18: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix : <http://dbpedia.org/ontology/>prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>construct {?film rdfs:label ?title} where {?film :director ?director;

a :Film;:starring ?actor;rdfs:label ?titlefilter(lang(?title)="en")

} limit 10

construct SPARQL query

Page 19: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

results

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .<http://dbpedia.org/resource/Alraune_(1918_film)> rdfs:label "Alraune (1918 film)"@en .<http://dbpedia.org/resource/Alraune_(1928_film)> rdfs:label "Alraune (1928 film)"@en .<http://dbpedia.org/resource/April_Love_(film)> rdfs:label "April Love (film)"@en .<http://dbpedia.org/resource/Ascendancy_(film)> rdfs:label "Ascendancy (film)"@en .<http://dbpedia.org/resource/Chervona_Ruta_(film)> rdfs:label "Chervona Ruta (film)"@en .

Page 20: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

prefix : <http://dbpedia.org/ontology/>prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>ask where {?film :director ?director;

a :Film;:starring ?actor;rdfs:label ?titlefilter(lang(?title)="en")

}

ask SPARQL query

Does the pattern exist true or false?

Page 21: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

describe <http://dbpedia.org/resource/Star_Wars_(film)>

describe SPARQL query<http://dbpedia.org/resource/Star_Wars_(film)>

select * where {<http://dbpedia.org/resource/Star_Wars_(film)> ?p ?o

}

Kind of a short hand for:

Page 22: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

“A free and open source Java framework for building Semantic Web and Linked Data applications.”

https://jena.apache.org/

Jena – Core RDF API Library

ARQ – SPARQL engineTDB – Native Quad/triple storeFuseki – implements http SPARQL protocol on TDB and provide SPARQL Endpoint

Ontology API – Web Ontology Language (OWL)Inference API – RDF reasoning

Apache Jena<http://dbpedia.org/resource/Star_Wars_(film)>

Page 23: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

ParameterizedSparqlString pss = new ParameterizedSparqlString("prefix : <http://dbpedia.org/ontology/>\n " + "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +"select ?title ?film ?director ?actor where {\n" +

"?film :director ?director; a :Film; :starring ?actor; rdfs:label ?title\n" +"filter(lang(?title)=\"en\") order by asc(?title) limit 100");

QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", pss.asQuery(), "http://dbpedia.org");

ResultSet rs = qe.execSelect();while (rs.hasNext()) {

QuerySolution qs = rs.next();

String film = qs.getResource("film").toString();String title = qs.getLiteral("title").toString();System.out.println(film+" "+title);

}

Apache Jena – Select Example<http://dbpedia.org/resource/Star_Wars_(film)>

Page 24: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

ParameterizedSparqlString pss = new ParameterizedSparqlString("prefix : <http://dbpedia.org/ontology/>\n" +"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +

"construct {?film rdfs:label ?title} where {\n" +"?film :director ?director;\n" +" a :Film;\n" +

" :starring ?actor;\n" +" rdfs:label ?title\n" +" filter(lang(?title)=\"en\")} limit 100");

QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", pss.asQuery(), "http://dbpedia.org");

Model m = qe.execConstruct();System.out.println("Triples loaded : "+m.size());

m.write(System.out,null,"TTL");

Apache Jena – Construct Example<http://dbpedia.org/resource/Star_Wars_(film)>

Page 25: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

ParameterizedSparqlString pss = new ParameterizedSparqlString("prefix : <http://dbpedia.org/ontology/>\n" +"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +

"ask where {\n" +"?film :director ?director;\n" +" a :Film;\n" +

" :starring ?actor;\n" +" rdfs:label ?title\n" +" filter(lang(?title)=\"en\")\n" +

"}");QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", pss.asQuery(),

"http://dbpedia.org");boolean answer = qe.execAsk();

System.out.println(answer);

Apache Jena – Ask Example<http://dbpedia.org/resource/Star_Wars_(film)>

Page 26: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

ParameterizedSparqlString pss = new ParameterizedSparqlString("describe <http://dbpedia.org>");QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", pss.asQuery(),

"http://dbpedia.org");

Model m = qe.execDescribe();System.out.println("Triples loaded : "+m.size());m.write(System.out,null,"TTL");

Apache Jena – Ask Example<http://dbpedia.org/resource/Star_Wars_(film)>

Page 27: Down and Dirty with RDF and SPARQL (Part 2) and Dirty with RDF and SPA… · Star Wars. Harrison Ford. George Lucas. Ender’s Game. Gavin Hood. X-Men Origins: Wolverine. Hugh Jackman

Any questions?

To be continued next month…Part 3