rdf

RDF lesen und schreiben, mit SPARQL abfragen

Maintainer: nonodename

Installation und Laden

INSTALL rdf FROM community;
LOAD rdf;

Beispiel

-- 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'
);

Über rdf

Die Erweiterung duck_rdf ermöglicht DuckDB, RDF-Daten (Resource Description Framework) direkt zu lesen und zu schreiben, unter Verwendung der Bibliothek SERD für Parsing und Serialisierung.

Unterstützte Formate

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

Schreiben: NTriples, Turtle, NQuads (über R2RML-Mapping).

RDF lesen

read_rdf() gibt sechs Spalten zurück: subject, predicate, object (immer gefüllt) sowie graph, language_tag, datatype (nullable). Sie akzeptiert einen Dateipfad oder ein Glob-Muster; mehrere passende Dateien werden parallel gescannt. .gz- und .zst-komprimierte Dateien werden unterstützt (Hinweis: Sie müssen die parquet-Erweiterung laden, damit die libzstd-Bibliothek geladen wird). Das RDF-Format wird anhand der Dateiendung automatisch erkannt, kann aber mit dem Parameter file_type überschrieben werden.

SELECT subject, predicate FROM read_rdf('data.ttl');
SELECT COUNT(*) FROM read_rdf('data/shards/*.nt');
SELECT * FROM read_rdf('data/*.dat', file_type = 'ttl', strict_parsing = false);

Optionale Parameter:

Parameter Default Description
strict_parsing true Set to false to allow malformed URIs
prefix_expansion false Expand CURIE-form URIs to full URIs (Turtle/TriG only)
file_type auto-detected Override format: ttl, nt, nq, trig, rdf/xml

pivot_rdf() nimmt dasselbe Pfad-/Glob-Argument wie read_rdf() entgegen und gibt eine pivotierte Tabelle zurück, eine Spalte pro Prädikat, mindestens eine Zeile pro Subjekt. (Um beliebige Dateigrößen zu verarbeiten, können Subjekte wiederholt werden, wenn sie außer der Reihenfolge auftreten.) Ein Pivot ist auch in der SQL-Domäne möglich, unterliegt aber Speichergrenzen; diese Funktion vermeidet das durch zwei Durchläufe über das RDF, wobei der erste die Datenform mit profile_rdf() profiliert.

Die Funktion read_sparql(endpoint, query) sendet eine SPARQL-SELECT-Abfrage an einen entfernten Endpunkt und gibt die Ergebnismenge als DuckDB-Tabelle zurück. Spaltennamen werden aus den SPARQL-Variablennamen abgeleitet; alle Spalten sind VARCHAR. Ungebundene Variablen werden als leere Zeichenketten zurückgegeben.

-- Count number of humans in wikidata
SELECT * FROM read_sparql(
'https://query.wikidata.org/sparql',
'SELECT (COUNT(*) AS ?count) WHERE { ?item wdt:P31 wd:Q5 .}'
);

Sie können auch eine SPARQL-1.1-Abfrage execute_sparql(query, mapping) gegen DuckDB-Tabellen ausführen und dabei eine R2RML-Mapping-Datei verwenden, um die Abbildung zwischen relationalen Daten und RDF zu definieren. Die SPARQL- Abfrage wird zur Abfragezeit mit einem regelbasierten Optimizer nach SQL umgewandelt. Die Funktion gibt die Ergebnismenge als DuckDB-Tabelle zurück. Ein guter Teil von SPARQL wird unterstützt. Weitere Informationen finden Sie in der Dokumentation.

Um die Abfrage zu sehen, die ausgeführt würde, verwenden Sie sparql_to_sql(sparql, mapping).

RDF schreiben (R2RML)

Schreiben Sie RDF mit R2RML-Mapping-Dateien über DuckDBs COPY TO- Syntax. Zwei Modi werden unterstützt:

Inside-out-Modus — DuckDB steuert die Abfrage; das Mapping hat kein rr:logicalTable:

COPY (SELECT empno, ename, deptno FROM emp)
TO 'output.nt' (FORMAT r2rml, mapping 'mapping.ttl');

Vollständiger R2RML-Modus — das Mapping definiert eigene Abfragen:

COPY (SELECT 1) TO 'output.nt' (FORMAT r2rml, mapping 'mapping.ttl');

Schreiboptionen:

Option Required Default Description
mapping Yes Path to R2RML mapping file (.ttl)
rdf_format No ntriples Output format: ntriples, turtle, or nquads
ignore_non_fatal_errors No true Raise an exception on the first parse error when false

Validierungshelfer

SELECT is_valid_r2rml('mapping.ttl'); -- validate an R2RML mapping file
SELECT can_call_inside_out('mapping.ttl'); -- check if inside-out mode is supported

Hinzugefügte Funktionen

function_name function_type description comment examples
can_call_inside_out scalar Return true if the given R2RML or YARRML mapping file can be executed in inside-out mode, where DuckDB runs the SQL query and the extension maps each output row to RDF triples. NULL [SELECT can_call_inside_out(‘mapping.ttl’)]
execute_sparql table Translate a SPARQL SELECT or ASK query into SQL via an R2RML or YARRRML mapping (like sparql_to_sql), then run it directly as a table. Unlike calling sparql_to_sql() and pasting the result into a second query, execute_sparql() splices the translated SQL into this query’s own plan before optimization, so filters, projections, and joins written around the call are planned together with it. Same mapping requirements and error messages as sparql_to_sql(). NULL [SELECT * FROM execute_sparql(‘SELECT ?e ?name WHERE { ?e http://example.com/ns#name ?name }’, ‘mapping.ttl’)]
is_valid_r2rml scalar Return true if the given file is a syntactically valid R2RML or YARRML mapping document. NULL [SELECT is_valid_r2rml(‘mapping.yml’)]
pivot_rdf table Read RDF triples and pivot them into a wide table where each distinct predicate becomes a column and subjects become row identifiers. Glob patterns and lists of file paths are supported. NULL [SELECT * FROM pivot_rdf(‘data.ttl’), SELECT * FROM pivot_rdf(‘data.nt’, prefix_expansion=true), SELECT * FROM pivot_rdf([‘a.ttl’, ‘b.ttl’])]
profile_rdf table Profile one or more RDF files and return a predicate-level statistical summary including value counts, datatypes, and cardinalities. Glob patterns and lists of file paths are supported. NULL [SELECT * FROM profile_rdf(‘data.nt’), SELECT * FROM profile_rdf(‘data/*.ttl’, strict_parsing=false), SELECT * FROM profile_rdf([‘a.nt’, ‘b.nt’])]
read_rdf table Read RDF triples from one or more files (Turtle, NTriples, NQuads, TriG, or RDF/XML) into a table with columns graph, subject, predicate, object, object_datatype, and object_lang. Glob patterns and lists of file paths are supported. NULL [SELECT * FROM read_rdf(‘data.nt’), SELECT subject, predicate, object FROM read_rdf(‘*.ttl’), SELECT * FROM read_rdf([‘a.nt’, ‘b.nt’]), SELECT * FROM read_rdf(‘data.rdf’, file_type=‘rdf’, strict_parsing=false)]
read_rdf_prefixes table Read the namespace prefix declarations from one or more RDF files. Returns prefix (local name), uri (namespace URI), and is_base (true for @base declarations) columns. Glob patterns and lists of file paths are supported. NULL [SELECT * FROM read_rdf_prefixes(‘data.ttl’), SELECT prefix, uri FROM read_rdf_prefixes(‘*.ttl’), SELECT * FROM read_rdf_prefixes([‘a.ttl’, ‘b.ttl’])]
read_sparql table Execute a SPARQL SELECT query against a remote endpoint and return the results as a table. Each SPARQL variable becomes a VARCHAR column. Not available in WebAssembly builds. NULL [SELECT * FROM read_sparql(‘https://dbpedia.org/sparql’, ‘SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 10’)]
sparql_to_sql scalar Translate a SPARQL SELECT or ASK query into an equivalent SQL query, using an R2RML or YARRRML mapping file in reverse. The mapping must be a full R2RML mapping (every TriplesMap has an rr:logicalTable or YARRRML ‘sources’ entry) - inside-out-only mappings are not accepted. Throws a detailed error naming the mapping file, the SPARQL syntax problem, or the unsupported SPARQL construct on failure. Currently only the ‘duckdb’ SQL dialect is supported. NULL [SELECT sparql_to_sql(‘SELECT ?e ?name WHERE { ?e http://example.com/ns#name ?name }’, ‘mapping.ttl’)]

Überladene Funktionen

Diese Erweiterung fügt keine Funktionsüberladungen hinzu.

Hinzugefügte Typen

Diese Erweiterung fügt keine Typen hinzu.

Hinzugefügte Einstellungen

Diese Erweiterung fügt keine Einstellungen hinzu.