Skip to content

CreateNamedGraphMapper

mark.birbeck@webBackplane.com edited this page Jan 16, 2010 · 4 revisions
  1. summary How to create a named-graph mapper.
  2. labels Module-RDFa,Type-HowTo

Table of Contents

Introduction

A named-graph mapper provides information to the library about how to process data from non-SPARQL services. This allows the library to handle all of the steps for retrieving data and storing it into the triple store, rather than the author having to hand-code this. Instead, the author can simply create a SPARQL query that refers to the non-SPARQL service.

Using a non-SPARQL service in a SPARQL query

The mechanism for retrieving the data is the same as for SPARQL end-points -- using named graphs. Rather than making a request to the service, processing the returned data, adding it to the triple store, and then querying for that data, an author can simply write a SPARQL query that uses a named graph.

For example, to obtain Twitter updates for a particular person, an author simply needs to write the following SPARQL query:

Setting up a mapper

Mappers are defined by inserting triples into a named graph, called . The triples consist of:

Subject

The subject of the triples is an identifier for the mapper. It won't be the same as the URL of the service, and shouldn't conflict with other mappers.

matches

The predicate provides a regular expression that is used to test named-graph URIs. If the URI matches the expression, the mapper will be used to control the service request, and the processing of any returned data.

For our Twitter example, the regular expression would be:

This matches any URL that begins with "http://www.twitter.com/", and stores the value after the slash ready for further use. In our example, that is "manusporny".

uri

The predicate provides information on how to construct a URI.

The Twitter API describes how Tweets for a particular person can be obtained -- using URLs that contain the Twitter name. The URL required by our Twitter example would be:

In order to get from our named-graph of "http://www.twitter.com/manusporny" to this URL, we set the URI predicate to the following value:

This tells the mapper to use the value extracted by the predicate, in place of the "%s" in the predicate.

params

The predicate is not yet implemented.

adddata

The predicate contains a callback that will be executed once data has been received from the service. The function usually contains a series of calls to the method, in order to add the data returned, as a set of triples.

A great deal of data is returned by Twitter, but to illustrate, an individual post looks something like this:

The key pieces of information we need to satisfy the SPARQL query, are the name of the person Twittering, what they said, and their picture. In the JSON object returned from the service, they are `user.screen_name`, `text`, and `user.profile_image_url`.

A Full Example

The following is a Twitter-mapper for obtaining statuses about a user: