WebTech Tutorial Querying DBPedia

41
Tutorial: Querying DBpedia Web Technology 2ID60 14 November 2013 Dr. Katrien Verbert Dr. ir. Natasha Stash Dr. George Fletcher

description

 

Transcript of WebTech Tutorial Querying DBPedia

Page 1: WebTech Tutorial Querying DBPedia

Tutorial: Querying DBpedia Web Technology 2ID60 14 November 2013

Dr. Katrien Verbert Dr. ir. Natasha Stash Dr. George Fletcher

Page 2: WebTech Tutorial Querying DBPedia

Overview

•  Introduction to Jena •  Setting up the environment •  Querying DBpedia •  Other APIs •  PHP example

Page 3: WebTech Tutorial Querying DBPedia

Jena

•  Jena is a Java framework for the creation of applications for the Semantic Web

•  Provides interfaces and classes for the creation and manipulation of RDF repositories

Page 4: WebTech Tutorial Querying DBPedia

RDF concepts

Page 5: WebTech Tutorial Querying DBPedia

Capabilities of Jena

•  RDF API •  Reading and writing in RDF/XML, N-Triples •  In-memory and persistent storage •  SPARQL query engine

Page 6: WebTech Tutorial Querying DBPedia

RDF concepts

•  The Jena RDF API contains classes and interfaces for every important aspect of the RDF specification

•  They can be used in order to construct RDF graphs from scratch, or edit existent graphs

•  These classes/interfaces reside in the com.hp.hpl.jena.rdf.model package

•  In Jena, the Model interface is used to represent RDF graphs

•  Through Model, statements can be obtained/ created/ removed etc

Page 7: WebTech Tutorial Querying DBPedia

RDF concepts

// Create an empty model Model model = ModelFactory.createDefaultModel(); String ns = new String("http://www.example.com/example#"); // Create two Resources Resource john = model.createResource(ns + "John"); Resource jane = model.createResource(ns + "Jane"); // Create the 'hasBrother' Property declaration Property hasBrother = model.createProperty(ns, "hasBrother"); // Associate jane to john through 'hasBrother' jane.addProperty(hasBrother, john); // Create the 'hasSister' Property declaration Property hasSister = model.createProperty(ns, "hasSister"); // Associate john and jane through 'hasSister' with a Statement Statement sisterStmt = model.createStatement(john, hasSister, jane); model.add(sisterStmt);

Page 8: WebTech Tutorial Querying DBPedia

SPARQL query processing

•  Jena uses the ARQ engine for the processing of SPARQL queries •  The ARQ API classes are found in com.hp.hpl.jena.query

•  Basic classes in ARQ: •  Query: Represents a single SPARQL query. •  Dataset: The knowledge base on which queries are executed

(Equivalent to RDF Models) •  QueryFactory: Can be used to generate Query objects from

SPARQL strings •  QueryExecution: Provides methods for the execution of queries •  ResultSet: Contains the results obtained from an executed query •  QuerySolution: Represents a row of query results.

•  If there are many answers to a query, a ResultSet is returned after the query is executed. The ResultSet contains many QuerySolutions

Page 9: WebTech Tutorial Querying DBPedia

SPARQL query processing

// Prepare query string String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "PREFIX : <http://www.example.com/onto1#>\n" + "SELECT ?married ?spouse WHERE {" + "?married rdf:type :MarriedPerson.\n" + "?married :hasSpouse ?spouse." + "}"; // Use the ontology model to create a Dataset object // Note: If no reasoner has been attached to the model, no results // will be returned (MarriedPerson has no asserted instances) Dataset dataset = DatasetFactory.create(ontModel); // Parse query string and create Query object Query q = QueryFactory.create(queryString); // Execute query and obtain result set QueryExecution qexec = QueryExecutionFactory.create(q, dataset); ResultSet resultSet = qexec.execSelect();

Page 10: WebTech Tutorial Querying DBPedia

SPARQL query processing

// Print results while(resultSet.hasNext()) {

// Each row contains two fields: ‘married’ and ‘spouse’, // as defined in the query string QuerySolution row = (QuerySolution)resultSet.next(); RDFNode nextMarried = row.get("married"); System.out.print(nextMarried.toString()); System.out.print(" is married to "); RDFNode nextSpouse = row.get("spouse"); System.out.println(nextSpouse.toString());

}

Page 11: WebTech Tutorial Querying DBPedia

ARQ Application API

http://jena.apache.org/documentation/query/app_api.html

Page 12: WebTech Tutorial Querying DBPedia

Overview

•  Introduction to Jena •  Setting up the environment •  Querying Dbpedia •  Other APIs

Page 13: WebTech Tutorial Querying DBPedia

Setting up the environment

Download Netbeans Java EE version: https://netbeans.org/downloads/

Page 14: WebTech Tutorial Querying DBPedia

Downloading Jena

http://jena.apache.org

Page 15: WebTech Tutorial Querying DBPedia

Download binary distribution

http://www.apache.org/dist/jena/

Page 16: WebTech Tutorial Querying DBPedia

Getting started with Jena in Netbeans

Create a new Java project

Page 17: WebTech Tutorial Querying DBPedia

Create a Java project

Page 18: WebTech Tutorial Querying DBPedia

Add Jena libraries to class path

Page 19: WebTech Tutorial Querying DBPedia

Add Jena libraries to class path

Page 20: WebTech Tutorial Querying DBPedia

Add all jars in lib folder of Jena distribution

Page 21: WebTech Tutorial Querying DBPedia

Add all jars in lib folder

Page 22: WebTech Tutorial Querying DBPedia

Using Jena with Eclipse

•  http://www.iandickinson.me.uk/articles/jena-eclipse-helloworld/

Page 23: WebTech Tutorial Querying DBPedia

Tutorials

http://jena.apache.org/getting_started/

Page 24: WebTech Tutorial Querying DBPedia

Overview

•  Introduction to Jena •  Setting up the environment •  Querying Dbpedia •  Other APIs

Page 25: WebTech Tutorial Querying DBPedia

QueryFactory

•  has various create() methods to read a textual query •  these create() methods

•  return a Query object, •  which encapsulates a parsed query.

Page 26: WebTech Tutorial Querying DBPedia

QueryExecutionFactory

Create a QueryExecution that will access a SPARQL service over HTTP QueryExecutionFactory.sparqlService(String service, Query query)

Page 27: WebTech Tutorial Querying DBPedia

Querying Dbpedia

SPARQL endpoint http://dbpedia.org/sparql

Page 28: WebTech Tutorial Querying DBPedia

Example

String service = "http://dbpedia.org/sparql"; String query = "ASK { }"; QueryExecution qe = QueryExecutionFactory.sparqlService(service,

query);

Page 29: WebTech Tutorial Querying DBPedia

Test connection import com.hp.hpl.jena.query.QueryExecution;

import com.hp.hpl.jena.query.QueryExecutionFactory;

import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP;

public class QueryTest {

public static void main(String[] args) {

String service = "http://dbpedia.org/sparql";

String query = "ASK { }";

QueryExecution qe = QueryExecutionFactory.sparqlService(service, query);

try {

if (qe.execAsk()) {

System.out.println(service + " is UP");

} // end if

} catch (QueryExceptionHTTP e) {

System.out.println(service + " is DOWN");

} finally {

qe.close();

}

}

}

Page 30: WebTech Tutorial Querying DBPedia

Example queries

http://wiki.dbpedia.org/OnlineAccess#h28-5

Page 31: WebTech Tutorial Querying DBPedia

03/28/11

Example query: people who were born in Eindhoven

String service="http://dbpedia.org/sparql"; String query="PREFIX dbo:<http://dbpedia.org/ontology/>" + "PREFIX : <http://dbpedia.org/resource/>" + "select ?person where {?person dbo:birthPlace :Eindhoven.}"; QueryExecution qe=QueryExecutionFactory.sparqlService(service, query); ResultSet rs=qe.execSelect(); while (rs.hasNext()){ QuerySolution s=rs.nextSolution(); System.out.println(s.getResource("?person").toString()); }

Page 32: WebTech Tutorial Querying DBPedia

Processing results

QuerySolution soln = results.nextSolution() ; RDFNode x = soln.get("varName") ; // Get a result variable by name. Resource r = soln.getResource("VarR") ; // Get a result variable - must be a resource Literal l = soln.getLiteral("VarL") ; // Get a result variable - must be a literal

Page 33: WebTech Tutorial Querying DBPedia

03/28/11

Example

String service="http://dbpedia.org/sparql"; String query="PREFIX dbo:<http://dbpedia.org/ontology/>" + "PREFIX : <http://dbpedia.org/resource/>" + "PREFIX foaf:<http://xmlns.com/foaf/0.1/>" + "select ?person ?name where {?person dbo:birthPlace :Eindhoven." + "?person foaf:name ?name}"; QueryExecution qe=QueryExecutionFactory.sparqlService(service, query); ResultSet rs=qe.execSelect(); while (rs.hasNext()){ QuerySolution s=rs.nextSolution(); Resource r=s.getResource("?person"); Literal name=s.getLiteral("?name"); System.out.println(s.getResource("?person").toString()); System.out.println(s.getLiteral("?name").getString()); }

Page 34: WebTech Tutorial Querying DBPedia

Example query: people who were born in Berlin before 1900

PREFIX dbo: http://dbpedia.org/ontology/ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX : http://dbpedia.org/resource/ SELECT ?name ?birth ?death ?person WHERE { ?person dbo:birthPlace :Berlin . ?person dbo:birthDate ?birth . ?person foaf:name ?name . ?person dbo:deathDate ?death . FILTER (?birth < "1900-01-01"^^xsd:date) . } ORDER BY ?name

Page 35: WebTech Tutorial Querying DBPedia

Other APIs

PHP:  RAP  –  RDF  h+p://www.seasr.org/wp-­‐content/plugins/meandre/rdfapi-­‐php/doc/    

Python:  RDFLib  h+p://www.rdflib.net/    

C:  Redland  h+p://librdf.org/  

Page 36: WebTech Tutorial Querying DBPedia

Installing PHP

Mac OS: https://netbeans.org/kb/docs/php/configure-php-environment-mac-os.html

Windows:

https://netbeans.org/kb/docs/php/configure-php-environment-windows.html

Page 37: WebTech Tutorial Querying DBPedia

Create new PHP project

Page 38: WebTech Tutorial Querying DBPedia

Install RAP

•  Download at: http://wifo5-03.informatik.uni-mannheim.de/bizer/rdfapi/

•  Unpack the zip file. •  Include RDF API into your scripts:

•  define("RDFAPI_INCLUDE_DIR", "C:/Apache/htdocs/rdf_api/api/");

•  include(RDFAPI_INCLUDE_DIR . "RDFAPI.php"); •  Change the constant RDFAPI_INCLUDE_DIR to the

directory in which you have unpacked the zip file.

Page 39: WebTech Tutorial Querying DBPedia

PHP RAP: example

Page 41: WebTech Tutorial Querying DBPedia

Sources

•  Konstantinos Tzonas. The Jena RDF Framework.