Jack Parker
Jack Parker

Reputation: 569

How Do I Query Against Data.gov

I am trying to teach myself this weekend how to run API queries against a data source in this case data.gov. At first I thought I'd use a simple SQL variant, but it seems in this case I have to use SPARQL.

I've read through the documentation, downloaded Twinkle, and can't seem to quite get it to run. Here is an example of a query I'm running. I'm basically trying to find all gas stations that are null around Denver, CO.

PREFIX station: https://api.data.gov/nrel/alt-fuel-stations/v1/nearest.json?api_key=???location=Denver+CO

SELECT *
WHERE
 { ?x station:network ?network like "null"
}

Any help would be very much appreciated.

Upvotes: 2

Views: 244

Answers (1)

scotthenninger
scotthenninger

Reputation: 4001

SPARQL is a graph pattern language for RDF triples. A query consists of a set of "basic graph patterns" described by triple patterns of the form <subject>, <predicate>, <object>. RDF defines the subject and predicate with URI's and the object is either a URI (object property) or literal (datatype or language-tagged property). Each triple pattern in a query must therefore have three entities.

Since we don't have any examples of your data, I'll provide a way to explore the data a bit. Let's assume your prefix is correctly defined, which I doubt - it will not be the REST API URL, but the URI of the entity itself. Then you can try the following:

PREFIX station: <http://api.data.gov/nrel...>
SELECT *
WHERE
{  ?s station:network ?network .
}

...setting the PREFIX to correctly represent the namespace for network. Then look at the binding for ?network and find out how they represent null. Let's say it is a string as you show. Then the query would look like:

PREFIX station: <http://api.data.gov/nrel...>
SELECT ?s
WHERE
{  ?s station:network "null" .
}

There is no like in SPARQL, but you could use a FILTER clause using regex or other string matching features of SPARQL.

And please, please, please google "SPARQL" and "RDF". There is lots of information about SPARQL, and the W3C's SPARQL 1.1 Query Language Recommendation is a comprehensive source with many good examples.

Upvotes: 3

Related Questions