A Claude-written graph database in Rust. Use at your own risk.
at main 472 lines 12 kB view raw view rendered
1# GigaBrain CLI Examples 2 3This document provides practical examples of using the GigaBrain CLI for various common tasks. 4 5## Getting Started 6 7### Starting the CLI 8```bash 9# Build the CLI 10cargo build --release --bin gigabrain-cli 11 12# Start interactive REPL 13./target/release/gigabrain-cli 14 15# Or use shorter commands if in PATH 16gigabrain-cli 17``` 18 19## Basic Graph Operations 20 21### Creating Nodes and Relationships 22 23```cypher 24# Create person nodes 25gigabrain> CREATE (alice:Person {name: 'Alice', age: 30, email: 'alice@example.com'}) 26gigabrain> CREATE (bob:Person {name: 'Bob', age: 25, email: 'bob@example.com'}) 27gigabrain> CREATE (charlie:Person {name: 'Charlie', age: 35, email: 'charlie@example.com'}) 28 29# Create a company 30gigabrain> CREATE (techcorp:Company {name: 'TechCorp', founded: 2010, industry: 'Technology'}) 31 32# Create relationships 33gigabrain> MATCH (alice:Person {name: 'Alice'}), (bob:Person {name: 'Bob'}) 34 -> CREATE (alice)-[:KNOWS {since: 2020}]->(bob) 35 36gigabrain> MATCH (alice:Person {name: 'Alice'}), (techcorp:Company {name: 'TechCorp'}) 37 -> CREATE (alice)-[:WORKS_FOR {position: 'Senior Developer', since: 2022}]->(techcorp) 38 39gigabrain> MATCH (bob:Person {name: 'Bob'}), (techcorp:Company {name: 'TechCorp'}) 40 -> CREATE (bob)-[:WORKS_FOR {position: 'Junior Developer', since: 2023}]->(techcorp) 41``` 42 43### Querying Data 44 45```cypher 46# Find all people 47gigabrain> MATCH (p:Person) RETURN p.name, p.age, p.email 48 49# Find relationships 50gigabrain> MATCH (p:Person)-[r:KNOWS]->(friend:Person) 51 -> RETURN p.name as person, friend.name as friend, r.since as since 52 53# Find coworkers 54gigabrain> MATCH (p1:Person)-[:WORKS_FOR]->(company:Company)<-[:WORKS_FOR]-(p2:Person) 55 -> WHERE p1 <> p2 56 -> RETURN p1.name as person1, p2.name as person2, company.name as company 57 58# Complex query with aggregation 59gigabrain> MATCH (p:Person)-[:WORKS_FOR]->(c:Company) 60 -> RETURN c.name as company, count(p) as employees 61 -> ORDER BY employees DESC 62``` 63 64## Administrative Tasks 65 66### Graph Statistics and Analysis 67 68```bash 69# Show basic statistics 70gigabrain> :stats 71 72# Analyze graph structure 73gigabrain> analyze structure 74 75# Analyze connectivity 76gigabrain> analyze connectivity 77 78# Show schema information 79gigabrain> :show schema 80 81# Show sample nodes 82gigabrain> :show nodes 83 84# Show sample relationships 85gigabrain> :show relationships 86``` 87 88### Performance Analysis 89 90```bash 91# Run benchmark 92gigabrain> benchmark 93 94# Analyze performance characteristics 95gigabrain> analyze performance 96 97# Show memory usage 98gigabrain> memory 99 100# Profile a query 101gigabrain> profile MATCH (p:Person)-[:KNOWS*1..3]->(friend) RETURN p.name, count(friend) 102``` 103 104### Database Maintenance 105 106```bash 107# Create backup 108gigabrain> backup daily_backup_20231201.json 109 110# Optimize database 111gigabrain> optimize 112 113# Vacuum (cleanup) 114gigabrain> vacuum 115 116# Show connection info 117gigabrain> connections 118``` 119 120## Data Import/Export 121 122### JSON Export/Import 123 124```bash 125# Export graph to JSON 126gigabrain> :export graph_backup.json 127 128# Import from JSON 129gigabrain> :import graph_backup.json 130 131# Using administrative commands 132gigabrain> export-json full_export.json 133gigabrain> import-json full_export.json 134``` 135 136### CSV Export/Import 137 138```bash 139# Export people to CSV 140gigabrain> export-csv people.csv "MATCH (p:Person) RETURN p.name, p.age, p.email" 141 142# Export relationships to CSV 143gigabrain> export-csv relationships.csv "MATCH (a)-[r]->(b) RETURN a.name, type(r), b.name" 144 145# Import CSV data 146gigabrain> import-csv new_people.csv 147``` 148 149## Batch Processing 150 151### Command Files 152 153Create a file `setup_social_network.cypher`: 154```cypher 155# Social network setup script 156# Clear existing data 157MATCH (n) DETACH DELETE n; 158 159# Create people 160CREATE (alice:Person {name: 'Alice', age: 30, city: 'New York'}); 161CREATE (bob:Person {name: 'Bob', age: 25, city: 'San Francisco'}); 162CREATE (charlie:Person {name: 'Charlie', age: 35, city: 'Los Angeles'}); 163CREATE (diana:Person {name: 'Diana', age: 28, city: 'Chicago'}); 164CREATE (eve:Person {name: 'Eve', age: 32, city: 'Boston'}); 165 166# Create friendships 167MATCH (alice:Person {name: 'Alice'}), (bob:Person {name: 'Bob'}) 168CREATE (alice)-[:FRIENDS {since: 2018}]->(bob); 169 170MATCH (bob:Person {name: 'Bob'}), (charlie:Person {name: 'Charlie'}) 171CREATE (bob)-[:FRIENDS {since: 2019}]->(charlie); 172 173MATCH (charlie:Person {name: 'Charlie'}), (diana:Person {name: 'Diana'}) 174CREATE (charlie)-[:FRIENDS {since: 2020}]->(diana); 175 176MATCH (diana:Person {name: 'Diana'}), (eve:Person {name: 'Eve'}) 177CREATE (diana)-[:FRIENDS {since: 2021}]->(eve); 178 179MATCH (eve:Person {name: 'Eve'}), (alice:Person {name: 'Alice'}) 180CREATE (eve)-[:FRIENDS {since: 2017}]->(alice); 181 182# Create some interests 183CREATE (tech:Interest {name: 'Technology'}); 184CREATE (music:Interest {name: 'Music'}); 185CREATE (sports:Interest {name: 'Sports'}); 186CREATE (travel:Interest {name: 'Travel'}); 187 188# Connect people to interests 189MATCH (alice:Person {name: 'Alice'}), (tech:Interest {name: 'Technology'}) 190CREATE (alice)-[:INTERESTED_IN]->(tech); 191 192MATCH (alice:Person {name: 'Alice'}), (music:Interest {name: 'Music'}) 193CREATE (alice)-[:INTERESTED_IN]->(music); 194 195MATCH (bob:Person {name: 'Bob'}), (tech:Interest {name: 'Technology'}) 196CREATE (bob)-[:INTERESTED_IN]->(tech); 197 198MATCH (bob:Person {name: 'Bob'}), (sports:Interest {name: 'Sports'}) 199CREATE (bob)-[:INTERESTED_IN]->(sports); 200 201# Show final statistics 202MATCH (n) RETURN labels(n)[0] as type, count(n) as count; 203``` 204 205Execute the file: 206```bash 207# Run the setup script 208./gigabrain-cli --file setup_social_network.cypher 209 210# Run silently for automation 211./gigabrain-cli --file setup_social_network.cypher --silent 212``` 213 214### Analysis Queries File 215 216Create `social_analysis.cypher`: 217```cypher 218# Social network analysis queries 219 220# Find mutual friends 221MATCH (a:Person)-[:FRIENDS]-(mutual)-[:FRIENDS]-(b:Person) 222WHERE a <> b AND NOT (a)-[:FRIENDS]-(b) 223RETURN a.name as person1, b.name as person2, mutual.name as mutual_friend; 224 225# Find popular interests 226MATCH (p:Person)-[:INTERESTED_IN]->(i:Interest) 227RETURN i.name as interest, count(p) as popularity 228ORDER BY popularity DESC; 229 230# Find people with similar interests 231MATCH (p1:Person)-[:INTERESTED_IN]->(i:Interest)<-[:INTERESTED_IN]-(p2:Person) 232WHERE p1 <> p2 233RETURN p1.name as person1, p2.name as person2, count(i) as shared_interests 234ORDER BY shared_interests DESC; 235 236# Find the social network diameter 237MATCH path = shortestPath((a:Person)-[:FRIENDS*]-(b:Person)) 238WHERE a <> b 239RETURN a.name as start, b.name as end, length(path) as distance 240ORDER BY distance DESC 241LIMIT 5; 242 243# Show network statistics 244MATCH (p:Person) 245OPTIONAL MATCH (p)-[:FRIENDS]-(friend) 246RETURN p.name as person, count(friend) as friend_count 247ORDER BY friend_count DESC; 248``` 249 250Run analysis: 251```bash 252./gigabrain-cli --file social_analysis.cypher --format table 253``` 254 255## Output Formatting 256 257### Different Output Formats 258 259```bash 260# Table format (default) 261gigabrain> MATCH (p:Person) RETURN p.name, p.age LIMIT 3 262╭────────────┬─────────╮ 263│ name │ age │ 264├────────────┼─────────┤ 265│ Alice │ 30266│ Bob │ 25267│ Charlie │ 35268╰────────────┴─────────╯ 269 270# Switch to JSON 271gigabrain> :format json 272gigabrain> MATCH (p:Person) RETURN p.name, p.age LIMIT 3 273{"columns": ["p.name", "p.age"], "rows": 3} 274 275# Switch to CSV 276gigabrain> :format csv 277gigabrain> MATCH (p:Person) RETURN p.name, p.age LIMIT 3 278p.name,p.age 279Alice,30 280Bob,25 281Charlie,35 282 283# Switch back to table 284gigabrain> :format table 285``` 286 287### Command Line Format Options 288 289```bash 290# JSON output for scripting 291./gigabrain-cli --execute "MATCH (p:Person) RETURN count(p)" --format json 292 293# CSV output for data analysis 294./gigabrain-cli --execute "MATCH (p:Person) RETURN p.name, p.age" --format csv > people.csv 295 296# Plain output for debugging 297./gigabrain-cli --execute ":stats" --format plain 298``` 299 300## Advanced Examples 301 302### Graph Algorithm Analysis 303 304```cypher 305# Create a more complex network for algorithm testing 306CREATE (a:Node {id: 'A'}), (b:Node {id: 'B'}), (c:Node {id: 'C'}), 307 (d:Node {id: 'D'}), (e:Node {id: 'E'}), (f:Node {id: 'F'}); 308 309CREATE (a)-[:CONNECTS {weight: 1}]->(b), 310 (b)-[:CONNECTS {weight: 2}]->(c), 311 (c)-[:CONNECTS {weight: 1}]->(d), 312 (d)-[:CONNECTS {weight: 3}]->(e), 313 (e)-[:CONNECTS {weight: 1}]->(f), 314 (f)-[:CONNECTS {weight: 2}]->(a), 315 (a)-[:CONNECTS {weight: 4}]->(d), 316 (b)-[:CONNECTS {weight: 1}]->(e); 317 318# Find shortest paths 319MATCH path = shortestPath((start:Node {id: 'A'})-[:CONNECTS*]-(end:Node {id: 'E'})) 320RETURN start.id as start, end.id as end, length(path) as hops; 321 322# Show node connectivity 323MATCH (n:Node) 324OPTIONAL MATCH (n)-[:CONNECTS]-(connected) 325RETURN n.id as node, count(connected) as degree 326ORDER BY degree DESC; 327``` 328 329### Time-Series Data Analysis 330 331```cypher 332# Create time-series events 333CREATE (user1:User {id: 'user1'}), 334 (user2:User {id: 'user2'}), 335 (user3:User {id: 'user3'}); 336 337CREATE (event1:Event {type: 'login', timestamp: 1640995200}), 338 (event2:Event {type: 'purchase', timestamp: 1640995800}), 339 (event3:Event {type: 'logout', timestamp: 1640996400}); 340 341CREATE (user1)-[:PERFORMED]->(event1), 342 (user1)-[:PERFORMED]->(event2), 343 (user1)-[:PERFORMED]->(event3); 344 345# Query events by time 346MATCH (u:User)-[:PERFORMED]->(e:Event) 347WHERE e.timestamp > 1640995000 348RETURN u.id as user, e.type as event, e.timestamp as time 349ORDER BY time; 350``` 351 352## Scripting and Automation 353 354### Backup Script 355 356Create `backup.sh`: 357```bash 358#!/bin/bash 359DATE=$(date +%Y%m%d_%H%M%S) 360BACKUP_FILE="gigabrain_backup_${DATE}.json" 361 362echo "Creating backup: ${BACKUP_FILE}" 363./gigabrain-cli --execute "backup ${BACKUP_FILE}" --silent 364 365if [ $? -eq 0 ]; then 366 echo "Backup completed successfully" 367 # Optionally upload to cloud storage 368 # aws s3 cp "${BACKUP_FILE}" s3://my-backups/gigabrain/ 369else 370 echo "Backup failed!" 371 exit 1 372fi 373``` 374 375### Health Check Script 376 377Create `health_check.sh`: 378```bash 379#!/bin/bash 380STATS=$(./gigabrain-cli --execute ":stats" --format json --silent) 381NODE_COUNT=$(echo $STATS | jq -r '.nodes // 0') 382 383if [ "$NODE_COUNT" -gt 0 ]; then 384 echo "Database healthy: $NODE_COUNT nodes" 385 exit 0 386else 387 echo "Database issue: No nodes found" 388 exit 1 389fi 390``` 391 392### Data Migration Script 393 394Create `migrate_data.cypher`: 395```cypher 396# Data migration script 397# Add new properties to existing nodes 398MATCH (p:Person) 399WHERE NOT EXISTS(p.created_at) 400SET p.created_at = timestamp(); 401 402# Create indexes for performance 403# Note: Index creation syntax may vary 404MATCH (p:Person) WHERE EXISTS(p.email) SET p:EmailUser; 405 406# Update relationship properties 407MATCH (p1:Person)-[r:FRIENDS]->(p2:Person) 408WHERE NOT EXISTS(r.strength) 409SET r.strength = 1.0; 410 411# Show migration results 412MATCH (p:Person) 413RETURN 414 count(p) as total_people, 415 count(CASE WHEN EXISTS(p.created_at) THEN 1 END) as with_timestamps, 416 count(CASE WHEN p:EmailUser THEN 1 END) as email_users; 417``` 418 419## Troubleshooting Examples 420 421### Performance Issues 422 423```cypher 424# Identify slow queries by profiling 425gigabrain> profile MATCH (p:Person)-[:FRIENDS*1..5]->(distant) RETURN p.name, count(distant) 426 427# Check memory usage 428gigabrain> memory 429 430# Analyze performance characteristics 431gigabrain> analyze performance 432 433# Optimize database 434gigabrain> optimize 435``` 436 437### Data Validation 438 439```cypher 440# Check for orphaned nodes 441MATCH (n) 442WHERE NOT (n)--() 443RETURN labels(n)[0] as type, count(n) as orphaned_count; 444 445# Validate relationship consistency 446MATCH (p:Person)-[r:FRIENDS]->(friend:Person) 447WHERE NOT EXISTS((friend)-[:FRIENDS]->(p)) 448RETURN p.name as person, friend.name as one_way_friend; 449 450# Check for duplicate data 451MATCH (p1:Person), (p2:Person) 452WHERE p1 <> p2 AND p1.email = p2.email 453RETURN p1.name as person1, p2.name as person2, p1.email as duplicate_email; 454``` 455 456### Debugging Connection Issues 457 458```bash 459# Check connection status 460gigabrain> connections 461 462# Test basic functionality 463gigabrain> CREATE (test:Test {id: 1}) RETURN test 464 465# Clean up test data 466gigabrain> MATCH (test:Test) DELETE test 467 468# Show recent logs (if implemented) 469gigabrain> logs recent 470``` 471 472These examples demonstrate the full capabilities of the GigaBrain CLI for interactive use, batch processing, administration, and automation. The CLI provides a powerful interface for both development and production use cases.