SPARQL
To programmatically perform your queries on the SPARQL endpoint, send a GET
request to https://query.semantic.builders/sparql
adding the header api-key
with a key enabled for "Access to SPARQL endpoint".
Here you find a few samples, in different languages:
class SPARQLQueryDispatcher {
constructor( endpoint ) {
this.endpoint = endpoint;
}
query( sparqlQuery ) {
const fullUrl = this.endpoint + '?query=' + encodeURIComponent( sparqlQuery );
const headers = {
'api-key': 'YOUR_API_KEY',
'Accept': 'application/sparql-results+json',
};
return fetch( fullUrl, { headers } ).then( body => body.json() );
}
}
const endpointUrl = 'https://query.semantic.builders/sparql';
const sparqlQuery = YOUR_SPARQL_QUERY;
const queryDispatcher = new SPARQLQueryDispatcher( endpointUrl );
queryDispatcher.query( sparqlQuery ).then( console.log );
<?php
// composer require easyrdf/easyrdf
// https://www.easyrdf.org/
class SPARQLQueryDispatcher extends \EasyRdf\Sparql\Client
{
private $endpointUrl;
public function __construct(string $endpointUrl, string $apiKey)
{
parent::__construct($endpointUrl);
$client = \EasyRdf\Http::getDefaultHttpClient();
$this->setHeaders($client, 'api-key', $apiKey);
}
}
$endpointUrl = 'https://query.semantic.builders/sparql';
$sparqlQueryString = YOUR_SPARQL_QUERY;
$apiKey = 'YOUR_API_KEY';
$queryDispatcher = new SPARQLQueryDispatcher($endpointUrl, $apiKey);
$queryResult = $queryDispatcher->query($sparqlQueryString);
var_export($queryResult);
use LWP::UserAgent;
use Data::Dumper;
use JSON::XS;
sub wdSparqlQuery(@args) {
my $query = shift;
my $key = shift;
my $format = shift;
my $endpointURL = "https://query.semantic.builders/sparql";
my $queryURL = "${endpointURL}?query=${query}&format=${format}";
my $ua = LWP::UserAgent -> new;
my $req = HTTP::Request -> new(
GET => $queryURL,
HTTP::Headers->new('api-key' => $key),
);
my $res = $ua -> request($req);
my $str = $res -> content;
return $str;
}
$query = "YOUR_SPARQL_QUERY";
$key = "YOUR_API_KEY";
$format = "json";
$data = JSON::XS::decode_json(wdSparqlQuery($query, $key, $format));
print "Retrieved data:\n";
print Dumper($data);
# pip install sparqlwrapper
# https://rdflib.github.io/sparqlwrapper/
import sys
from SPARQLWrapper import SPARQLWrapper, JSON
endpoint_url = "https://query.semantic.builders/sparql"
query = "YOUR_SPARQL_QUERY"
apikey = "YOUR_API_KEY"
def get_results(endpoint_url, query):
sparql = SPARQLWrapper(endpoint_url)
sparql.addCustomHttpHeader('api-key', apikey)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query().convert()
results = get_results(endpoint_url, query)
for result in results["results"]["bindings"]:
print(result)
#gem install sparql
#http://www.rubydoc.info/github/ruby-rdf/sparql/frames
require 'sparql/client'
endpoint = "https://query.semantic.builders/sparql"
sparql = YOUR_SPARQL_QUERY
client = SPARQL::Client.new(endpoint, :method => :get, headers: {'api-key' => 'YOUR_API_KEY'})
rows = client.query(sparql)
puts "Number of rows: #{rows.size}"
for row in rows
for key,val in row do
# print "#{key.to_s.ljust(10)}: #{val}\t"
print "#{key}: #{val}\t"
end
print "\n"
end