Skip to main content
SAP HANA Cloud Knowledge Graph is a fully integrated knowledge graph solution within the SAP HANA Cloud database.

Setup & Installation

Prerequisites: To use SAP HANA Knowledge Graph Engine with LangChain, install the @sap/hana-langchain package along with its peer dependencies:
Then, import the HanaRdfGraph Class.

Creating a HanaRdfGraph instance

The constructor requires:
  • connection: an active @sap/hana-client Connection instance
  • graphUri: the named graph (or "DEFAULT") where your RDF data lives
  • One of:
    1. ontologyQuery: a SPARQL CONSTRUCT to extract schema triples
    2. ontologyUri: a hosted ontology graph URI
    3. ontologyLocalFile + ontologyLocalFileFormat: a local Turtle/RDF file
    4. autoExtractOntology: true (not recommended for production—see note)
graphUri vs. Ontology
  • graphUri: The named graph in your SAP HANA Cloud instance that contains your instance data (sometimes 100k+ triples). If no graphUri, "" or "DEFAULT" is provided, the default graph is used.
  • Ontology: a lean schema (typically ~50-100 triples) describing classes, properties, domains, ranges, labels, comments, and subclass relationships. The ontology guides SPARQL generation and result interpretation.

Creating a graph instance with DEFAULT graph

More info on the DEFAULT graph can be found at DEFAULT Graph and Named Graphs.

Creating a graph instance with a graph_uri

Creating a graph instance with a remote ontology_uri

Creating a graph instance with a custom ontology_query

Use a custom CONSTRUCT query to selectively extract schema triples.

Load ontology from a local RDF file

Supported RDF formats: Turtle, N-Triples, Notation-3, Trig, N-Quads.

Auto extraction of ontology

(auto_extract_ontology=True): Infer schema information directly from your instance data.
Note: Auto-extraction is not recommended for production—it omits important triples like rdfs:label, rdfs:comment, and rdfs:subClassOf in general.

Executing SPARQL Queries

You can use the query() method to execute arbitrary SPARQL queries (SELECT, ASK, CONSTRUCT, etc.) on the data graph. The function has the following parameters
  • query: the SPARQL query string.
  • content_type: the response format for the output (Default is CSV)
Please use the following strings for the respective formats.
  • CSV: "sparql-results+xml"
  • JSON: "sparql-results+json"
  • XML: "sparql-results+csv"
  • TSV: "sparql-results+tsv"
Note: CONSTRUCT and ASK Queries return turtle and boolean formats respectively.
Let us insert some data into the Puppets graph.
Then, we create a graph instance for the Puppets graph.
The given query lists all tuples in the Puppets graph.