660 likes | 668 Views
Learn how to install Apache Jena Fuseki to set up your own SPARQL server and endpoint for querying knowledge bases. Fuseki provides SPARQL 1.1 protocols and integrates with TDB for robust RDF storage and query capabilities.
E N D
SPARQLPartII Jan Pettersen Nytun, UiA
First a small SPARQL example Jan Pettersen Nytun, UiA
[4]: Ask: SELECT ?who WHERE { :JamesDean :playedIn ?what . ?what :directedBy ?who . } Answer: :GeorgeStevens, :EliaKazan, :NicholasRay, :FredGuiol
Installing Your Own SPARQL Endpoint Jan Pettersen Nytun, UiA
Apache Jena Fuseki • SPARQL server / SPARQL endpoint enables users to query a knowledge base via the SPARQL language • Has a user interface for server monitoring and administration. • Provides the SPARQL 1.1 protocols for query and update.
TDB • Fuseki is tightly integrated with TDB to provide a robust, transactional persistent storage layer. • TDB is a component of Jena for RDF storage and query. It support the full range of Jena APIs. TDB can be used as a high performance RDF store on a single machine.
Download https://jena.apache.org/download/
Using YourSPARQL Endpoint Jan Pettersen Nytun, UiA
You may copy the text on next slide into a ttl-file (OWL in Turtle format) and upload it.
@prefix : <http://www.uia.no/film_ontology#> . @prefixowl: <http://www.w3.org/2002/07/owl#> . @prefixrdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefixxml: <http://www.w3.org/XML/1998/namespace> . @prefixxsd: <http://www.w3.org/2001/XMLSchema#> . @prefixrdfs: <http://www.w3.org/2000/01/rdf-schema#> . @base <http://www.uia.no/film_ontology> . <http://www.uia.no/film_ontology> rdf:typeowl:Ontology . :Film rdf:typeowl:Class . :Human rdf:typeowl:Class . :Man rdf:typeowl:Class; rdfs:subClassOf:Human . :Womanrdf:typeowl:Class; rdfs:subClassOf:Human . :playedInrdf:typeowl:ObjectProperty . :Giantrdf:type:Film , owl:NamedIndividual . :EastOfEdenrdf:type:Film , owl:NamedIndividual . :CarrollBakerrdf:type:Woman, owl:NamedIndividual; :playedIn :Giant . :ElizabethTaylorrdf:type:Woman, owl:NamedIndividual ; :playedIn :Giant . :JoVanFleetrdf:type:Woman, owl:NamedIndividual ; :playedIn :EastOfEden . :JulieHarrisrdf:type:Woman, owl:NamedIndividual ; :playedIn:EastOfEden . :MercedesMcCambridgerdf:type :Woman, owl:NamedIndividual ; :playedIn :Giant . :RockHudsonrdf:type:Man , owl:NamedIndividual ; :playedIn :Giant. :JamesDeanrdf:type:Man , owl:NamedIndividual ; :playedIn :EastOfEden , :Giant .
The postfix of the file must be correct (must be ttl if Turtle)
Find the women that played in the same movies as James Dean.
Given the following graph, find the women that played in the same movies as James Dean.
Prefixes prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#>
[4]: SELECT ?actress ?movie WHERE { :JamesDean :playedIn ?movie . ?actress :playedIn ?movie . ?actress rdf:type :Woman }
Defining Graphs in Fuseki Jan Pettersen Nytun, UiA
2.1.4 Specifying an RDF Dataset A SPARQL query is executed against an RDF Dataset. The RDF Dataset for a query may be specified either via the default-graph-uriand named-graph-uri parameters in the SPARQL Protocol or in the SPARQL query string using the FROM and FROM NAMED keywords. [https://www.w3.org/TR/sparql11-protocol/]:
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#> SELECT ?actress ?movie WHERE { GRAPH <http://localhost:3030/Testing/data/JPNGRAPHH> { :JamesDean :playedIn ?movie . ?actress :playedIn ?movie . ?actress rdf:type :Woman } }
The result contains two occurrences of JamesDean! prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#> SELECT ?actor WHERE { ?actor :playedIn ?movie . }
Ordering of the SPARQL statement triples • Semantically the order is insignificant. • In regard to efficiency: • Typically the queries are processed in top-to-bottom order… • Order triples in a query so that the first triples gives fewest matches, e.g., by have the triples with fewest new variables first.
New ontology for the next slides. You may copy the text on next slide into a ttl-file and upload it.
@prefix : <http://www.uia.no/janpettersennytun/ontologies/family#> . @prefixowl: <http://www.w3.org/2002/07/owl#> . @prefixrdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefixxml: <http://www.w3.org/XML/1998/namespace> . @prefixxsd: <http://www.w3.org/2001/XMLSchema#> . @prefixrdfs: <http://www.w3.org/2000/01/rdf-schema#> . @base <http://www.uia.no/janpettersennytun/ontologies/family> . <http://www.uia.no/janpettersennytun/ontologies/family> rdf:typeowl:Ontology . :hasBrotherrdf:typeowl:ObjectProperty . :hasParentrdf:typeowl:ObjectProperty . :hasSisterrdf:typeowl:ObjectProperty . :hasUnclerdf:typeowl:ObjectProperty . :birthdayrdf:typeowl:DatatypeProperty; rdfs:rangexsd:dateTime . :diedOnrdf:typeowl:DatatypeProperty; rdfs:rangexsd:dateTime . :hasNamerdf:typeowl:DatatypeProperty; rdfs:rangexsd:string . :CityManrdf:typeowl:Class; rdfs:subClassOf:Man . :Man rdf:typeowl:Class; rdfs:subClassOf :Person . :Person rdf:typeowl:Class . :Womanrdf:typeowl:Class; rdfs:subClassOf :Person . :Ester rdf:type :Woman , owl:NamedIndividual ; :hasName "Ester"^^xsd:string ; :hasBrother :Olav , :Sigmund . :Jan rdf:type :Man , owl:NamedIndividual ; :birthday "1957-12-16T04:30:00"^^xsd:dateTime ; :hasName "Jan"^^xsd:string ; :hasParent :Sigmund . :Kirsten rdf:type :Woman , owl:NamedIndividual ; :birthday "1956-05-12T14:00:00"^^xsd:dateTime ; :hasName "Kirsten"^^xsd:string ; :hasParent :Sigmund . :Olav rdf:type :Man , owl:NamedIndividual ; :hasName "Olav"^^xsd:string ; :hasSister :Ester ; :hasBrother :Sigmund . :Rolf rdf:type :Man , owl:NamedIndividual ; :hasName "Rolf"^^xsd:string . :Sigmund rdf:type :Man , owl:NamedIndividual ; :birthday "1933-08-21T15:00:00"^^xsd:dateTime ; :diedOn "1980/07/14T21:32:52"^^xsd:dateTime ; :hasName "Sigmund"^^xsd:string ; :hasBrother :Rolf . :Sigrund rdf:type :Woman , owl:NamedIndividual ; :birthday "1952-07-21T15:00:007"^^xsd:dateTime ; :hasName "Sigrund"^^xsd:string ; :hasParent :Sigmund .
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> SELECT ?parent WHERE { ?child :hasParent ?parent }
But first find all persons. PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?person WHERE { ?person rdf:type:Person } OBS! The result set is empty!!
Find all men are easy! PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?person WHERE { ?person rdf:type:Man} The result is all men:
Find all persons, i.e., find all members of Person and the subclasses of Person PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person } SPARQL 1.1 includes a transitive operator: *[4]: If we include a * after a property name, then the triple matches any number of chained occurrences of the same property.
Find all persons and their birthdays PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT ?person ?birthday WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . ?person :birthday ?birthday } (Persons without a birthday will not be found.)
Find all persons born after beginning of 1940 by using FILTER.