Jena Sparql for Mac 2202021 1 Jena SPARQL

  • Slides: 25
Download presentation
Jena Sparql (for Mac) 2/20/2021 1

Jena Sparql (for Mac) 2/20/2021 1

Jena SPARQL l SPARQL queries RDF graphs (a set of triples): l l l

Jena SPARQL l SPARQL queries RDF graphs (a set of triples): l l l RDF graphs – models (in Jena) RDF triples – statements (in Jena) It is the triples that SPARQL cares, not the serialization. l l 2/20/2021 The serialization is just a way to write the triples down Here we use Turtle 2

vc-db-1. rdf in Turtle @prefix v. Card: @prefix rdf: <http: //www. w 3. org/2001/vcard-rdf/3.

vc-db-1. rdf in Turtle @prefix v. Card: @prefix rdf: <http: //www. w 3. org/2001/vcard-rdf/3. 0#>. <http: //www. w 3. org/1999/02/22 -rdf-syntax-ns#>. <http: //somewhere/Matt. Jones/> v. Card: FN <http: //somewhere/Matt. Jones/> v. Card: N _: b 0 v. Card: Family "Jones". _: b 0 v. Card: Given "Matthew". "Matt Jones". _: b 0. <http: //somewhere/Rebecca. Smith/> v. Card: FN <http: //somewhere/Rebecca. Smith/> v. Card: N _: b 1 v. Card: Family "Smith". _: b 1 v. Card: Given "Rebecca". "Becky Smith". _: b 1. <http: //somewhere/John. Smith/> _: b 2 v. Card: Family "Smith". _: b 2 v. Card: Given "John". v. Card: FN v. Card: N "John Smith". _: b 2. <http: //somewhere/Sarah. Jones/> _: b 3 v. Card: Family "Jones". _: b 3 v. Card: Given "Sarah". v. Card: FN v. Card: N "Sarah Jones". _: b 3. 2/20/2021 3

ARQ l l ARQ is a query engine for Jena that supports the SPARQL

ARQ l l ARQ is a query engine for Jena that supports the SPARQL RDF Query language. ARQ Features: l l 2/20/2021 Multiple query languages l SPARQL (. rq – file extension) l RDQL (. rdql) l ARQ, the engine’s own language (. arq) Multiple query engines l General purpose engine l Remote access engines l Rewriter to SQL 4

Install ARQ l Set ARQROOT l export ARQROOT=“/Users/username/somewhere/Jena/ARQ-2. 2” ‼ Replace username with your

Install ARQ l Set ARQROOT l export ARQROOT=“/Users/username/somewhere/Jena/ARQ-2. 2” ‼ Replace username with your own username; replace somewhere with the file path where you save Jena folder (Jena folder is saved in Desktop in this screenshot) 2/20/2021 5

Query 1 l l l SPARQL file: “~/Desktop/Jena/ARQ-2. 2/bin/sparql” Data file: “~/Desktop/Jena/Tutorial/vc-db-1. rdf” Query

Query 1 l l l SPARQL file: “~/Desktop/Jena/ARQ-2. 2/bin/sparql” Data file: “~/Desktop/Jena/Tutorial/vc-db-1. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 1. rq” SELECT ? x WHERE { ? x <http: //www. w 3. org/2001/vcard-rdf/3. 0#FN> "John Smith" } l Execute query q 1. rq l ‼ ARQ-2. 2/bin/sparql --data=Tutorial/vc-db-1. rdf --query=Tutorial/arq/q 1. rq The working directory in all the examples is “~/Desktop/Jena”. If you are not in the same directory, you have to modify the path of the SPARQL file, the data file, and the query file. 2/20/2021 6

Query 2 l l Data file: “~/Desktop/Jena/Tutorial/vc-db-1. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 2. rq” PREFIX

Query 2 l l Data file: “~/Desktop/Jena/Tutorial/vc-db-1. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 2. rq” PREFIX vcard: <http: //www. w 3. org/2001/vcard-rdf/3. 0#> SELECT ? y ? given. Name WHERE { ? y vcard: Family "Smith". ? y vcard: Given ? given. Name. } 2/20/2021 7

Query 3 - Filter l l Data file: “~/Desktop/Jena/Tutorial/vc-db-1. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 3.

Query 3 - Filter l l Data file: “~/Desktop/Jena/Tutorial/vc-db-1. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 3. rq” PREFIX vcard: <http: //www. w 3. org/2001/vcard-rdf/3. 0#> SELECT ? g WHERE { ? y vcard: Given ? g. FILTER regex(? g, "r", "i") } 2/20/2021 8

Query 4 - OPTIONAL l l Data file: “~/Desktop/Jena/Tutorial/vc-db-2. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 4.

Query 4 - OPTIONAL l l Data file: “~/Desktop/Jena/Tutorial/vc-db-2. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 4. rq” PREFIX info: PREFIX vcard: <http: //somewhere/people. Info#> <http: //www. w 3. org/2001/vcard-rdf/3. 0#> SELECT ? name ? age WHERE { ? person vcard: FN ? name. OPTIONAL { ? person info: age ? age } 2/20/2021 } 9

Query 5 l l Data file: “~/Desktop/Jena/Tutorial/vc-db-2. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 5. rq” PREFIX

Query 5 l l Data file: “~/Desktop/Jena/Tutorial/vc-db-2. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 5. rq” PREFIX info: PREFIX vcard: <http: //somewhere/people. Info#> <http: //www. w 3. org/2001/vcard-rdf/3. 0#> SELECT ? name ? age WHERE { ? person vcard: FN ? name. ? person info: age ? age. } 2/20/2021 10

Query 6 – Optional and Filter l l Data file: “~/Desktop/Jena/Tutorial/vc-db-2. rdf” Query file:

Query 6 – Optional and Filter l l Data file: “~/Desktop/Jena/Tutorial/vc-db-2. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 6. rq” PREFIX info: PREFIX vcard: <http: //somewhere/people. Info#> <http: //www. w 3. org/2001/vcard-rdf/3. 0#> SELECT ? name ? age WHERE { ? person vcard: FN ? name. OPTIONAL { ? person info: age ? age. FILTER ( ? age > “ 24” ) } } 2/20/2021 11

Query 7 - Union l Data: ~/Desktop/Jena/Tutorial/name. rdf @prefix foaf: <http: //xmlns. com/foaf/0. 1/>.

Query 7 - Union l Data: ~/Desktop/Jena/Tutorial/name. rdf @prefix foaf: <http: //xmlns. com/foaf/0. 1/>. @prefix vcard: <http: //www. w 3. org/2001/vcard-rdf/3. 0#>. _: a foaf: name "Matt Jones". _: b foaf: name "Sarah Jones". _: c vcard: FN "Becky Smith". _: d vcard: FN "John Smith". 2/20/2021 12

Query 7 – Union l l Data file: “~/Desktop/Jena/Tutorial/name. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 7.

Query 7 – Union l l Data file: “~/Desktop/Jena/Tutorial/name. rdf” Query file: “~/Desktop/Jena/Tutorial/arq/q 7. rq” PREFIX info: PREFIX vcard: <http: //somewhere/people. Info#> <http: //www. w 3. org/2001/vcard-rdf/3. 0#> SELECT ? name ? age WHERE { ? person vcard: FN ? name. OPTIONAL { ? person info: age ? age. FILTER ( ? age > “ 24” ) } } 2/20/2021 13

Query 8 – Named Graphs @prefix dc: <http: //purl. org/dc/elements/1. 1/>. @prefix xsd: <http:

Query 8 – Named Graphs @prefix dc: <http: //purl. org/dc/elements/1. 1/>. @prefix xsd: <http: //www. w 3. org/2001/XMLSchema#>. Default graph: ds-dft. ttl <ds-ng-1. ttl> dc: date "2005 -07 -14 T 03: 18: 56+0100"^^xsd: date. Time. <ds-ng-2. ttl> dc: date "2005 -09 -22 T 05: 53: 05+0100"^^xsd: date. Time. @prefix dc: <http: //purl. org/dc/elements/1. 1/>. Named graph: ds-ng-1. ttl [] dc: title "Harry Potter and the Philospher's Stone". [] dc: title "Harry Potter and the Chamber of Secrets". @prefix dc: <http: //purl. org/dc/elements/1. 1/>. Named graph: ds-ng-2. ttl [] dc: title "Harry Potter and the Sorcerer's Stone". [] dc: title "Harry Potter and the Chamber of Secrets". 2/20/2021 14

Query 8 – Named Graphs l l Data file: ~/Desktop/Jena/Tutorial Query file: ~/Desktop/Jena/Tutorial/arq/q 8.

Query 8 – Named Graphs l l Data file: ~/Desktop/Jena/Tutorial Query file: ~/Desktop/Jena/Tutorial/arq/q 8. rq PREFIX xsd: <http: //www. w 3. org/2001/XMLSchema#> PREFIX dc: <http: //purl. org/dc/elements/1. 1/> PREFIX : <. > SELECT ? title ? graph FROM <ds-dft. ttl> FROM NAMED <ds-ng-1. ttl> FROM NAMED <ds-ng-2. ttl> WHERE { GRAPH ? graph { ? x dc: title ? title. } } 2/20/2021 15

Query 8 – Named Graphs 2/20/2021 16

Query 8 – Named Graphs 2/20/2021 16

Executing SPARQL queries via Jena API l l SPARQL queries are created and executed

Executing SPARQL queries via Jena API l l SPARQL queries are created and executed with Jena via classes in the com. hpl. jena. query package. Using Query. Factory is the simplest approach. l Create() methods are used to read a textual query from a file or from a String. l Create() returns a query object with a parsed query Create an instance of Query. Execution to perform a different type of query l Call Query. Execution. Factory. create(query, model) l Because the data for the query is provided programmatically, the query does not need a FROM clause. Result. Set allows you to iterate over Query. Solution providing access to each bound variable’s value. 2/20/2021 17

Bloggers. rdf 2/20/2021 18

Bloggers. rdf 2/20/2021 18

Query bloggers l l Data file: ~/Desktop/Jena/Tutorial/arq/bloggers. rdf Query file: ~/Desktop/Jena/Tutorial/arq/bloggers 1. rq PREFIX

Query bloggers l l Data file: ~/Desktop/Jena/Tutorial/arq/bloggers. rdf Query file: ~/Desktop/Jena/Tutorial/arq/bloggers 1. rq PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> SELECT ? url FROM <bloggers. rdf> WHERE { ? contributor foaf: name "Jon Foobar". ? contributor foaf: weblog ? url. } 2/20/2021 19

import import com. hpl. jena. rdf. model. *; com. hpl. jena. util. File. Manager;

import import com. hpl. jena. rdf. model. *; com. hpl. jena. util. File. Manager; com. hpl. jena. query. * ; com. hpl. jena. query. ARQ; com. hpl. jena. sparql. *; import java. io. *; public class Bloggers extends Object { static final String input. File. Name = "bloggers. rdf"; public static void main (String args[]) { // Create an empty in-memory model Model model = Model. Factory. create. Default. Model(); // use the File. Manager to open the bloggers RDF graph from the filesystem Input. Stream in = File. Manager. get(). open(input. File. Name); if (in == null) { throw new Illegal. Argument. Exception( "File: " + input. File. Name + " not found"); } // read the RDF/XML file model. read( in, "" ); Bloggers. java 2/20/2021 20

// Create a new query String query. String = "PREFIX foaf: <http: //xmlns. com/foaf/0.

// Create a new query String query. String = "PREFIX foaf: <http: //xmlns. com/foaf/0. 1/> " + "SELECT ? url " + "WHERE {" + " ? contributor foaf: name "Jon Foobar". " + " ? contributor foaf: weblog ? url. " + " }"; Query query = Query. Factory. create(query. String); // Execute the query and obtain results Query. Execution qe = Query. Execution. Factory. create(query, model); Result. Set results = qe. exec. Select(); // Output query results Result. Set. Formatter. out(System. out, results, query); // Important - free up resources used running the query qe. close(); } } Bloggers. java 2/20/2021 21

Executing SPARQL queries via Jena API l Set up CLASSPATH for Jena API l

Executing SPARQL queries via Jena API l Set up CLASSPATH for Jena API l l ‼ CLASSPATH=“. : /Users/usersname/somewhere/Jena-2. 5. 5/lib/*” export CLASSPATH Replace username with your own username; replace somewhere with the file path where you save Jena folder (Jena folder is saved in Desktop in this screenshot) 2/20/2021 22

Executing SPARQL queries via Jena API l l Store bloggers. java in ~DesktopJenaTutorialarq Compile

Executing SPARQL queries via Jena API l l Store bloggers. java in ~DesktopJenaTutorialarq Compile Run The working directory in this examples is “~/Desktop/Jena/Tutorial/arq”. If you are not in the same directory, you have to modify the file path. 2/20/2021 23

Executing SPARQL queries via Jena API PREFIX SELECT FROM WHERE ? x } l

Executing SPARQL queries via Jena API PREFIX SELECT FROM WHERE ? x } l l foaf: <http: //xmlns. com/foaf/0. 1/> ? name ? url <bloggers. rdf> { foaf: name ? name. foaf: weblog ? url. Bloggers 1. java Store bloggers 1. java in ~/Desktop/Jena/Tutorial/arq Compile and run 2/20/2021 24

Summary l l Practicing and mastering all the Jena Sparql tutorials on your own.

Summary l l Practicing and mastering all the Jena Sparql tutorials on your own. Be able to create similar tutorials using your own examples. 2/20/2021 25