GrafeoDB/grafeo

GitHub: GrafeoDB/grafeo

一款纯 Rust 高性能图数据库,支持 LPG 与 RDF 双模型、多语言查询,可嵌入或独立运行,内置向量与全文检索能力。

Stars: 201 | Forks: 4

[![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/140f9696e0092936.svg)](https://github.com/GrafeoDB/grafeo/actions/workflows/ci.yml) [![Docs](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/60a28eacc9092945.svg)](https://github.com/GrafeoDB/grafeo/actions/workflows/docs.yml) [![codecov](https://codecov.io/gh/GrafeoDB/grafeo/graph/badge.svg)](https://codecov.io/gh/GrafeoDB/grafeo) [![Crates.io](https://img.shields.io/crates/v/grafeo.svg)](https://crates.io/crates/grafeo) [![PyPI](https://img.shields.io/pypi/v/grafeo.svg)](https://pypi.org/project/grafeo/) [![npm](https://img.shields.io/npm/v/@grafeo-db/js.svg)](https://www.npmjs.com/package/@grafeo-db/js) [![wasm](https://img.shields.io/npm/v/@grafeo-db/wasm.svg?label=wasm)](https://www.npmjs.com/package/@grafeo-db/wasm) [![NuGet](https://img.shields.io/nuget/v/Grafeo.svg)](https://www.nuget.org/packages/Grafeo) [![pub.dev](https://img.shields.io/pub/v/grafeo.svg)](https://pub.dev/packages/grafeo) [![Web](https://img.shields.io/npm/v/@grafeo-db/web.svg?label=web)](https://www.npmjs.com/package/@grafeo-db/web) [![Go](https://img.shields.io/badge/go-1.22%2B-00ADD8)](https://pkg.go.dev/github.com/GrafeoDB/grafeo/crates/bindings/go) [![Docker](https://img.shields.io/docker/v/grafeo/grafeo-server?label=server)](https://hub.docker.com/r/grafeo/grafeo-server) [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE) [![MSRV](https://img.shields.io/badge/MSRV-1.91.1-blue)](https://www.rust-lang.org) [![Python](https://img.shields.io/badge/python-3.12%2B-blue)](https://www.python.org) # Grafeo Grafeo 是一个使用 Rust 从零开始构建的图数据库,专为速度和低内存占用而设计。它可以作为库嵌入运行,也可以作为独立服务器运行,支持内存或持久化存储以及完整的 ACID 事务。 在 [LDBC Social Network Benchmark](https://github.com/GrafeoDB/graph-bench) 上,Grafeo 在嵌入式和服务器配置中都是测试过最快的图数据库,同时内存占用仅为部分替代方案的很小一部分。 [![Grafeo Playground](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/a1cfd1d253092953.png)](https://grafeo.ai) Grafeo 同时支持 **Labeled Property Graph (LPG)** 和 **Resource Description Framework (RDF)** 数据模型以及所有主流查询语言。 ## 功能特性 ### 核心能力 - **双数据模型支持**:LPG 和 RDF,各自拥有优化的存储方式 - **多语言查询**:GQL, Cypher, Gremlin, GraphQL, SPARQL 和 SQL/PGQ - 可嵌入,零外部依赖 —— 无需 JVM、无需 Docker、无需外部进程 - **多语言绑定**:Python (PyO3), Node.js/TypeScript (napi-rs), Go (CGO), C (FFI), C# (.NET 8 P/Invoke), Dart (dart:ffi), WebAssembly (wasm-bindgen) - 内存和持久化存储模式 - 支持快照隔离的 MVCC 事务 ### 查询语言 - **GQL** (ISO/IEC 39075) - **Cypher** (openCypher 9.0) - **Gremlin** (Apache TinkerPop) - **GraphQL** - **SPARQL** (W3C 1.1) - **SQL/PGQ** (SQL:2023) ### 向量搜索与 AI - **向量作为一等公民类型**:`Value::Vector(Arc<[f32]>)` 与图数据一同存储 - **HNSW 索引**:O(log n) 近似最近邻搜索,召回率可调 - **距离函数**:Cosine, Euclidean, Dot Product, Manhattan (SIMD 加速: AVX2, SSE, NEON) - **向量量化**:标量 (f32 → u8)、二进制 (1-bit) 和乘积量化 (8-32x 压缩) - **BM25 文本搜索**:全文倒排索引,支持 Unicode 分词器和停用词移除 - **混合搜索**:结合文本 + 向量搜索,支持倒数排名融合 (RRF) 或加权融合 - **变更数据捕获 (CDC)**:用于审计追踪和历史记录的变更前后属性快照 - **混合图+向量查询**:在 GQL 和 SPARQL 中结合图遍历与向量相似度 - **内存映射存储**:带有 LRU 缓存的磁盘后备向量,适用于大型数据集 - **批量操作**:通过 rayon 进行并行多查询搜索 ### 性能特性 - 具有自适应块大小的 **Push-based 向量化执行** - 具有自动检测线程数的 **Morsel-driven 并行机制** - 具有字典、增量和 RLE 压缩的 **列式存储** - 具有 DPccp 连接排序和直方图的 **基于代价的优化器** - 用于智能数据跳过的 **Zone maps** (包括向量 zone maps) - 具有运行时重优化的 **自适应查询执行** - 用于核外处理的 **透明溢出** - 用于高效成员测试的 **Bloom filters** ### 基准测试 通过 [graph-bench](https://github.com/GrafeoDB/graph-bench) 使用 [LDBC Social Network Benchmark](https://ldbcouncil.org/benchmarks/snb/) 进行测试: **Embedded** (SF0.1, 进程内): | Database | SNB Interactive | Memory | Graph Analytics | Memory | |----------|---------------:|-------:|----------------:|-------:| | **Grafeo** | **2,904 ms** | 136 MB | **0.4 ms** | 43 MB | | LadybugDB(Kuzu) | 5,333 ms | 4,890 MB | 225 ms | 250 MB | | FalkorDB Lite | 7,454 ms | 156 MB | 89 ms | 88 MB | **Server** (SF0.1, 网络传输): | Database | SNB Interactive | Graph Analytics | |----------|---------------:|----------------:| | **Grafeo Server** | **730 ms** | **15 ms** | | Memgraph | 4,113 ms | 19 ms | | Neo4j | 6,788 ms | 253 ms | | ArangoDB | 40,043 ms | 22,739 ms | 完整结果:[embedded](https://github.com/GrafeoDB/graph-bench/blob/main/RESULTS_EMBEDDED.md) | [server](https://github.com/GrafeoDB/graph-bench/blob/main/RESULTS_SERVER.md) ## 查询语言与数据模型支持 | Query Language | LPG | RDF | |----------------|-----|-----| | GQL | ✅ | - | | Cypher | ✅ | - | | Gremlin | ✅ | - | | GraphQL | ✅ | ✅ | | SPARQL | - | ✅ | | SQL/PGQ | ✅ | - | Grafeo 采用模块化翻译器架构,查询语言被解析为 AST,然后翻译为统一的逻辑计划,在相应的存储后端(LPG 或 RDF)上执行。 ### 数据模型 - **LPG (Labeled Property Graph)**:带有标签和属性的节点,带有类型和属性的边。适用于社交网络、知识图谱和应用数据。 - **RDF (Resource Description Framework)**:基于三元组的存储,带有 SPO/POS/OSP 索引。适用于语义网、关联数据和基于本体的应用。 ## 安装 ### Rust ``` cargo add grafeo ``` 默认启用 `embedded` 配置:GQL、AI 功能(向量/文本/混合搜索、CDC)、图算法和并行执行。使用功能组进行自定义: ``` # 默认(嵌入式 profile):GQL + AI + algorithms + parallel cargo add grafeo # 所有查询语言 + AI + algorithms + storage cargo add grafeo --no-default-features --features full # 仅查询语言,无 AI 功能或 algorithms cargo add grafeo --no-default-features --features languages # 仅 GQL 带 AI 功能 cargo add grafeo --no-default-features --features gql,ai # 最小化:仅 GQL cargo add grafeo --no-default-features --features gql # 带 graph algorithms(SSSP、PageRank、centrality、community detection 等) cargo add grafeo --no-default-features --features gql,algos # 带 ONNX embedding 生成(opt-in,约 17MB) cargo add grafeo --features embed ``` ### Node.js / TypeScript ``` npm install @grafeo-db/js ``` ### Go ``` go get github.com/GrafeoDB/grafeo/crates/bindings/go ``` ### WebAssembly ``` npm install @grafeo-db/wasm ``` ### C# / .NET ``` dotnet add package Grafeo ``` ### Dart ``` # pubspec.yaml dependencies: grafeo: ^0.5.21 ``` ### Python ``` uv add grafeo ``` 支持 CLI: ``` uv add grafeo[cli] ``` ## 快速开始 ### Node.js / TypeScript ``` const { GrafeoDB } = require('@grafeo-db/js'); // Create an in-memory database const db = await GrafeoDB.create(); // Or open a persistent database // const db = await GrafeoDB.create({ path: './my-graph.db' }); // Create nodes and relationships await db.execute("INSERT (:Person {name: 'Alix', age: 30})"); await db.execute("INSERT (:Person {name: 'Gus', age: 25})"); await db.execute(` MATCH (a:Person {name: 'Alix'}), (b:Person {name: 'Gus'}) INSERT (a)-[:KNOWS {since: 2020}]->(b) `); // Query the graph const result = await db.execute(` MATCH (p:Person)-[:KNOWS]->(friend) RETURN p.name, friend.name `); console.log(result.rows); await db.close(); ``` ### Python ``` import grafeo # 创建内存数据库 db = grafeo.GrafeoDB() # 或打开/创建持久化数据库 # db = grafeo.GrafeoDB("/path/to/database") # 使用 GQL 创建节点 db.execute("INSERT (:Person {name: 'Alix', age: 30})") db.execute("INSERT (:Person {name: 'Gus', age: 25})") # 创建关系 db.execute(""" MATCH (a:Person {name: 'Alix'}), (b:Person {name: 'Gus'}) INSERT (a)-[:KNOWS {since: 2020}]->(b) """) # 查询图 result = db.execute(""" MATCH (p:Person)-[:KNOWS]->(friend) RETURN p.name, friend.name """) for row in result: print(row) # 或使用直接 API node = db.create_node(["Person"], {"name": "Harm"}) print(f"Created node with ID: {node.id}") # 管理标签 db.add_node_label(node.id, "Employee") # Add a label db.remove_node_label(node.id, "Contractor") # Remove a label labels = db.get_node_labels(node.id) # Get all labels ``` ### Admin APIs (Python) ``` # 数据库检查 db.info() # Overview: mode, counts, persistence db.detailed_stats() # Memory usage, index counts db.schema() # Labels, edge types, property keys db.validate() # Integrity check # 持久化控制 db.save("/path/to/backup") # Save to disk db.to_memory() # Create in-memory copy GrafeoDB.open_in_memory(path) # Load as in-memory # WAL 管理 db.wal_status() # WAL info db.wal_checkpoint() # Force checkpoint ``` ### Rust ``` use grafeo::GrafeoDB; fn main() { // Create an in-memory database let db = GrafeoDB::new_in_memory(); // Or open a persistent database // let db = GrafeoDB::open("./my_database").unwrap(); // Execute GQL queries db.execute("INSERT (:Person {name: 'Alix'})").unwrap(); let result = db.execute("MATCH (p:Person) RETURN p.name").unwrap(); for row in result.rows { println!("{:?}", row); } } ``` ### 向量搜索 ``` import grafeo db = grafeo.GrafeoDB() # 存储带 embeddings 的文档 db.execute("""INSERT (:Document { title: 'Graph Databases', embedding: vector([0.1, 0.8, 0.3, 0.5]) })""") db.execute("""INSERT (:Document { title: 'Vector Search', embedding: vector([0.2, 0.7, 0.4, 0.6]) })""") db.execute("""INSERT (:Document { title: 'Cooking Recipes', embedding: vector([0.9, 0.1, 0.2, 0.1]) })""") # 创建 HNSW index 以进行快速近似搜索 db.execute(""" CREATE VECTOR INDEX doc_idx ON :Document(embedding) DIMENSION 4 METRIC 'cosine' """) # 使用 cosine similarity 查找相似文档 query = [0.15, 0.75, 0.35, 0.55] result = db.execute(f""" MATCH (d:Document) WHERE cosine_similarity(d.embedding, vector({query})) > 0.9 RETURN d.title, cosine_similarity(d.embedding, vector({query})) AS score ORDER BY score DESC """) for row in result: print(row) # Graph Databases, Vector Search (Cooking Recipes filtered out) ``` ## 命令行界面 面向运维人员和 DevOps 的可选管理 CLI: ``` # 安装 CLI 支持 uv add grafeo[cli] # 检查 grafeo info ./mydb # Overview: counts, size, mode grafeo stats ./mydb # Detailed statistics grafeo schema ./mydb # Labels, edge types, property keys grafeo validate ./mydb # Integrity check # 备份与恢复 grafeo backup create ./mydb -o backup grafeo backup restore backup ./copy --force # WAL 管理 grafeo wal status ./mydb grafeo wal checkpoint ./mydb # 输出格式 grafeo info ./mydb --format json # Machine-readable JSON grafeo info ./mydb --format table # Human-readable table (default) ``` ## 生态系统 | Project | Description | |---------|-------------| | [**grafeo-server**](https://github.com/GrafeoDB/grafeo-server) | HTTP server & web UI:REST API、事务、单一二进制文件(约 40MB Docker 镜像) | | [**grafeo-web**](https://github.com/GrafeoDB/grafeo-web) | 基于 WebAssembly 的浏览器端 Grafeo,支持 IndexedDB 持久化 | | [**gwp**](https://github.com/GrafeoDB/gql-wire-protocol) | GQL Wire Protocol:用于 GQL (ISO/IEC 39075) 的 gRPC 线路协议,提供 5 种语言的客户端绑定 | | [**boltr**](https://github.com/GrafeoDB/boltr) | Bolt Wire Protocol:纯 Rust 实现的 Bolt v5.x,用于 Neo4j 驱动兼容性 | | [**grafeo-langchain**](https://github.com/GrafeoDB/grafeo-langchain) | LangChain 集成:graph store, vector store, Graph RAG retrieval | | [**grafeo-llamaindex**](https://github.com/GrafeoDB/grafeo-llamaindex) | LlamaIndex 集成:PropertyGraphStore, vector search, knowledge graphs | | [**grafeo-mcp**](https://github.com/GrafeoDB/grafeo-mcp) | Model Context Protocol server:将 Grafeo 作为工具暴露给 LLM agents | | [**grafeo-memory**](https://github.com/GrafeoDB/grafeo-memory) | LLM 应用的 AI 记忆层:事实提取、去重、语义搜索 | | [**anywidget-graph**](https://github.com/GrafeoDB/anywidget-graph) | Python notebook 的交互式图可视化 | | [**anywidget-vector**](https://github.com/GrafeoDB/anywidget-vector) | Python notebook 的 3D 向量/embedding 可视化 | | [**playground**](https://grafeo.ai) | 交互式浏览器 playground:支持 6 种语言查询、图可视化、schema 探索 | | [**graph-bench**](https://github.com/GrafeoDB/graph-bench) | 跨越 25+ 基准测试的图数据库对比基准套件 | | [**ann-benchmarks**](https://github.com/GrafeoDB/ann-benchmarks) | ann-benchmarks 的分支,包含 Grafeo HNSW 适配器,用于向量搜索基准测试 | ## 文档 完整文档请访问 [grafeo.dev](https://grafeo.dev)。 ## 致谢 Grafeo 的执行引擎灵感来源于: - [DuckDB](https://duckdb.org/),向量化 push-based 执行,morsel-driven 并行机制 - [LadybugDB](https://github.com/LadybugDB/ladybug),基于 CSR 的邻接索引,因子化查询处理 ## 许可证 Apache-2.0
标签:ACID事务, AI工具, Cypher, DNS解析, Docker, GraphQL, LPG, MITM代理, OPA替代, RDF, Rust, WebAssembly, 内存数据库, 可视化界面, 多人体追踪, 多语言绑定, 安全防御评估, 嵌入式数据库, 开源项目, 持久化存储, 数据库内核, 日志审计, 服务器, 网络流量审计, 自动化治理, 语义网, 请求拦截, 逆向工具, 通知系统, 键值存储