Skip to content
DuckDB community extension

rdf

Read and write RDF, query using SPARQL

01 / SETUP

Install and load

Run these commands in a Haybarn SQL session. The extension is available from the community repository.

Haybarn 2.0.0 · SQL
INSTALL rdf FROM community;
LOAD rdf FROM community;

Try it in a temporary SQL session with rdf already loaded. Queries run in this tab; examples using files or services need their own setup.

Using upstream DuckDB? Follow the maintainer’s installation instructions.

02 / QUERY

Usage example

0. Assuming the extension is already installed and loaded

SQL
-- 0. Assuming the extension is already installed and loaded

-- 1. Get number of ntriples in a directory
SELECT COUNT(*) FROM read_rdf('data/shards/*.nt');

-- 2. Get subjects and predicates of a turtle file
SELECT subject, predicate FROM read_rdf('test/rdf/tests.ttl');

-- 3. Write a query to turtle RDF, using R2RML mapping
COPY (SELECT empno, ename, deptno FROM emp)
TO 'output.nt'
(FORMAT r2rml, mapping 'mapping.ttl');

-- 4. Execute a full R2RML mapping (with embedded queries) to write RDF
COPY (SELECT 1) TO 'output.nt' (FORMAT r2rml, mapping 'mapping.ttl');

-- 5. Check if an R2RML mapping is valid
SELECT is_valid_r2rml('mapping.ttl');

-- 6. Pivot RDF to a wide table
SELECT * FROM pivot_rdf('data.ttl');

-- 7. Read a SPARQL endpoint
SELECT * FROM read_sparql(
         'https://query.wikidata.org/sparql',
         'SELECT (COUNT(*) AS ?count) WHERE { ?item wdt:P31 wd:Q5 .}'
     );

-- 8. Execute a SPARQL query using against DuckDB tables using an R2RML mapping
SELECT * FROM execute_sparql(
         'PREFIX ex: <http://example.com/ns#> SELECT ?e ?name WHERE { ?e ex:name ?name }',
         'full_r2rml_mapping.ttl'
     );

Provided by nonodename. Not yet verified on Haybarn 2.0.0.

Sample data & setup

About this extension

The duck_rdf extension enables DuckDB to read and write RDF (Resource Description Framework) data directly, using the SERD library for parsing and serialization.

Read: Turtle (.ttl), NTriples (.nt), NQuads (.nq), TriG (.trig), and RDF/XML (.rdf/.xml).

Project overview from the community descriptor.

Documentation and resources

Project and build details
Repository
nonodename/duck_rdf
License
MIT
Maintainers
nonodename
Written in
C++
Build source
badeb50fd12f
Build definition
Community descriptor