|
| 1 | +"""Export/import example for sqlite-vec-client. |
| 2 | +
|
| 3 | +Demonstrates: |
| 4 | +- Exporting data to JSON and CSV |
| 5 | +- Importing data from JSON and CSV |
| 6 | +- Filtered exports |
| 7 | +- Backup and restore workflows |
| 8 | +""" |
| 9 | + |
| 10 | +from sqlite_vec_client import SQLiteVecClient |
| 11 | + |
| 12 | + |
| 13 | +def main(): |
| 14 | + # Create and populate database |
| 15 | + client = SQLiteVecClient(table="documents", db_path=":memory:") |
| 16 | + client.create_table(dim=128, distance="cosine") |
| 17 | + |
| 18 | + texts = [ |
| 19 | + "Introduction to Python", |
| 20 | + "Advanced JavaScript", |
| 21 | + "Python for Data Science", |
| 22 | + "Java Programming Guide", |
| 23 | + "Machine Learning with Python", |
| 24 | + ] |
| 25 | + |
| 26 | + embeddings = [[0.1 * i] * 128 for i in range(len(texts))] |
| 27 | + |
| 28 | + metadata = [ |
| 29 | + {"category": "python", "level": "beginner"}, |
| 30 | + {"category": "javascript", "level": "advanced"}, |
| 31 | + {"category": "python", "level": "intermediate"}, |
| 32 | + {"category": "java", "level": "beginner"}, |
| 33 | + {"category": "python", "level": "advanced"}, |
| 34 | + ] |
| 35 | + |
| 36 | + client.add(texts=texts, embeddings=embeddings, metadata=metadata) |
| 37 | + print(f"Added {client.count()} documents\n") |
| 38 | + |
| 39 | + # Example 1: Export all data to JSON |
| 40 | + print("=== Export to JSON ===") |
| 41 | + count = client.export_to_json("backup.jsonl") |
| 42 | + print(f"Exported {count} records to backup.jsonl\n") |
| 43 | + |
| 44 | + # Example 2: Export filtered data to JSON |
| 45 | + print("=== Export Filtered Data ===") |
| 46 | + count = client.export_to_json("python_docs.jsonl", filters={"category": "python"}) |
| 47 | + print(f"Exported {count} Python documents to python_docs.jsonl\n") |
| 48 | + |
| 49 | + # Example 3: Export to CSV (without embeddings for readability) |
| 50 | + print("=== Export to CSV ===") |
| 51 | + count = client.export_to_csv("documents.csv", include_embeddings=False) |
| 52 | + print(f"Exported {count} records to documents.csv\n") |
| 53 | + |
| 54 | + # Example 4: Export to CSV with embeddings |
| 55 | + print("=== Export to CSV with Embeddings ===") |
| 56 | + count = client.export_to_csv("documents_full.csv", include_embeddings=True) |
| 57 | + print(f"Exported {count} records with embeddings to documents_full.csv\n") |
| 58 | + |
| 59 | + # Example 5: Backup and restore workflow |
| 60 | + print("=== Backup and Restore Workflow ===") |
| 61 | + |
| 62 | + # Backup |
| 63 | + print("Creating backup...") |
| 64 | + client.export_to_json("backup_full.jsonl") |
| 65 | + |
| 66 | + # Simulate data loss |
| 67 | + print("Simulating data loss...") |
| 68 | + original_count = client.count() |
| 69 | + for rowid in range(1, original_count + 1): |
| 70 | + client.delete(rowid) |
| 71 | + print(f"Records after deletion: {client.count()}") |
| 72 | + |
| 73 | + # Restore |
| 74 | + print("Restoring from backup...") |
| 75 | + count = client.import_from_json("backup_full.jsonl") |
| 76 | + print(f"Restored {count} records") |
| 77 | + print(f"Records after restore: {client.count()}\n") |
| 78 | + |
| 79 | + # Example 6: Data migration scenario |
| 80 | + print("=== Data Migration Scenario ===") |
| 81 | + |
| 82 | + # Export from source |
| 83 | + print("Exporting from source database...") |
| 84 | + client.export_to_json("migration.jsonl") |
| 85 | + |
| 86 | + # Import to new database (simulated with same client) |
| 87 | + print("Importing to destination database...") |
| 88 | + # In real scenario, you would create a new client with different db_path |
| 89 | + # new_client = SQLiteVecClient(table="documents", db_path="new.db") |
| 90 | + # new_client.create_table(dim=128) |
| 91 | + # new_client.import_from_json("migration.jsonl") |
| 92 | + print("Migration complete!\n") |
| 93 | + |
| 94 | + # Example 7: Filtered export for data sharing |
| 95 | + print("=== Export Subset for Sharing ===") |
| 96 | + count = client.export_to_csv( |
| 97 | + "beginner_docs.csv", include_embeddings=False, filters={"level": "beginner"} |
| 98 | + ) |
| 99 | + print(f"Exported {count} beginner-level documents for sharing\n") |
| 100 | + |
| 101 | + # Cleanup |
| 102 | + print("=== Cleanup ===") |
| 103 | + import os |
| 104 | + |
| 105 | + for file in [ |
| 106 | + "backup.jsonl", |
| 107 | + "python_docs.jsonl", |
| 108 | + "documents.csv", |
| 109 | + "documents_full.csv", |
| 110 | + "backup_full.jsonl", |
| 111 | + "migration.jsonl", |
| 112 | + "beginner_docs.csv", |
| 113 | + ]: |
| 114 | + if os.path.exists(file): |
| 115 | + os.remove(file) |
| 116 | + print(f"Removed {file}") |
| 117 | + |
| 118 | + client.close() |
| 119 | + print("\nExample complete!") |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments