Storage Operations

AWS Neptune

Learn how to save your knowledge graph to an AWS Neptune database.

The Perseus SDK allows you to generate Cypher statements that can be executed in any openCypher-compatible database, including AWS Neptune. This guide shows you how to run these statements to populate your Neptune instance.

Configuration

The boto3 library, used in this example, will automatically search for AWS credentials in your environment. For more information on configuring your AWS credentials, refer to the official boto3 documentation.

You will need to have boto3 installed: pip install boto3.

Saving the Graph

After building a KnowledgeGraph, you can use the to_cypher_statements() method to get a list of Cypher queries. You can then iterate through these statements and execute them on your Neptune instance.

For more details on the to_cypher_statements method, refer to the Python API.

import boto3
import perseus_client

knowledge_graphs = perseus_client.build_graph(
    file_paths=["assets/sample.txt"],
)

NEPTUNE_HOST = "your-neptune-instance-endpoint"
NEPTUNE_PORT = 8182
AWS_REGION = "your-aws-region"

neptune_client = boto3.client(
    'neptunedata',
    endpoint_url=f'https://{NEPTUNE_HOST}:{NEPTUNE_PORT}',
    region_name=AWS_REGION
)

for kg in knowledge_graphs:
    statements = kg.to_cypher_statements()

    for statement in statements:
        neptune_client.execute_open_cypher_query(
            openCypherQuery=statement
        )