How to Export Knowledge Graphs from Semantica to RDF, Parquet, Cypher, or JSON-LD

Semantica provides dedicated exporter classes—ParquetExporter, RDFExporter, and LPGExporter—that serialize knowledge graphs to Apache Parquet, RDF/OWL (Turtle, JSON-LD, N-Triples), and Cypher formats through methods like export_knowledge_graph() and export().

The semantica-agi/semantica repository includes a modular export layer in semantica/export/ that converts internal knowledge graph structures—standard Python dictionaries containing entities and relationships—into validated, industry-standard interchange formats. Each exporter enforces strict schemas, handles namespace mapping, and integrates with the global ProgressTracker for long-running operations.

Exporting Knowledge Graphs to Apache Parquet

The ParquetExporter class in semantica/export/parquet_exporter.py serializes entities and relationships into columnar Arrow tables using explicit PyArrow schemas.

Schema Architecture

The exporter defines two immutable schemas that eliminate runtime inference:


# semantica/export/parquet_exporter.py

ENTITY_SCHEMA = pa.schema([
    pa.field("id", pa.string(), nullable=False),
    pa.field("text", pa.string(), nullable=True),
    pa.field("metadata", pa.struct([
        pa.field("keys", pa.list_(pa.string())),
        pa.field("values", pa.list_(pa.string())),
    ]), nullable=True),
])

Relationships follow a similar RELATIONSHIP_SCHEMA structure. The export_knowledge_graph() method normalizes input dictionaries—mapping fields like label to text—before writing via pa.Table.from_pylist(data, schema=schema) and pq.write_table().

Usage

from semantica.export import ParquetExporter

exporter = ParquetExporter(compression="snappy")
exporter.export_knowledge_graph(kg, "output/knowledge_graph")

This produces two files: knowledge_graph_entities.parquet and knowledge_graph_relationships.parquet (see ParquetExporter.export_knowledge_graph lines 61‑87).

Serializing to RDF, OWL, and JSON-LD

The RDFExporter in semantica/export/rdf_exporter.py handles semantic web serialization through the NamespaceManager and RDFSerializer classes.

Namespace Management and Validation

The NamespaceManager (lines 41‑84) resolves prefix conflicts and maintains URI mappings for standard vocabularies including rdf, xsd, and semantica. The RDFValidator performs syntax and consistency checks before serialization.

Format Dispatch

The export_to_rdf() method supports four output formats:

if format == "turtle":
    result = self.serializer.serialize_to_turtle(...)
elif format == "rdfxml":
    result = self.serializer.serialize_to_rdfxml(...)
elif format == "jsonld":
    result = self.serializer.serialize_to_jsonld(...)
elif format == "ntriples":
    result = self.serializer.serialize_to_ntriples(...)

Passing format="turtle" with include_temporal=True emits OWL‑Time compatible triples (lines 95‑104), enabling OWL ontology support by including temporal bounds and standard OWL prefixes.

RDF Export Examples

from semantica.export import RDFExporter

rdf_exporter = RDFExporter()

# OWL-compatible Turtle

rdf_exporter.export(kg, "ontology.ttl", format="turtle", include_temporal=True)

# Linked Data format

rdf_exporter.export(kg, "graph.jsonld", format="jsonld")

Generating Cypher Scripts for Neo4j and Memgraph

Cypher export uses the LPG (Labeled Property Graph) serializer accessible via the unified export_knowledge_graph() helper in semantica/export/methods.py.

Implementation Details

When invoked with format="cypher", the dispatcher routes to the LPGExporter class, which generates CREATE statements for nodes and relationships. The conversion relies on literal serialization helpers in semantica/graph_store/age_store.py:

  • _value_to_cypher_literal() – Converts Python scalars to Cypher syntax (lines 113‑138)
  • _props_to_cypher_literal() – Maps property dictionaries to Cypher map literals

The resulting .cypher files contain statements compatible with neo4j-admin import and Memgraph loaders.

Cypher Export Usage

from semantica.export import export_knowledge_graph

# Generate Cypher script

export_knowledge_graph(kg, "output/graph.cypher", format="cypher")

Direct instantiation of the low-level exporter is also available:

from semantica.export.methods import LPGExporter

lpg = LPGExporter()
lpg.export_knowledge_graph(kg, "output/graph.cypher")

Unified Export Interface

The export_knowledge_graph() function in semantica/export/methods.py serves as a high-level dispatcher. It accepts a format parameter—"parquet", "rdf", "cypher", "jsonld", "turtle", or "ntriples"—and delegates to the appropriate exporter class while handling progress tracking and optional provenance logging via semantica/export/export_provenance.py.

Summary

  • Parquet – Use ParquetExporter with explicit ENTITY_SCHEMA and RELATIONSHIP_SCHEMA for Snappy-compressed columnar storage.
  • RDF/OWL – Use RDFExporter.export() with format="turtle" and include_temporal=True for OWL-compatible output; supports JSON-LD, RDF/XML, and N-Triples via the serializer dispatcher.
  • Cypher – Use the export_knowledge_graph() helper with format="cypher" to generate Neo4j-compatible CREATE statements, leveraging literal conversion utilities from age_store.py.
  • Validation – All exporters validate input graphs before serialization, ensuring ID presence and type consistency.

Frequently Asked Questions

Which exporter supports OWL ontology serialization?

The RDFExporter class supports OWL through Turtle output (format="turtle") combined with the include_temporal=True flag. This emits OWL‑Time namespace triples and standard OWL prefixes; you can further extend OWL compatibility by adding owl:Class or owl:equivalentClass assertions directly to your entity dictionaries before export.

How do I optimize Parquet exports for large knowledge graphs?

Instantiate ParquetExporter with compression="snappy" or "gzip" to reduce file size. The explicit Arrow schemas (ENTITY_SCHEMA and RELATIONSHIP_SCHEMA) prevent costly type inference on large datasets, and the exporter writes to disk using pq.write_table() with zero-copy Arrow buffers (see semantica/export/parquet_exporter.py lines 81‑87).

Can I export the same graph to multiple formats in one operation?

While no single method writes multiple formats simultaneously, you can chain calls to export_knowledge_graph() with different format parameters or instantiate specific exporters (ParquetExporter, RDFExporter) sequentially. Each export runs independently with its own validation pass.

Is the generated Cypher compatible with Neo4j 5.x and Memgraph?

Yes. The LPGExporter generates standard openCypher CREATE statements using literal escaping helpers (_value_to_cypher_literal in semantica/graph_store/age_store.py). The output contains no deprecated syntax and loads directly into Neo4j 5.x, Memgraph, and Amazon Neptune’s Cypher endpoints.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →