Querying the Semantic Web

Similar documents
SPARQL เอกสารหล ก ใน มคอ.3

Day 2. RISIS Linked Data Course

3. Queries Applied Artificial Intelligence Prof. Dr. Bernhard Humm Faculty of Computer Science Hochschule Darmstadt University of Applied Sciences

SPARQL. Fausto Giunchiglia and Mattia Fumagallli. University of Trento

Chapter 3 Querying RDF stores with SPARQL

SPARQL QUERY LANGUAGE WEB:

Semantic Web Information Management

Lars Schmidt-Thieme, Information Systems and Machine Learning Lab (ISMLL), University of Hildesheim, Germany, Course on XML and Semantic Web

2. Knowledge Representation Applied Artificial Intelligence

SPARQL ME-E4300 Semantic Web,

XML and Semantic Web Technologies. III. Semantic Web / 3. SPARQL Query Language for RDF

SPARQL: An RDF Query Language

SPARQL Protocol And RDF Query Language

Semantic Information Retrieval: An Ontology and RDFbased

Mapping Relational Data to RDF with Virtuoso's RDF Views

CSC Web Programming. Introduction to SQL

infoh509 xml & web technologies lecture 9: sparql Stijn Vansummeren February 14, 2017

BUILDING THE SEMANTIC WEB

Semantic Web Technologies: Assignment 1. Axel Polleres Siemens AG Österreich

MI-PDB, MIE-PDB: Advanced Database Systems

From the Web to the Semantic Web: RDF and RDF Schema

Building Blocks of Linked Data

Knowledge Representation RDF Turtle Namespace

Linked data and its role in the semantic web. Dave Reynolds, Epimorphics

SPARQL By Example: The Cheat Sheet

INFO216: Advanced Modelling

KNOWLEDGE GRAPHS. Lecture 3: Modelling in RDF/Introduction to SPARQL. TU Dresden, 30th Oct Markus Krötzsch Knowledge-Based Systems

Chapter 3 RDF Syntax

SPARQL. Dr Nicholas Gibbins

RDF AND SPARQL. Part IV: Syntax of SPARQL. Dresden, August Sebastian Rudolph ICCL Summer School

Introduction to Semantic Web Databases. Version 1 Prepared By: Amgad Madkour Ph.D. Candidate Purdue University April 2018

Semantic Web. Querying on the Web: XQuery, RDQL, SparQL. Morteza Amini. Sharif University of Technology Fall 94-95

Semantic Web Systems Querying Jacques Fleuriot School of Informatics

Querying RDF & RDFS. Several query languages exist to retrieve

Formalising the Semantic Web. (These slides have been written by Axel Polleres, WU Vienna)

SEMANTIC WEB 04 RDF SYNTAX TURTLE, RDF/XML, N-TRIPLES, N3 IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Multi-agent and Semantic Web Systems: Querying

Semantic Web Technologies

COMP9321 Web Application Engineering

Jena.

DBPedia (dbpedia.org)

COMPUTER AND INFORMATION SCIENCE JENA DB. Group Abhishek Kumar Harshvardhan Singh Abhisek Mohanty Suhas Tumkur Chandrashekhara

Knowledge Representation VII - IKT507. SPARQL stands for SPARQL Protocol And RDF Query Language

Semantic Web Fundamentals

Outline. Introduction Semantic Web

Semantic Web Fundamentals

Semantic Days 2011 Tutorial Semantic Web Technologies

Implementing and extending SPARQL queries over DLVHEX

Profiles Research Networking Software API Guide

Semantic Web and Python Concepts to Application development

Describing Web Resources in RDF

Sematic Web. Lecture Outline. Reasoning. Holger Wache SS 2008

CS 582 Database Management Systems II

RDFS. Suresh Manandhar* & Dimitar Kazakov

Web Ontology Editor: architecture and applications

Linked Data and RDF. COMP60421 Sean Bechhofer

Semantic Web Solutions

Web NDL Authorities SPARQL API Specication

KNOWLEDGE GRAPHS. Lecture 4: Introduction to SPARQL. TU Dresden, 6th Nov Markus Krötzsch Knowledge-Based Systems

An overview of RDB2RDF techniques and tools

Ontological Modeling: Part 2

Chapter 3. RDF Schema

> Semantic Web Use Cases and Case Studies

Introduction to RDF and the Semantic Web for the life sciences

Orchestrating Music Queries via the Semantic Web

Making BioPAX SPARQL

Introducing Linked Data

Using RDF to Model the Structure and Process of Systems

Linked Data. Department of Software Enginnering Faculty of Information Technology Czech Technical University in Prague Ivo Lašek, 2011

SEMANTIC WEB 05 RDF SCHEMA MODELLING SEMANTICS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Linked Data and RDF. COMP60421 Sean Bechhofer

2. RDF Semantic Web Basics Semantic Web

Today s Plan. INF3580/4580 Semantic Technologies Spring Reminder: RDF triples. Outline. Lecture 4: The SPARQL Query Language.

Contents. G52IWS: The Semantic Web. The Semantic Web. Semantic web elements. Semantic Web technologies. Semantic Web Services

SADI Semantic Web Services

BASI DI DATI II 2 modulo Parte VIII: SPARQL

Connecting SMW to RDF Databases: Why, What, and How?

Grid Resources Search Engine based on Ontology

SEMANTIC WEB 07 SPARQL TUTORIAL BY EXAMPLE: DBPEDIA IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Querying Description Logics

Semantic Web. MPRI : Web Data Management. Antoine Amarilli Friday, January 11th 1/29

INF3580 SPRING 2014 EXERCISES WEEK 4

Energy-related data integration using Semantic data models for energy efficient retrofitting projects

Semantic Web and Linked Data

SPARQL Protocol And RDF Query Language

Semantic Integration with Apache Jena and Apache Stanbol

Fuseki Server Installation

Unit 1 a Bird s Eye View on RDF(S), OWL & SPARQL

Interacting with Linked Data Part I: General Introduction

Querying Semantic Web Data

RDF. Charlie Abela Department of Artificial Intelligence

Developing markup metaschemas to support interoperation among resources with different markup schemas

An Introduction to the Semantic Web. Jeff Heflin Lehigh University

Inception of RDF: Context

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Chapter 4: SQL. Basic Structure

Optimising a Semantic IoT Data Hub

SEMANTIC WEB 03 RDF DATA MODEL RESOURCE DESCRIPTION FRAMEWORK IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Transcription:

Querying the Semantic Web CSE 595 Semantic Web Instructor: Dr. Paul Fodor Stony Brook University http://www3.cs.stonybrook.edu/~pfodor/courses/cse595.html

Lecture Outline SPARQL Infrastructure Basics: Matching Patterns Filters Constructs for Dealing with an Open World Organizing Result Sets Other Forms of SPARQL Queries Querying Schemas Adding Information with SPARQL Update 2

Why an RDF Query Language? 3 SPARQL is specifically designed for RDF, and is tailored to and relies upon the various technologies underlying the web If you are familiar with database query languages like SQL, you will notice many similarities XML is at a lower level of abstraction than RDF Thus we would require: XML namespaces several XPath queries XSD data types

SPARQL Infrastructure A triple store is a database for RDF Within the specifications for SPARQL a triple store is referred to as a Graph Store. Before one can query a triple store, it needs to be populated with RDF A mechanism called SPARQL Update provides a series of options for inserting, loading, and deleting RDF into a triple store Most triple stores provide bulk upload options Once data is loaded into a triple store, it can be queried 4 by sending SPARQL queries using the SPARQL protocol

SPARQL Infrastructure Each triple store provides what is termed an endpoint, where SPARQL queries can be submitted 5 clients send queries to an endpoint using the HTTP protocol clients can issue a SPARQL query to an endpoint by entering it into the browser s URL better clients designed specifically for SPARQL are used APIs are also used (e.g., Jena ARQ)

SPARQL Infrastructure There are numerous SPARQL endpoints on the web access to large amounts of data For example, http://dbpedia.org/sparql provides a query endpoint to query over an RDF representation of Wikipedia https://query.wikidata.org/ http://babelnet.org/sparql/ list of SPARQL endpoints at http://ckan.org 6

SPARQL Basic Queries SPARQL is based on matching graph patterns: The simplest graph pattern is the triple pattern like an RDF triple, but with the possibility of a variable instead of an RDF term in the subject, predicate, or object positions A variable starts with? Combining triple patterns gives a basic graph pattern, where an exact match to a graph is needed to fulfill a pattern 7

Using select-from-where As in SQL, SPARQL queries have a SELECT-FROM-WHERE structure: SELECT specifies the projection: the number and order of retrieved data FROM is used to specify the source being queried (optional) WHERE imposes constraints on possible solutions in the form of graph pattern templates and boolean constraints Retrieve all phone numbers of staff members: SELECT?x?y WHERE {?x uni:phone?y. Here?x and?y are variables, and?x uni:phone?y represents a resource-property-value triple pattern 8

Example Consider the RDF describing the Baron Way apartment and its location: @prefix swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. @prefix dbpedia: <http://dbpedia.org/resource/>. @prefix dbpedia-owl: <http://dbpedia.org/ontology/>. swp:baronwayapartment swp:hasnumberofbedrooms 3; swp:ispartof swp:baronwaybuilding. swp:baronwaybuilding dbpedia-owl:location dbpedia:amsterdam, dbpedia:netherlands. 9

Example To find the location of the building, a triple pattern is: swp:baronwaybuilding dbpedia-owl:location dbpedia:amsterdam. In SPARQL, we can just replace any element of the triple with a variable: swp:baronwaybuilding dbpedia-owl:location?location The triple store will take this graph pattern and try to find sets of triples that match the pattern it would return dbpedia:amsterdam and dbpedia:netherlands it finds all triples where swp:baronwaybuilding is in the subject position and dbpedia-owl:location is in the predicate position 10

Example A complete SPARQL query also contains all prefixes and we need to tell the triple store that we are interested in the results for a particular variable: PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?location WHERE { swp:baronwaybuilding dbpedia-owl:location?location. 11

Example The results of the query are returned in a set of mappings called bindings that denote which elements correspond to a given variable: 12

Example Find where the BaronWayApartment is located: PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. 13 SELECT?location WHERE { swp:baronwayapartment swp:ispartof?building.?building dbpedia-owl:location?location. The variable?building is in the subject position: variables can occur in any position in the SPARQL query. The query reuses the variable name?building: find triples where the object of the first statement is the same as the subject of the second statement.

Example Find all the information about Baron Way Apartment in the triple store: PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?p?o WHERE { swp:baronwayapartment?p?o. 14

Example On larger data sets we may not know how many results there are or if our query would return a whole dataset it is fairly easy to write queries that can return millions of triples PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT?c WHERE {?c rdf:type rdfs:class. Retrieves all triple patterns, where: the property is rdf:type the object is rdfs:class Which means that it retrieves all classes 15

Example It is good practice to limit the number of answers a query returns, especially when using public endpoints with the LIMIT keyword PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?p?o WHERE { swp:baronwayapartment?p?o. LIMIT 10 16

Example SPARQL provides a way of expressing concisely chains of properties instead of: SELECT?apartment WHERE {?apartment swp:ispartof?building.?building dbpedia-owl:location dbpedia:amsterdam. we can do: SELECT?apartment WHERE {?apartment swp:ispartof/dbpedia-owl:location dbpedia:amsterdam. 17

Filters Find all the apartments that have more than 2 bedrooms: 18 PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?apartment WHERE {?apartment swp:hasnumberofbedrooms?bedrooms. FILTER (?bedrooms > 2). The syntactic shortcuts for SPARQL and Turtle are the same like Turtle, SPARQL allows for shortened forms of common literals in this case, 2 is a shortcut for "2"^^xsd:integer Less than, greater than, and equality are supported for numeric data types (i.e., integers, decimals) as well as date/time

Filters Regular expressions for strings assume that our data set contains the triple: swp:baronwayapartment swp:address "4 Baron Way Circle" We might like to find all the resources that contain "Baron Way" in their address PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?apartment WHERE {?apartment swp:address?address. FILTER regex(?address, "Baron Way"). 19

Filters regex is a filter function str function converts resources and literals into string representations that can then be used in regex For example, we can search for "Baron" in the URL of the resource instead of using the label 20 PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?apartment WHERE {?apartment swp:address?address. FILTER regex(str(?apartment), "Baron"). Note that the apartment resource is a URL

Implicit Join Retrieve all lecturers and their phone numbers: SELECT?x?y WHERE {?x rdf:type uni:lecturer ; uni:phone?y. Implicit join: We restrict the second pattern only to those triples, the resource of which is in the variable?x Here we use a syntax shortcut as well: a semicolon indicates that the following triple shares its subject with the previous one 21

Implicit join The previous query is equivalent to writing: SELECT?x?y WHERE {?x rdf:type uni:lecturer.?x uni:phone?y. 22

Explicit Join Retrieve the name of all courses taught by the lecturer with ID 949352: SELECT?n WHERE {?x rdf:type uni:course ; uni:istaughtby :949352.?c uni:name?n. FILTER (?c =?x). 23

Constructs for Dealing with an Open World 24 Unlike a traditional database, not every resource on the Semantic Web will be described using the same schema or have all of the same properties This is called the open world assumption some apartments may be more well described than others some may be described using a different vocabulary @prefix swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. @prefix dbpedia: <http://dbpedia.org/resource/>. @prefix dbpedia-owl: <http://dbpedia.org/ontology/>. @prefix xsd: <http://www.w3.org/2001/xmlschema#>. swp:baronwayapartment swp:hasnumberofbedrooms 3. swp:baronwayapartment dbpedia-owl:location dbpedia:amsterdam. swp:baronwayapartment refs:label "Baron Way Apartment for Rent". swp:floridaavestudio swp:hasnumberofbedrooms 1. swp:floridaavestudio dbpedia-owl:locationcity dbpedia:amsterdam.

Constructs for Dealing with an Open World Some results are OPTIONAL: PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?apartment WHERE { {?apartment dbpedia-owl:location dbpedia:amsterdam. UNION {?apartment dbpedia-owl:locationcity dbpedia:amsterdam. OPTIONAL {?apartment rdfs:label?label. 25

Constructs for Dealing with an Open World Property paths can also be used to create a more concise SPARQL query using the operator that can express one or more possibilities: PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?apartment WHERE { {?apartment dbpedia-owl:location dbpedia-owl:locationcity dbpedia:amsterdam. OPTIONAL {?apartment rdfs:label?label. 26

Optional Patterns <uni:lecturer rdf:about= 949352 > <uni:name>grigoris Antoniou</uni:name> </uni:lecturer> <uni:professor rdf:about= 94318 > <uni:name>david Billington</uni:name> <uni:email>david@work.example.org</uni:email> </uni:professor> For one lecturer it only lists the name For the other it also lists the email address 27

Optional Patterns All lecturers and their email addresses: SELECT?name?email WHERE {?x rdf:type uni:lecturer ; uni:name?name ; uni:email?email. Grigoris Antoniou is listed as a lecturer, but he has no e-mail address, so he is not selected?name?email David Billington david@work.example.org 28

Optional Patterns 29 As a solution we can adapt the query to use an optional pattern: SELECT?name?email WHERE {?x rdf:type uni:lecturer ; uni:name?name. OPTIONAL { x? uni:email?email The meaning is roughly give us the names of lecturers, and if known also their e-mail address?name?email Grigoris Antoniou David Billington david@work.example.org

Organizing Result Sets It is often the case that we want the results of our queries to be returned in a particular way, either grouped, counted, or ordered: We can eliminate duplicate results from the results set using the DISTINCT keyword by placing it after the SELECT keyword (this will ensure that only unique variable bindings are returned) We can order a returned result set using the ORDER BY keyword The keyword DESC denotes descending order. Likewise, ASC denotes ascending order. Ordering a string or url is done alphabetically. 30

Organizing Result Sets Find the apartments ordered by the number of bedrooms: PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT?apartment?bedrooms WHERE {?apartment swp:hasnumberofbedrooms?bedrooms. ORDER BY DESC(?bedrooms) 31

Organizing Result Sets Collect results set together using aggregate functions count the number of results (COUNT) sum (SUM), minimum, maximum, and average (MIN, MAX, AVG). PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. SELECT (AVG(?bedrooms) AS?avgNumRooms) WHERE {?apartment swp:hasnumberofbedrooms?bedrooms. 32

Other Forms of SPARQL Queries ASK query simply checks to see whether a graph pattern exists in a data set instead of returning a result PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. ASK?apartment WHERE {?apartment swp:hasnumberofbedrooms 3. ASK queries are used because they are faster to compute than retrieving an entire set of results. 33

Other Forms of SPARQL Queries The CONSTRUCT query is used to retrieve an RDF graph from a larger set of RDF, not a list of variable bindings PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX dbpedia: <http://dbpedia.org/resource/>. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>. CONSTRUCT {?apartment swp:hasnumberofbedrooms?bedrooms.?apartment swp:isbigapartment true. WHERE{?apartment swp:hasnumberofbedrooms?bedrooms. FILTER (?bedrooms > 2) A graph is returned with new properties 34

Querying Schemas Consider an RDFS housing ontology @prefix swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. swp:unit rdf:type rdfs:class. swp:residentialunit rdf:type rdfs:class. swp:residentialunit rdfs:subclassof swp:unit. swp:apartment rdf:type rdfs:class. swp:apartment rdfs:subclassof swp:residentialunit. 35

Querying Schemas Determine the Residential Units in our dataset by querying both the instance data and schema simultaneously PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>. SELECT?apartment WHERE{?apartment a?unittype.?unittype rdfs:subclassof swp:residentialunit. we used the same Turtle shorthand, a, to denote rdf:type 36

Adding Information with SPARQL Update SPARQL constructs for insertion, loading, and deleting of triples PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>. INSERT DATA { swp:luxuryapartment rdfs:subclassof swp:apartment. If you have a large file containing RDF available on the web, you can load it into a triple store using the following command: LOAD <http://example.com/apartment.rdf> 37

Deleting Information with SPARQL Update Delete triples with DELETE DATA: PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>. DELETE DATA { swp:luxuryapartment rdfs:subclassof swp:apartment. no variables are allowed and all triples must be fully specified Delete triples with DELETE WHERE: remove all the triples containing information about apartments with more than two bedrooms PREFIX swp: <http://www.semanticwebprimer.org/ontology/apartments.ttl#>. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>. DELETE WHERE{?apartment swp:hasnumberofbedrooms?bedrooms. FILTER (?bedrooms > 2) 38

Deleting Information with SPARQL Update Remove all the contents of a triple store the CLEAR construct: CLEAR ALL 39

References http://www.w3.org/tr/sparql11-query/ http://www.w3.org/tr/sparql11-update/ http://www.w3.org/tr/rdf-sparql-protocol/ http://jena.sourceforge.net/arq/tutorial/ 40