@comunica/query-sparql-rdfjs

中文文档
5.4.1发布于 2026/09/15MITTypeScriptCommonJS近 30 天下载 4.3万

A SPARQL query engine for querying over RDF/JS sources

原始使用文档
文档目录4 节
  1. Install
  2. Query
  3. Update
  4. Optimization

Comunica SPARQL RDF/JS

npm version

Comunica SPARQL is a SPARQL query engine for JavaScript for querying RDF/JS sources that implement the Source interface.

This module is part of the Comunica framework.

Install

$ yarn add @comunica/query-sparql-rdfjs

Query

const QueryEngine = require('@comunica/query-sparql-rdfjs').QueryEngine;
const N3Store = require('n3').Store;
const DataFactory = require('n3').DataFactory;

// This can be any RDFJS source
const store = new N3Store();
store.addQuad(DataFactory.quad(
  DataFactory.namedNode('a'), DataFactory.namedNode('b'), DataFactory.namedNode('http://dbpedia.org/resource/Belgium')));
store.addQuad(DataFactory.quad(
  DataFactory.namedNode('a'), DataFactory.namedNode('b'), DataFactory.namedNode('http://dbpedia.org/resource/Ghent')));

// Create our engine, and query it.
// If you intend to query multiple times, be sure to cache your engine for optimal performance.
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: [store],
});

// 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 in an application.

Update

const QueryEngine = require('@comunica/query-sparql-rdfjs').QueryEngine;
const N3Store = require('n3').Store;

// This can be any RDFJS store
const store = new N3Store();

// Create our engine, and query it.
const myEngine = new QueryEngine();
const query = `
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
  <http://example/book1> dc:title "A new book" ;
                         dc:creator "A.N.Other" .
}
`;

// Execute the update
await myEngine.queryVoid(query, {
  sources: [store],
});

// Prints '2' => the store is updated
console.log(store.size);

Read more about updating in an application.

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.

版本与发布记录

@comunica/query-sparql-rdfjs 共发布 106 个版本,首次发布于 2022/02/08;过去 12 个月发布了 16 个版本。

版本发布日期距今
5.4.1latest2026/09/151 个月前
5.4.02026/09/141 个月前
5.3.02026/07/104 个月前
5.2.32026/05/265 个月前
5.2.22026/05/036 个月前
5.2.12026/04/206 个月前

其他发布通道:next → 4.4.2-alpha.49.0。

关于 @comunica/query-sparql-rdfjs 的常见问题

@comunica/query-sparql-rdfjs 是什么?

A SPARQL query engine for querying over RDF/JS sources

如何安装 @comunica/query-sparql-rdfjs?

在项目目录运行 npm install @comunica/query-sparql-rdfjs,或使用 pnpm add / yarn add / bun add。

@comunica/query-sparql-rdfjs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@comunica/query-sparql-rdfjs 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@comunica/query-sparql-rdfjs 可以商用吗?

@comunica/query-sparql-rdfjs 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@comunica/query-sparql-rdfjs 的下载量有多少?

根据 npm 官方下载统计,@comunica/query-sparql-rdfjs 在 2026/10/11 – 2026/11/09 这 30 天内被下载 42,882 次,日均约 1,429 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@comunica/query-sparql-rdfjs 还在维护吗?

最新版本 5.4.1 发布于 2026/09/15(1 个月前);至今共发布 106 个版本,其中过去 12 个月发布了 16 个;首次发布于 2022/02/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。