这个项目能做什么

GraFlo 是一个 Python 库,可将文件、SQL 数据库、RDF、REST API 或 Kafka 主题中的记录转换为带标签的属性图。你只需在一个名为清单的 YAML 文件中描述一次图结构,GraFlo 便会创建模式,并将顶点和边写入你选择的图数据库,或写入磁盘上的目录。 它面向那些从多个来源构建图,并希望将图的描述放在一个可审查的文件中,而不是分散在各个加载脚本中的工程师。 ## 你可以用它做什么 - **描述一次图结构,然后向其中加载数据。** 清单会命名顶点和边的类型,说明哪些属性标识一个顶点,并说明每种记录如何变成顶点和边。同一个清单可以加载到 ArangoDB、Neo4j、TigerGraph、FalkorDB、Memgraph、NebulaGraph、PostgreSQL 或文件后端,具有相同标识的记录会合并为一个顶点。GraFlo 还可以将现有图从 Neo4j、ArangoDB 或 PostgreSQL 复制到另一个数据库(`GraphEngine.migrate_graph`)。 - **随时间更改描述,并记录历史。** 重命名类型、合并两个类型或更改属性类型都是类型化操作。操作被记录为提交(`graflo commit`、`log`、`checkout`、`verify`、`revert`),你可以重放、检查,并且对大多数操作可以撤销。对同一个清单的两个更改分支通过三方合并(`graflo merge3`)进行协调,不同团队编写的两个清单通过并集(`graflo merge`)合并为一个。 - **检查和推断描述。** GraFlo 可以从 PostgreSQL 数据库或 OWL 本体推断清单,从样本数据中提出标识记录的属性,并根据一致性配置文件(`graflo check`)检查清单,该配置文件是一组建模规则,例如“每个顶点类型都声明其标识”。 ## 快速体验 一个清单包含三个块:`schema` 说明图的样子,`ingestion_model` 说明记录如何映射到图上,`bindings` 说明记录来自哪里。下面这个清单读取包含 `person_id`、`person` 和 `department` 列的 CSV 文件: ```yaml schema: metadata: {name: hr} graph: vertex_config: vertices: - {name: person, properties: [id, name], identity: [id]} - {name: department, properties: [name], identity: [name]} edge_config: edges: [{source: person, target: department}] ingestion_model: resources: - name: departments pipeline: - {vertex: person, from: {id: person_id, name: person}} - {vertex: department, from: {name: department}} bindings: connectors: - {regex: "^dep.*\\.csv$", sub_path: data, resource_name: departments} ``` 下面将它加载到 ArangoDB: ```python from graflo import GraphEngine, GraphManifest from graflo.connections import ArangoConfig manifest = GraphManifest.from_yaml("manifest.yaml") manifest.finish_init() engine = GraphEngine() engine.define_and_ingest(manifest=manifest, target_db_config=ArangoConfig.from_env()) ``` `ArangoConfig.from_env()` 读取 `ARANGO_URI`、`ARANGO_USERNAME`、`ARANGO_PASSWORD` 和 `ARANGO_DATABASE`;每个数据库都有这样的类。参见[数据库连接](https://growgraph.github.io/graflo/guides/database_connections/)。 ## 文档 完整文档:[growgraph.github.io/graflo](https://growgraph.github.io/graflo) - [快速开始](https://growgraph.github.io/graflo/getting_started/quickstart/):将两个 CSV 文件逐步导入图 - [创建清单](https://growgraph.github.io/graflo/getting_started/creating_manifest/):清单的三个块 - [示例](https://growgraph.github.io/graflo/examples/):可运行的示例,每个示例一个问题,其数据位于 [`examples/`](https://github.com/growgraph/graflo/tree/main/examples) - [概念](https://growgraph.github.io/graflo/concepts/):模式、标识、摄取、连接器、演进和版本控制 - [指南](https://growgraph.github.io/graflo/guides/):数据库连接、图迁移、模式推断、API 接线、批量加载 - [GraFlo 本体](https://growgraph.github.io/graflo/concepts/schema/ontology/):将清单表示为 RDF(`graflo manifest-to-rdf`、`graflo rdf-to-manifest`) ## 安装 GraFlo 需要 Python 3.11 或更高版本。数据库客户端、RDF 和 Kafka 支持是默认安装的一部分。 ```bash pip install graflo ``` 可选附加项(参见[安装](https://growgraph.github.io/graflo/getting_started/installation/)指南): - `dev`:pytest 及其插件、hypothesis、ty、pre-commit - `docs`:ProperDocs 及其插件,用于构建文档站点 - `plot`:`pygraphviz`,用于 `graflo plot-manifest` 以及 `graflo merge` 和 `graflo merge3` 的 `--plot` 图 ```bash pip install "graflo[dev,docs,plot]" ``` ## 开发 从克隆安装: ```shell git clone [email protected]:growgraph/graflo.git && cd graflo uv sync --extra dev ``` 完整工作流程参见[贡献指南](https://growgraph.github.io/graflo/contributing/)。 ### 测试 数据库测试需要数据库容器。从克隆中使用 [docker/](https://github.com/growgraph/graflo/tree/main/docker) 下的脚本启动它们: ```shell cd docker ./start-all.sh # Start all services ./stop-all.sh # Stop all services ./cleanup-all.sh # Remove containers and volumes ``` 各引擎的 compose 文件和端口记录在 [docker README](https://github.com/growgraph/graflo/blob/main/docker/README.md) 中。 运行测试: ```shell uv run pytest test ``` 除非传入 `--run-tigergraph`、`--run-nebula` 或 `--run-kafka`,否则 TigerGraph、NebulaGraph 和 Kafka 测试会被跳过。 不需要数据库的测试套件无需容器即可运行,CI 会在每个拉取请求上运行它们: ```shell uv run pytest test --ignore=test/db --ignore=test/data_source --ignore=test/object_storage ``` ## 许可证 基于 [Apache License 2.0](https://github.com/growgraph/graflo/blob/main/LICENSE) 开源。版权和商标声明见 [NOTICE](https://github.com/growgraph/graflo/blob/main/NOTICE):该许可证不授予 **GraFlo** 和 **GrowGraph** 商标的任何权利。重新许可之前的版本以 Business Source License 1.1 发布,并保留那些条款;参见[变更日志](https://github.com/growgraph/graflo/blob/main/CHANGELOG.md)。 ## 贡献 欢迎贡献。参见[贡献指南](https://growgraph.github.io/graflo/contributing/)。贡献者只需在首次拉取请求中发表评论,即表示接受[贡献者许可协议](https://github.com/growgraph/graflo/blob/main/CLA.md)。