mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-09-15 04:00:33 +00:00
- Add new Graph_Store.ipynb introduction notebook - Update Advanced_Graph_Analytics.ipynb with graph store persistence - Update Fraud_Detection.ipynb with graph database storage - Update Transaction_Network_Analysis.ipynb with blockchain graph storage - Update Criminal_Network_Analysis.ipynb with criminal network persistence - Update Welcome_to_Semantica.ipynb with Graph Store module documentation - Update docs/cookbook.md, docs/examples.md, docs/CodeExamples.md - Sync all notebooks to docs/cookbook directory
370 lines
11 KiB
Plaintext
370 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Graph Store\n",
|
|
"\n",
|
|
"## Overview\n",
|
|
"\n",
|
|
"This notebook demonstrates how to store and query property graphs using Semantica's graph store modules. You'll learn to use `GraphStore` with multiple backends including Neo4j, KuzuDB, and FalkorDB.\n",
|
|
"\n",
|
|
"### Learning Objectives\n",
|
|
"\n",
|
|
"- Use `GraphStore` to store nodes and relationships\n",
|
|
"- Execute Cypher queries for graph retrieval\n",
|
|
"- Use graph analytics (shortest path, neighbors)\n",
|
|
"- Compare different graph database backends\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Prerequisites\n",
|
|
"\n",
|
|
"Install the required graph database client:\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"# For Neo4j\n",
|
|
"pip install neo4j\n",
|
|
"\n",
|
|
"# For KuzuDB (embedded - no server required)\n",
|
|
"pip install kuzu\n",
|
|
"\n",
|
|
"# For FalkorDB\n",
|
|
"pip install falkordb\n",
|
|
"# And run: docker run -p 6379:6379 -p 3000:3000 -it --rm falkordb/falkordb\n",
|
|
"```\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 1: Initialize Graph Store\n",
|
|
"\n",
|
|
"Create a graph store with your preferred backend.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from semantica.graph_store import GraphStore\n",
|
|
"\n",
|
|
"# Option 1: Neo4j (requires Neo4j server)\n",
|
|
"# store = GraphStore(\n",
|
|
"# backend=\"neo4j\",\n",
|
|
"# uri=\"bolt://localhost:7687\",\n",
|
|
"# user=\"neo4j\",\n",
|
|
"# password=\"password\"\n",
|
|
"# )\n",
|
|
"\n",
|
|
"# Option 2: KuzuDB (embedded - no server required)\n",
|
|
"store = GraphStore(\n",
|
|
" backend=\"kuzu\",\n",
|
|
" database_path=\"./demo_graph_db\"\n",
|
|
")\n",
|
|
"\n",
|
|
"# Option 3: FalkorDB (requires Redis/FalkorDB server)\n",
|
|
"# store = GraphStore(\n",
|
|
"# backend=\"falkordb\",\n",
|
|
"# host=\"localhost\",\n",
|
|
"# port=6379,\n",
|
|
"# graph_name=\"demo_graph\"\n",
|
|
"# )\n",
|
|
"\n",
|
|
"# Connect to the database\n",
|
|
"store.connect()\n",
|
|
"print(\"Connected to graph store!\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 2: Create Nodes\n",
|
|
"\n",
|
|
"Create nodes with labels and properties.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Create individual nodes\n",
|
|
"apple = store.create_node(\n",
|
|
" labels=[\"Company\"],\n",
|
|
" properties={\"name\": \"Apple Inc.\", \"founded\": 1976, \"industry\": \"Technology\"}\n",
|
|
")\n",
|
|
"print(f\"Created company node: {apple}\")\n",
|
|
"\n",
|
|
"tim_cook = store.create_node(\n",
|
|
" labels=[\"Person\"],\n",
|
|
" properties={\"name\": \"Tim Cook\", \"title\": \"CEO\", \"age\": 63}\n",
|
|
")\n",
|
|
"print(f\"Created person node: {tim_cook}\")\n",
|
|
"\n",
|
|
"cupertino = store.create_node(\n",
|
|
" labels=[\"Location\"],\n",
|
|
" properties={\"name\": \"Cupertino\", \"state\": \"California\", \"country\": \"USA\"}\n",
|
|
")\n",
|
|
"print(f\"Created location node: {cupertino}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Create multiple nodes in batch\n",
|
|
"other_companies = store.create_nodes([\n",
|
|
" {\"labels\": [\"Company\"], \"properties\": {\"name\": \"Microsoft\", \"founded\": 1975}},\n",
|
|
" {\"labels\": [\"Company\"], \"properties\": {\"name\": \"Google\", \"founded\": 1998}},\n",
|
|
" {\"labels\": [\"Company\"], \"properties\": {\"name\": \"Amazon\", \"founded\": 1994}},\n",
|
|
"])\n",
|
|
"print(f\"Created {len(other_companies)} company nodes in batch\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 3: Create Relationships\n",
|
|
"\n",
|
|
"Create relationships between nodes.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Create relationships\n",
|
|
"ceo_rel = store.create_relationship(\n",
|
|
" start_node_id=tim_cook[\"id\"],\n",
|
|
" end_node_id=apple[\"id\"],\n",
|
|
" rel_type=\"CEO_OF\",\n",
|
|
" properties={\"since\": 2011}\n",
|
|
")\n",
|
|
"print(f\"Created CEO relationship: {ceo_rel}\")\n",
|
|
"\n",
|
|
"location_rel = store.create_relationship(\n",
|
|
" start_node_id=apple[\"id\"],\n",
|
|
" end_node_id=cupertino[\"id\"],\n",
|
|
" rel_type=\"HEADQUARTERED_IN\",\n",
|
|
" properties={\"since\": 1977}\n",
|
|
")\n",
|
|
"print(f\"Created location relationship: {location_rel}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 4: Query Nodes and Relationships\n",
|
|
"\n",
|
|
"Retrieve nodes and relationships from the graph.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Get all Company nodes\n",
|
|
"companies = store.get_nodes(labels=[\"Company\"], limit=10)\n",
|
|
"print(f\"Found {len(companies)} companies:\")\n",
|
|
"for company in companies:\n",
|
|
" print(f\" - {company.get('properties', {}).get('name', 'Unknown')}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Get relationships for a node\n",
|
|
"relationships = store.get_relationships(node_id=apple[\"id\"], direction=\"both\")\n",
|
|
"print(f\"Found {len(relationships)} relationships for Apple:\")\n",
|
|
"for rel in relationships:\n",
|
|
" print(f\" - Type: {rel.get('type')}, Properties: {rel.get('properties')}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 5: Execute Cypher Queries\n",
|
|
"\n",
|
|
"Use Cypher queries for complex graph operations.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Execute a Cypher query\n",
|
|
"results = store.execute_query(\"\"\"\n",
|
|
" MATCH (p:Person)-[r:CEO_OF]->(c:Company)\n",
|
|
" RETURN p.name as person, c.name as company, r.since as since\n",
|
|
"\"\"\")\n",
|
|
"\n",
|
|
"print(\"CEO relationships:\")\n",
|
|
"for record in results.get(\"records\", []):\n",
|
|
" print(f\" {record}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Parameterized query\n",
|
|
"results = store.execute_query(\n",
|
|
" \"MATCH (c:Company) WHERE c.founded > $year RETURN c.name, c.founded\",\n",
|
|
" parameters={\"year\": 1990}\n",
|
|
")\n",
|
|
"\n",
|
|
"print(\"Companies founded after 1990:\")\n",
|
|
"for record in results.get(\"records\", []):\n",
|
|
" print(f\" {record}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 6: Graph Analytics\n",
|
|
"\n",
|
|
"Use built-in graph analytics functions.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Get neighbors of a node\n",
|
|
"neighbors = store.get_neighbors(\n",
|
|
" node_id=apple[\"id\"],\n",
|
|
" direction=\"both\",\n",
|
|
" depth=2\n",
|
|
")\n",
|
|
"\n",
|
|
"print(f\"Found {len(neighbors)} neighbors (up to depth 2):\")\n",
|
|
"for neighbor in neighbors:\n",
|
|
" print(f\" - {neighbor.get('properties', {}).get('name', 'Unknown')}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Find shortest path (if nodes are connected)\n",
|
|
"path = store.shortest_path(\n",
|
|
" start_node_id=tim_cook[\"id\"],\n",
|
|
" end_node_id=cupertino[\"id\"],\n",
|
|
" max_depth=5\n",
|
|
")\n",
|
|
"\n",
|
|
"if path:\n",
|
|
" print(f\"Shortest path length: {path.get('length')}\")\n",
|
|
" print(f\"Nodes in path: {len(path.get('nodes', []))}\")\n",
|
|
"else:\n",
|
|
" print(\"No path found\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 7: Get Graph Statistics\n",
|
|
"\n",
|
|
"Get statistics about the graph.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"stats = store.get_stats()\n",
|
|
"\n",
|
|
"print(\"Graph Statistics:\")\n",
|
|
"print(f\" Node count: {stats.get('node_count', 'N/A')}\")\n",
|
|
"print(f\" Relationship count: {stats.get('relationship_count', 'N/A')}\")\n",
|
|
"print(f\" Label counts: {stats.get('label_counts', {})}\")\n",
|
|
"print(f\" Relationship types: {stats.get('relationship_type_counts', {})}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 8: Clean Up\n",
|
|
"\n",
|
|
"Close the connection when done.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Close the connection\n",
|
|
"store.close()\n",
|
|
"print(\"Connection closed.\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Summary\n",
|
|
"\n",
|
|
"You've learned how to use graph stores:\n",
|
|
"\n",
|
|
"- **GraphStore**: Unified interface for property graph databases\n",
|
|
"- **Multiple Backends**: Neo4j, KuzuDB, FalkorDB support\n",
|
|
"- **Node Operations**: Create, read, update, delete nodes\n",
|
|
"- **Relationship Operations**: Create and query relationships\n",
|
|
"- **Cypher Queries**: Execute powerful graph queries\n",
|
|
"- **Graph Analytics**: Shortest path, neighbors, centrality\n",
|
|
"\n",
|
|
"### Backend Comparison\n",
|
|
"\n",
|
|
"| Backend | Best For | Deployment |\n",
|
|
"|---------|----------|------------|\n",
|
|
"| **Neo4j** | Enterprise, full features | Server/Cloud |\n",
|
|
"| **KuzuDB** | Analytics, embedded | Embedded (no server) |\n",
|
|
"| **FalkorDB** | LLM apps, real-time | Redis-based |\n",
|
|
"\n",
|
|
"Next: Learn how to visualize graphs in the Visualization notebook.\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|