down and dirty with rdf and sparql (part 2) and dirty with rdf and spa… · star wars. harrison...
TRANSCRIPT
Down and Dirty with RDF and SPARQL (Part 2)
Erich BremerStony Brook Semantic Web Meetup
April 15, 2015
SPARQLselect, construct, ask, and describe
Directors and actorslinked through movies
http://dbpedia.org/sparql
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
The Anatomy of a SPARQL query
<prefixes><command> <results clause> where {<triple pattern>}<optional query modifier>
“where” is optional
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
results
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
prefix dbp: <http://dbpedia.org/ontology/>select * where {?film dbp:director ?director;
a dbp:Film;dbp:starring ?actor
} limit 100
using predicate lists
prefix : <http://dbpedia.org/ontology/>select * where {?film :director ?director;
a :Film;:starring ?actor
} limit 100
using default name space “:”
prefix : <http://dbpedia.org/ontology/>select ?film ?director ?actor where {?film :director ?director;
a :Film;:starring ?actor
} limit 100
specifying variables in select clause
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
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
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
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
results
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
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 .
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?
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:
“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)>
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)>
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)>
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)>
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)>
Any questions?
To be continued next month…Part 3