Files

6.4 KiB

Getting Started with Triplet Store

This notebook introduces the Semantica Triplet Store module, which allows you to store and query knowledge graph data using industry-standard RDF Triple Stores.

What is a Triplet Store?

A Triplet Store (or RDF Store) is a database optimized for storing and retrieving triples: Subject - Predicate - Object. For example: Alice (Subject) knows (Predicate) Bob (Object).

Semantica supports:

  • Blazegraph (Default, High Performance)
  • Apache Jena (Great for inference)
  • RDF4J (Standard Java framework)
In [ ]:
import sys
import os

# Add the project root to the path so we can import the local version of semantica
sys.path.append(os.path.abspath('../../'))

# If running in Google Colab, uncomment the following line to install dependencies
# !pip install -q semantica
In [ ]:
# Import the TripletStore class
from semantica.triplet_store import TripletStore
from semantica.semantic_extract.triplet_extractor import Triplet

1. Connecting to a Store

To use the Triplet Store, you need a backend running. In this example, we assume a Blazegraph instance is running locally.

Note: If you don't have a store running, the code below shows how you would connect.

In [ ]:
# Connect to a Blazegraph instance
# You can also use backend="jena" or backend="rdf4j"
store = TripletStore(
    backend="blazegraph",
    endpoint="http://localhost:9999/blazegraph"
)

# Check connection status
if hasattr(store._store_backend, 'connected') and store._store_backend.connected:
    print(f"Successfully connected to {store.backend_type} store at {store.endpoint}")
else:
    print(f"Warning: Could not connect to {store.backend_type} at {store.endpoint}")
    print("Operations requiring a live store connection will be skipped or fail.")

2. Creating Triplets

We use the Triplet class to define our data.

In [ ]:
# Define a single triplet
triplet1 = Triplet(
    subject="http://example.org/Alice",
    predicate="http://xmlns.com/foaf/0.1/knows",
    object="http://example.org/Bob"
)

print(f"Created triplet: {triplet1.subject} -> {triplet1.predicate} -> {triplet1.object}")

3. Adding Data

You can add triplets one by one or in bulk.

In [ ]:
from semantica.utils.exceptions import ProcessingError

try:
    # Add a single triplet
    store.add_triplet(triplet1)
    print("Added single triplet successfully.")

    # Create more triplets
    triplets = [
        Triplet(
            subject="http://example.org/Bob",
            predicate="http://xmlns.com/foaf/0.1/knows",
            object="http://example.org/Charlie"
        ),
        Triplet(
            subject="http://example.org/Charlie",
            predicate="http://xmlns.com/foaf/0.1/knows",
            object="http://example.org/David"
        )
    ]

    # Bulk add
    store.add_triplets(triplets)
    print("Added bulk triplets successfully.")

except ProcessingError as e:
    print(f"Operation skipped: {e}")
except Exception as e:
    print(f"An error occurred: {e}")

4. Querying Data (SPARQL)

Use SPARQL queries to retrieve data from the store.

In [ ]:
# Simple query to get all triplets (limited to 10)
query = """
SELECT ?s ?p ?o
WHERE {
  ?s ?p ?o
}
LIMIT 10
"""

try:
    results = store.execute_query(query)
    print("Query Results:", results)
except ProcessingError as e:
    print(f"Query skipped: {e}")
except Exception as e:
    print(f"An error occurred: {e}")

5. Deleting Data

Remove triplets when they are no longer needed.

In [ ]:
store.delete_triplet(triplet1)
print("Deleted triplet1")

Next Steps

Check out the Advanced Triplet Store guide in the cookbook/advanced folder for more complex operations like bulk loading optimization, transactions, and advanced SPARQL features.