# CGEF 导入与合并

> 导入 Codeweb Graph Exchange Format 格式的外部图谱数据，并与本地分析结果合并。

---

LLMS 索引： [llms.txt](/codeweb/zh/llms.txt)

---

## 为什么需要 CGEF {#why-cgef}

CGEF(Codeweb Graph Exchange Format)是 codeweb 定义的 JSON 图谱交换格式，用于承接 codeweb 之外的图谱数据——例如无法提供源码扫描的私有 ERP 存储过程调用关系。先导入为独立 store，再与本地分析合并，即可跨边界追溯链路。

## 文档结构 {#document-structure}

CGEF 文档是一个 JSON 文件，顶层字段如下:

```json
{
  "format_version": 1,
  "metadata": {
    "source": "erp-system",
    "generated_at": "2026-04-28T10:00:00Z"
  },
  "node_schemas": {},
  "edge_schemas": {},
  "nodes": [],
  "edges": []
}
```

| 字段 | 必填 | 含义 |
| --- | --- | --- |
| `format_version` | 是 | 格式版本;当前为 `1` |
| `metadata` | 是 | `source` 来源标识与 ISO 8601 `generated_at` 时间戳 |
| `node_schemas` | 否 | 自定义节点类型声明 |
| `edge_schemas` | 否 | 自定义边类型声明 |
| `nodes` | 是 | 节数组(可为空) |
| `edges` | 是 | 边数组(可为空) |

标准节点类型覆盖 codeweb 全部内置对象(`procedure`、`function`、`table`、`view`、`mapped_statement`、`java_method`、`java_class`、`java_sql`、`package`、`trigger`、`type`、`sequence`、`index`、`materialized_view` 等)。每种类型的 key 字段规范见 [CGEF 用户指南](https://github.com/c2j/codeweb/blob/main/docs/cgef-user-guide.md)与 [JSON Schema](https://github.com/c2j/codeweb/blob/main/docs/cgef-schema.json)。

## 自定义类型 {#custom-types}

没有内置等价物的节点/边类型，在 `node_schemas` / `edge_schemas` 中声明(显示名、key 字段、属性定义)。其边以 `CustomEdge` 导入，其节点以 `custom` 标签导入，在 trace、impact、export 中与一等节点同等对待。

## 导入 {#import}

```bash
codeweb import --file enterprise-graph.json --output erp-store.bincode
codeweb import --file enterprise-graph.json --output erp.json --prefix "/opt/erp/src" --name "erp"
codeweb import --file broken.json --output erp.bincode --force
```

| 参数 | 含义 |
| --- | --- |
| `--file` | CGEF JSON 输入 |
| `--output` | 输出 store:`.bincode` 或 `.json` |
| `--prefix` | 为所有相对文件路径添加的前缀 |
| `--name` | 导入 store 的项目名 |
| `--force` | 存在校验或解析错误时仍强制导入 |

## 合并 {#merge}

```bash
codeweb merge -o full-graph.bincode my-project.bincode erp-store.bincode
codeweb merge -o full-graph.bincode --name "combined" my-project.bincode erp-store.bincode
```

合并会对多个 store 做节点/边去重，本地分析与 CGEF 导入的同一张表会收敛为一个节点。合并后的 store 在 `trace`、`serve`、`export` 中与本地 store 完全等价。

## 查看完整指南 {#view-the-full-guide}

中文指南 [docs/cgef-user-guide.md](https://github.com/c2j/codeweb/blob/main/docs/cgef-user-guide.md) 提供了完整实操示例，包括 OP Excel 血缘转换器(`lib/op-lineage.py`)。

---

反链：

- [CLI 参考](/codeweb/zh/docs/cli-reference/)
