Page:Aaron Swartz s A Programmable Web An Unfinished Work.pdf/53

From Wikisource
Jump to navigation Jump to search
This page has been proofread, but needs to be validated.

CHAPTER 6 41

Building a Database:

Queries and Dumps

APIs are nice and all, but they’re fairly limiting: they only give you the answers to questions you already know how to ask. Want to find out more about book 3j7is? Sure, it’ll tell you. But want to know which books published recently share an author with a book published over a hundred years ago? That’s a little more complicated.

But, luckily, not impossible. It seems ridiculous to come up with your own API that could answer any sort of question like this. But remember those RDF query languages we were making fun of in the last chapter? This turns out to be just the sort of thing they’re perfect at.

The official RDF query language is called SPARQL (SPARQL Protocol And RDF Query Language—pronounced “sparkle”). If you’re familiar with SQL, the standard database query language, SPARQL will look similar, only with RDF stuck in all the right places. Here’s how you express our previous question in SPARQL:

    >    PREFIX : <http://books.example.org/api/schema#>
    >    SELECT ?booknew, ?bookold
    >    WHERE {
    >      ?booknew :author ?author .
    >      ?booknew :publication_year ?yearnew .
    >      FILTER ( ?yearnew >= 2008 )
    >      ?bookold :author ?author .
    >      ?bookold :publication_year ?yearold .
    >      FILTER ( ?yearold <= 1908 )
    >    }