Comunica SPARQL is a SPARQL query engine for JavaScript for querying local and remote RDF files.
This module is part of the Comunica framework.
Since this engine can read local files,
SERVICE clauses are not allowed to target local files by default,
as queries from untrusted parties could otherwise read arbitrary files from the local file system.
If all queries originate from trusted parties,
this can be enabled via the serviceAllowFileTargets context entry:
$ comunica-sparql-file path/to/my/file.ttl -c '{ "serviceAllowFileTargets": true }' "SELECT * WHERE { ... }"
$ yarn global add @comunica/query-sparql-file
or
$ npm install -g @comunica/query-sparql-file
Show 100 triples from a remote FOAF profile:
$ comunica-sparql-file https://ruben.verborgh.org/profile/#me "CONSTRUCT WHERE { ?s ?p ?o } LIMIT 100"
Show 100 triples from a local RDF file:
$ comunica-sparql-file path/to/my/file.ttl "CONSTRUCT WHERE { ?s ?p ?o } LIMIT 100"
Show all triples from a local and remote file:
$ comunica-sparql-file path/to/my/file.ttl https://ruben.verborgh.org/profile/#me "CONSTRUCT WHERE { ?s ?p ?o } LIMIT 100"
Show the help with all options:
$ comunica-sparql-file --help
Just like Comunica SPARQL,
a dynamic variant (comunica-dynamic-sparql-file) also exists.
Read more about querying from the command line.
This engine can be used in JavaScript/TypeScript applications as follows:
const QueryEngine = require('@comunica/query-sparql-file').QueryEngine;
const myEngine = new QueryEngine();
const bindingsStream = await myEngine.queryBindings(`
SELECT ?s ?p ?o WHERE {
?s ?p <http://dbpedia.org/resource/Belgium>.
?s ?p ?o
} LIMIT 100`, {
sources: ['http://fragments.dbpedia.org/2015/en'],
});
// Consume results as a stream (best performance)
bindingsStream.on('data', (binding) => {
console.log(binding.toString()); // Quick way to print bindings for testing
console.log(binding.has('s')); // Will be true
// Obtaining values
console.log(binding.get('s').value);
console.log(binding.get('s').termType);
console.log(binding.get('p').value);
console.log(binding.get('o').value);
});
bindingsStream.on('end', () => {
// The data-listener will not be called anymore once we get here.
});
bindingsStream.on('error', (error) => {
console.error(error);
});
// Consume results as async iterable (easier)
for await (const binding of bindingsStream) {
console.log(binding.toString());
}
// Consume results as an array (easier)
const bindings = await bindingsStream.toArray();
console.log(bindings[0].get('s').value);
console.log(bindings[0].get('s').termType);
Read more about querying an application.
Start a webservice exposing http://fragments.dbpedia.org/2015-10/en via the SPARQL protocol, i.e., a SPARQL endpoint.
$ comunica-sparql-file-http path/to/my/file.ttl
Show the help with all options:
$ comunica-sparql-file-http --help
The SPARQL endpoint can only be started dynamically.
An alternative config file can be passed via the COMUNICA_CONFIG environment variable.
Use bin/http.js when running in the Comunica monorepo development environment.