swt lecture session 4 - sesame

43
+ Sesame Mariano Rodriguez-Muro, Free University of Bozen-Bolzano

Upload: mariano-rodriguez

Post on 13-Jan-2015

527 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: SWT Lecture Session 4 - Sesame

+

Sesame

Mariano Rodriguez-Muro, Free University of Bozen-Bolzano

Page 2: SWT Lecture Session 4 - Sesame

+Disclaimer

License This work is licensed under a

Creative Commons Attribution-Share Alike 3.0 License (http://creativecommons.org/licenses/by-sa/3.0/)

Material for these slides has been taken from Programming the Semantic Web (Chapter 8) Sesame’s documentation

Page 3: SWT Lecture Session 4 - Sesame

+Reading material

Programming the Semantic Web Chapter 8

Sesame user guide http://www.openrdf.org/doc/sesame2/users/

See also Jetty 8http://download.eclipse.org/jetty/stable-8/dist/

Page 4: SWT Lecture Session 4 - Sesame

+

Overview

Page 5: SWT Lecture Session 4 - Sesame

+Overview

Sesame History Overview Repository API

Creating a Repository Repository connections

Sesame Console Sesame workbench

Page 6: SWT Lecture Session 4 - Sesame

+Overview and History

Open source Java for storage and querying RDF

RDF inference

RDF IO (all file formats)

JDBC-like user API

RESTful HTTP interface

SPARQL Protocol support

Easy learning curve, great management features

By Aduna for the On-To-Knowledge EU project

Now developed by Nlnet foundation, Ontotext and community volunteers

Available at www.openrdf.org

Page 7: SWT Lecture Session 4 - Sesame

+Sesame components

RDF Model: contains all basic entities

Rio: parsers and writers

Sail: low level API for RDF stores and inferencers (abstraction)

Repository API: high level API with developer-methods

HTTP Server: Java Servlets to access Sesame repos

HTTPClient: Abstraction layer to access HTTP Servers

Page 8: SWT Lecture Session 4 - Sesame

+

Repository API

Page 9: SWT Lecture Session 4 - Sesame

+Repository API

Developer-focused API

In contrast with Jena, in Sesame RDF models are not handled by the user (normally), instead we use Repositories.

Vendors provide triple stores as Repository implementations

Sesame provides the following repository implementations: Main memory Native RDF repository Remote repository (HTTP proxy)

To use Sesame repositories, these must be stacked in Sails, i.e., stacks of layered behavior

Page 10: SWT Lecture Session 4 - Sesame

+Creating a repository

An in-memory repo

An in-memory repo with persistance

Repository myRepository = new SailRepository(new MemoryStore());myRepository.initialize();

File dataDir = new File("c:\\temp\\myRepository\\");Repository myRepository = new SailRepository( new MemoryStore(dataDir) );myRepository.initialize();

Page 11: SWT Lecture Session 4 - Sesame

+Creating a repository

a native RDF repository

a native RDF repository with custom indexes

File dataDir = new File("/path/to/datadir/");Repository myRepository = new SailRepository(new NativeStore(dataDir));myRepository.initialize();

File dataDir = new File("/path/to/datadir/");String indexes = "spoc,posc,cosp";Repository myRepository = new SailRepository(new NativeStore(dataDir, indexes));myRepository.initialize();

Page 12: SWT Lecture Session 4 - Sesame

+Creating a repository

a remote Repository

String sesameServer = "http://example.org/sesame2";String repositoryID = "example-db";

Repository myRepository = new HTTPRepository(sesameServer, repositoryID);myRepository.initialize();

Page 13: SWT Lecture Session 4 - Sesame

+Using a repository: RepositoryConnection

JDBC like-interface

Operations add triples by file, URI,

Statement query using SPARQL

SELECT, ASK or Construct queries

create, retrieve, remove individual statements

prepared queries

Transaction support commit(), rollback()

Page 14: SWT Lecture Session 4 - Sesame

+Adding to a repository

File file = new File("/path/to/example.rdf");String baseURI = "http://example.org/example/local";

try { RepositoryConnection con = myRepository.getConnection(); try { con.add(file, baseURI, RDFFormat.RDFXML);

URL url = new URL("http://example.org/example/remote"); con.add(url, url.toString(), RDFFormat.RDFXML); } finally { con.close(); }}catch (…)

Page 15: SWT Lecture Session 4 - Sesame

+Querying a repository (tuple)

RepositoryConnection con = myRepository.getConnection(); try { String queryString = “SELECT …. “;TupleQuery tupleQuery = con.prepareTupleQuery(SPARQL, queryString); TupleQueryResult result = tupleQuery.evaluate(); try { .... // do something with the result } finally { result.close(); } } finally { con.close(); }

Page 16: SWT Lecture Session 4 - Sesame

+Tuple queries (cont)

while (result.hasNext()) { BindingSet bindingSet = result.next(); Value valueOfX = bindingSet.getValue("x"); Value valueOfY = bindingSet.getValue("y");

// do something interesting with the values here...}

List<String> bindingNames = result.getBindingNames();while (result.hasNext()) { BindingSet bindingSet = result.next(); Value firstValue = bindingSet.getValue(bindingNames.get(0)); Value secondValue = bindingSet.getValue(bindingNames.get(1));

// do something interesting with the values here...}

Page 17: SWT Lecture Session 4 - Sesame

+TupleQueryResultHandlerFileOutputStream out = new FileOutputStream("/path/to/result.srx");try { SPARQLResultsXMLWriter sparqlWriter = new SPARQLResultsXMLWriter(out);

RepositoryConnection con = myRepository.getConnection(); try { String queryString = "SELECT * FROM {x} p {y}"; TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SERQL, queryString); tupleQuery.evaluate(sparqlWriter); } finally { con.close(); }}finally { out.close();}

Page 18: SWT Lecture Session 4 - Sesame

+Evaluating Graph queries

GraphQueryResult graphResult = con.prepareGraphQuery( QueryLanguage.SPARQL, "CONSTRUCT ….").evaluate();

while (graphResult.hasNext()) { Statement st = graphResult.next(); // ... do something with the resulting statement here.}

Page 19: SWT Lecture Session 4 - Sesame

+RDF Handler

Equivalent for TupleQueryResultHandler > RDFHandler

Examples: RDFXMLWriter, TurtleWriter, etc.

TurtleWriter turtleWriter = new TurtleWriter(System.out);

con.prepareGraphQuery(QueryLanguage.SPARQL, "CONSTRUCT …").evaluate(turtleWriter);

Page 20: SWT Lecture Session 4 - Sesame

+Adding individual statements

ValueFactory f = myRepository.getValueFactory();

URI alice = f.createURI("http://example.org/people/alice");URI bob = f.createURI("http://example.org/people/bob");URI name = f.createURI("http://example.org/ontology/name");URI person = f.createURI("http://example.org/ontology/Person");Literal bobsName = f.createLiteral("Bob");Literal alicesName = f.createLiteral("Alice");

RepositoryConnection con = myRepository.getConnection();

con.add(alice, RDF.TYPE, person); con.add(alice, name, alicesName);

con.add(bob, RDF.TYPE, person); con.add(bob, name, bobsName);

Page 21: SWT Lecture Session 4 - Sesame

+Retrieving

RepositoryResult<Statement> statements = con.getStatements(alice, null, null, true);

try { while (statements.hasNext()) { Statement st = statements.next();

... // do something with the statement }}finally { statements.close(); // make sure the result object is closed properly}

Page 22: SWT Lecture Session 4 - Sesame

+Deleting statements

a single statement

many statements

con.remove(alice, name, alicesName);

con.remove(alice, null, null);

Page 23: SWT Lecture Session 4 - Sesame

+Iterators and statements

Sesame’s API offer many calls compatible for iterators to facilitate “batch” manipulation

// Retrieve all statements about Alice and put them in a listRepositoryResult<Statement> statements = con.getStatements(alice, null, null, true));List<Statement> aboutAlice = Iterations.addAll(statements, new ArrayList<Statement>());

// Then, remove them from the repositorycon.remove(aboutAlice);

con.remove(con.getStatements(alice, null, null, true));

Page 24: SWT Lecture Session 4 - Sesame

+Named graphs

Named graphs are natively supported by Sesame

Named graphs are called “Context” in Sesame

String location = "http://example.org/example/example.rdf";String baseURI = location;URL url = new URL(location);URI context = f.createURI(location);

con.add(url, baseURI, RDFFormat.RDFXML, context);

Page 25: SWT Lecture Session 4 - Sesame

+Transactions

SQL-like transactions

Treat a “block” of operations as a single update

Failures can be “rolled back”

Page 26: SWT Lecture Session 4 - Sesame

+TransactionsFile inputFile1 = new File("/path/to/example1.rdf");String baseURI1 = "http://example.org/example1/";

File inputFile2 = new File("/path/to/example2.rdf");String baseURI2 = "http://example.org/example2/";

RepositoryConnection con = myRepository.getConnection();try { con.setAutoCommit(false);

con.add(inputFile1, baseURI1, RDFFormat.RDFXML);

con.add(inputFile2, baseURI2, RDFFormat.RDFXML);

con.commit();}catch (RepositoryException e) { con.rollback(); }finally { con.close(); }

Page 27: SWT Lecture Session 4 - Sesame

+

Sesame Console

Page 28: SWT Lecture Session 4 - Sesame

+Sesame console

command-line application to interact with Sesame

Easy way to create and manage repositories

Create in the console, use from Java

Possible actions: Creating repositories Load/Unload data from

Files/URIs/SPARQL Update Query using SPARQL Querying/Managing

remote Sesame repositories

> sh bin/console.shConnected to default data directory

Commands end with '.' at the end of a lineType 'help.' for help> help.

Page 29: SWT Lecture Session 4 - Sesame

+Repository types

memory,memory-rdfsa memory based RDF repository (optionaly with RDFS inference)

native, native-rdfsa repository that uses on-disk data structure (optional RDFS inference)

pgsql, mysqla repository that stores data in a PostgreSQL (mysql) database

remote -- a repository that serves as a proxy for a

repository on a Sesame Server

Please specify values for the following variables:Repository ID [native]: myRepoRepository title [Native store]: My repositoryTriple indexes [spoc,posc]: Repository created> show repositories.> +----------> |SYSTEM> |myRepo(”My repository")> +----------

Page 30: SWT Lecture Session 4 - Sesame

+

Sesame WorkbenchRepository Manager and SPARQL end-points with Sesame

Page 31: SWT Lecture Session 4 - Sesame

+Sesame workbench

SPARQL Protocol implementation (sparql-endpoint) for Sesame repositories

Web based management console for Sesame repositories

Web based query interface for repositories.

Page 32: SWT Lecture Session 4 - Sesame

+Setup

Requires a JSP server, e.g., Tomcat or Jetty 8

Drop the 2 .war files from the /war folder of the sesame .zip into your webapps folder

Start the JSP server If you are using Jetty, do:

jetty.sh/bat start

in the command line

Page 33: SWT Lecture Session 4 - Sesame
Page 34: SWT Lecture Session 4 - Sesame
Page 35: SWT Lecture Session 4 - Sesame
Page 36: SWT Lecture Session 4 - Sesame
Page 37: SWT Lecture Session 4 - Sesame
Page 38: SWT Lecture Session 4 - Sesame
Page 39: SWT Lecture Session 4 - Sesame
Page 40: SWT Lecture Session 4 - Sesame
Page 41: SWT Lecture Session 4 - Sesame
Page 42: SWT Lecture Session 4 - Sesame
Page 43: SWT Lecture Session 4 - Sesame