Querying over RDF/JS sources


On this page

    Two of the different types of sources that are supported by Comunica are the RDF/JS Source interface and the RDF/JS DatasetCore interface. These allow you to pass objects as source to Comunica as long as they implement one of the interfaces.

    An RDF/JS Source exposes the match method that allows quad pattern queries to be executed, and matching quads to be returned as a stream. An RDF/JS DatasetCore also exposes a match method which returns a Dataset instead of a stream.

    Learn more about RDF/JS in this RDF/JS guide.

    Several implementations of these Source and DatasetCore interfaces exist. In the example below, we make use of the Store from N3.js (which implements both interfaces), that offers one possible implementation when you want to query over it with Comunica within a JavaScript application:

    const store = new N3.Store();
    store.addQuad(
      namedNode('http://ex.org/Pluto'),
      namedNode('http://ex.org/type'),
      namedNode('http://ex.org/Dog')
    );
    store.addQuad(
      namedNode('http://ex.org/Mickey'),
      namedNode('http://ex.org/type'),
      namedNode('http://ex.org/Mouse')
    );
    
    const bindingsStream = await myEngine.queryBindings(`SELECT * WHERE { ?s ?p ?o }`, {
      sources: [store],
    });
    
    We recommend passing RDF/JS `Source`'s when possible. As they have an asynchronous streaming interface that leads to better performance when working with large datasets.
    If an object implements both the RDF/JS `Source` and RDF/JS `DatasetCore`, then the source implementation is used.
    Instead of the default Comunica SPARQL package (@comunica/query-sparql), the Comunica SPARQL RDF/JS (@comunica/query-sparql-rdfjs) can also be used as a more lightweight alternative that only allows querying over RDF/JS sources.
    If the RDF/JS `Source` also implements the RDF/JS Store interface, then it is also supports update queries to add, change or delete quads in the store.

    Optional: query optimization

    The RDFJS Source interface by default only exposed the match method. In order to allow Comunica to produce more efficient query plans, you can optionally expose a countQuads method that has the same signature as match, but returns a number or Promise<number> that represents (an estimate of) the number of quads that would match the given quad pattern. Certain Source implementations may be able to provide an efficient implementation of this method, which would lead to better query performance.

    If Comunica does not detect a countQuads method, it will fallback to a sub-optimal counting mechanism where match will be called again to manually count the number of matches.