PyAnzo
PyAnzo is a Python library for programmatic interaction with Anzo, a knowledge graph platform developed by Cambridge Semantics. Anzo enables the ingestion, transformation, storage, exploration, and analysis of diverse data types using open W3C standards such as Web Ontology Language (OWL), Resource Description Framework (RDF), and SPARQL. The library is currently at version 3.3.12 and receives regular, primarily patch-level, updates.
Common errors
-
AnzoClientException: Could not connect to Anzo server at [hostname]:[port]
cause The PyAnzo client could not establish a network connection to the specified Anzo server. This might be due to an incorrect hostname/IP, wrong port, firewall blocking the connection, or the Anzo server not running or being inaccessible.fixCheck the `ANZO_HOST` and `ANZO_PORT` values. Ping the server's hostname/IP from the client machine. Verify firewall rules. Confirm the Anzo server is running and accessible. -
AnzoClientException: Invalid credentials provided for Anzo server.
cause The username or password provided to `AnzoClient` is incorrect, or the user lacks the necessary permissions to connect or perform actions.fixDouble-check the `ANZO_USER` and `ANZO_PASS` values. Ensure the user exists and has the required permissions on the Anzo server. -
AnzoClientException: Graphmart [graphmart_uri] not found or not activated.
cause The specified graphmart URI does not exist on the connected Anzo server, or it has not been activated and made ready for querying.fixVerify the `ANZO_GRAPH MART_URI` is correct by checking Anzo's administrative interface. Ensure the graphmart is activated and has successfully loaded its data.
Warnings
- gotcha Anzo server connectivity issues are common. Ensure the correct host, port, and credentials are provided, and that the Anzo server is reachable from where PyAnzo is executed. Default ports might vary (e.g., 443 for HTTPS, 80 for HTTP).
- gotcha Incorrect Graphmart URIs or non-activated graphmarts will lead to query failures. The URI must precisely match an existing and activated graphmart on the Anzo server.
- gotcha SPARQL queries are case-insensitive for keywords but sensitive for URIs and literals. Syntax errors in SPARQL can cause query failures.
Install
-
pip install pyanzo
Imports
- AnzoClient
from pyanzo import AnzoClient
Quickstart
import os
from pyanzo import AnzoClient
ANZO_HOST = os.environ.get('ANZO_HOST', 'your_anzo_server.com')
ANZO_PORT = int(os.environ.get('ANZO_PORT', '443'))
ANZO_USER = os.environ.get('ANZO_USER', 'your_username')
ANZO_PASS = os.environ.get('ANZO_PASS', 'your_password')
ANZO_GRAPH MART_URI = os.environ.get('ANZO_GRAPH MART_URI', 'http://cambridgesemantics.com/anzo/graphmart/example')
# Basic SPARQL query example
SPARQL_QUERY = """
SELECT ?s ?p ?o WHERE {
?s ?p ?o .
}
LIMIT 10
"""
try:
client = AnzoClient(ANZO_HOST, ANZO_USER, ANZO_PASS, anzo_port=ANZO_PORT)
print(f"Successfully connected to Anzo at {ANZO_HOST}:{ANZO_PORT}")
print(f"Querying graphmart: {ANZO_GRAPH MART_URI}")
results = client.query_graphmart_SELECT(ANZO_GRAPH MART_URI, SPARQL_QUERY)
print("Query Results:")
for row in results:
print(row)
except Exception as e:
print(f"An error occurred: {e}")