GitHub user dosubot[bot] edited a comment on the discussion: 关于电网拓扑数据导入
<!-- Answer -->
你的数据结构和需求已经很清楚,下面给你一份适配你电网拓扑数据的 HugeGraph schema.groovy 和 struct.json
配置模板,并说明如何实现双向连接和 HTTP 工作流集成。
---
### 1. schema.groovy 示例(每种点类型和边类型都单独定义,便于扩展)
```groovy
// 属性定义
schema.propertyKey("id").asText().ifNotExist().create();
schema.propertyKey("name").asText().ifNotExist().create();
schema.propertyKey("objectType").asText().ifNotExist().create();
schema.propertyKey("objectHandle").asText().ifNotExist().create();
schema.propertyKey("deviceType").asText().ifNotExist().create();
schema.propertyKey("internalEndpointNo").asInt().ifNotExist().create();
schema.propertyKey("terminalNo").asInt().ifNotExist().create();
schema.propertyKey("usage").asText().ifNotExist().create();
schema.propertyKey("powerFlowDirection").asInt().ifNotExist().create();
schema.propertyKey("belongSubstation").asText().ifNotExist().create();
schema.propertyKey("belongFeeder").asText().ifNotExist().create();
// 顶点标签
schema.vertexLabel("Substation").properties("id", "name", "objectType",
"objectHandle", "deviceType", "internalEndpointNo", "terminalNo", "usage",
"powerFlowDirection", "belongSubstation",
"belongFeeder").primaryKeys("id").ifNotExist().create();
schema.vertexLabel("LineSegment").properties("id", "name", "objectType",
"objectHandle", "deviceType", "internalEndpointNo", "terminalNo", "usage",
"powerFlowDirection", "belongSubstation",
"belongFeeder").primaryKeys("id").ifNotExist().create();
schema.vertexLabel("LineSwitch").properties("id", "name", "objectType",
"objectHandle", "deviceType", "internalEndpointNo", "terminalNo", "usage",
"powerFlowDirection", "belongSubstation",
"belongFeeder").primaryKeys("id").ifNotExist().create();
schema.vertexLabel("StationHouse").properties("id", "name", "objectType",
"objectHandle", "deviceType", "internalEndpointNo", "terminalNo", "usage",
"powerFlowDirection", "belongSubstation",
"belongFeeder").primaryKeys("id").ifNotExist().create();
// 边标签(每种类型对每种类型都定义一条边,便于扩展和双向连接)
schema.edgeLabel("Substation2LineSegment").sourceLabel("Substation").targetLabel("LineSegment").properties().ifNotExist().create();
schema.edgeLabel("LineSegment2StationHouse").sourceLabel("LineSegment").targetLabel("StationHouse").properties().ifNotExist().create();
schema.edgeLabel("LineSegment2LineSegment").sourceLabel("LineSegment").targetLabel("LineSegment").properties().ifNotExist().create();
schema.edgeLabel("StationHouse2LineSwitch").sourceLabel("StationHouse").targetLabel("LineSwitch").properties().ifNotExist().create();
// ...根据实际需要继续补充
// 反向边(如需双向,定义反向边标签)
schema.edgeLabel("LineSegment2Substation").sourceLabel("LineSegment").targetLabel("Substation").properties().ifNotExist().create();
schema.edgeLabel("StationHouse2LineSegment").sourceLabel("StationHouse").targetLabel("LineSegment").properties().ifNotExist().create();
// ...以此类推
```
---
### 2. struct.json 示例(关键点:source/target 字段要和顶点主键一致)
```json
{
"vertices": [
{
"label": "Substation",
"input": {
"type": "file",
"path": "vertex_substation-1.csv",
"format": "CSV",
"header": ["id", "name", "objectType", "objectHandle", "deviceType",
"internalEndpointNo", "terminalNo", "usage", "powerFlowDirection",
"belongSubstation", "belongFeeder"],
"charset": "UTF-8"
},
"null_values": ["NULL", "null", ""]
},
// 其他顶点类型同理
],
"edges": [
{
"label": "Substation2LineSegment",
"source": ["from"],
"target": ["to"],
"input": {
"type": "file",
"path": "edge_substation2linesegment.csv",
"format": "CSV",
"header": ["from", "to"]
},
"field_mapping": {
"from": "id",
"to": "id"
}
},
// 反向边
{
"label": "LineSegment2Substation",
"source": ["to"],
"target": ["from"],
"input": {
"type": "file",
"path": "edge_substation2linesegment.csv",
"format": "CSV",
"header": ["from", "to"]
},
"field_mapping": {
"from": "id",
"to": "id"
}
}
// 其他边类型同理
]
}
```
---
### 3. 如何实现双向连接
HugeGraph 的边是有向的,A→B 和 B→A 需要各导入一条边。你可以用脚本或 Excel 处理,把所有边数据复制一份,把 from/to
互换,合并后导入即可(比如 edge_xxx.csv
里每条边都正反各一条)。这样查询时可以支持任意方向的遍历和分析。[官方文档说明](https://github.com/apache/incubator-hugegraph-doc/blob/3e1c610281c91ea3a5c600b393088e9ffae34799/content/en/docs/quickstart/toolchain/hugegraph-loader.md#L11-L1110)
---
### 4. 导入命令与验证
导入命令示例:
```bash
sh bin/hugegraph-loader.sh -g hugegraph -f struct.json -s schema.groovy -h
127.0.0.1 -p 8080
```
导入后可用 curl 验证:
```bash
curl "http://localhost:8080/graphs/hugegraph/graph/vertices"
curl "http://localhost:8080/graphs/hugegraph/graph/edges"
```
---
### 5. Dify 工作流 HTTP 集成
HugeGraph 支持标准 RESTful API,Dify 工作流可以像集成 Neo4j 一样通过 HTTP 访问
HugeGraph,进行增删查改、批量导入、图查询等。比如创建边:
```http
POST http://localhost:8080/graphs/hugegraph/graph/edges
Content-Type: application/json
{
"label": "Substation2LineSegment",
"outV": "BDZ-3002",
"inV": "XLD-3445",
"outVLabel": "Substation",
"inVLabel": "LineSegment"
}
```
详见 [RESTful API
文档](https://github.com/apache/incubator-hugegraph-doc/blob/3e1c610281c91ea3a5c600b393088e9ffae34799/content/en/docs/clients/restful-api/edge.md#L11-L650)。
---
### 6. 扩展性建议
- 每种点类型、边类型都单独定义,后续扩展新类型只需补充 schema 和 struct 配置即可。
- 边标签建议按“类型A2类型B”命名,便于管理和后续扩展。
- 数据量大时建议分批导入,或用 Loader 的断点续传功能。
- schema 变更需注意边标签的单一源/目标类型限制,新增类型时需新增边标签。
如需更详细的配置模板或自动化脚本,可以继续补充你的具体需求。
<!-- Dosu Comment Footer -->
*To reply, just mention
[@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fhugegraph).*
---
Share context across your team and agents. Try
[Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-tagline&utm_term=apache%2Fhugegraph).
[](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-feedback&utm_term=apache%2Fhugegraph#message_id=78feb77b-100d-4b74-9db3-13e58a403bda)
[](https://github.dosu.com/apache/hugegraph?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-learn-repo&utm_term=apache%2Fhugegraph)
[](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-add-team&utm_term=apache%2Fhugegraph)
GitHub link:
https://github.com/apache/hugegraph/discussions/2917#discussioncomment-15192892
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]