Now that we understand RDF and SPARQL, we can try to build our own database in RDF format and also, query them using SPARQL. To use the database, which might be offline, and query on them, we can use Twinkle SPARQL query tool available at http://www.ldodds.com/projects/twinkle/. I have downloaded Twinkle 2.0 version (binary) and tried. Below mentioned examples are run with this version.
STARTING UP (Taken from the Twinkle's page)
You'll need to have Java 1.5 or higher installed to use Twinkle. Download the distribution and unzip it into a new directory. Open a command-prompt and change the current directory to this newly created directory i.e.
cd <path>
and execute the following:
cd <path>
and execute the following:
java -jar twinkle.jar
Below is the screenshot of Twinkle tool obtained on the execution of the above command. On Twinkle, result can be viewed in two formats - text or table. The format can be selected by clicking on relevant tab in the bottom region of the window.
EXAMPLE 1
Let us use john.xml or john.n3 as the data source. Save either file at a location on your computer. In Twinkle's window, against 'Data URL', click on 'File' button to browse your computer and specify the file (john.xml or john.n3).
Once the database is fixed, we can write a SPARQL query to access specific information in it.
Query 1
If we want to get John's mother's name, the query looks as below:
SPARQL query:
Explanation:
Result:
Query 2
Similarly, if we want to get John's father's name, the query is as below:
SPARQL query:
Explanation:
Result:
EXAMPLE 2
Lets try writing SPARQL queries on a slightly bigger dataset. For this, lets consider the foaf.rdf file generated by David Beckett - http://www.dajobe.org/foaf.rdf. Save this file on your computer and supply this as the 'Data URL' for Twinkle.
Query 1 (Taken from http://librdf.org/query)
SPARQL query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT ?name WHERE { ?x rdf:type foaf:Person . ?x foaf:name ?name } ORDER BY ?name
Explanation:
This query uses namespaces of rdf and foaf. The query is to get distinct names (duplicate entries not listed) of persons listed in the database sorted in alphabetical order.
Result: (text format)
------------------------
| name |
========================
| "Bijan Parsia" |
| "Damian Steer" |
| "Dan Brickley" |
| "Dan Connolly" |
| "Dave Beckett" |
| "Edd Dumbill" |
| "Eric Miller" |
| "Jan Grant" |
| "Jim Hendler" |
| "Jo Walsh" |
| "Libby Miller" |
| "Matt Biddulph" |
| "Morten Frederiksen" |
| "Norm Walsh" |
| "Phil McCarthy" |
| "Sean B. Palmer" |
| "Tim Berners-Lee" |
------------------------
Prev: RDF & SPARQL
No comments:
Post a Comment