dosubot[bot] commented on issue #2822:
URL:
https://github.com/apache/incubator-hugegraph/issues/2822#issuecomment-3003431263
<!-- Answer -->
下面是 HugeGraph Java 客户端批量创建边(addEdges)的标准用法案例,结合了实际测试代码和官方示例:
1. **确保已定义好 VertexLabel 和 EdgeLabel**,如:
```java
schema.vertexLabel("person")
.properties("name", "age")
.primaryKeys("name")
.ifNotExist()
.create();
schema.vertexLabel("software")
.properties("name", "lang", "price")
.primaryKeys("name")
.ifNotExist()
.create();
schema.edgeLabel("created")
.link("person", "software")
.properties("date")
.ifNotExist()
.create();
```
2. **创建顶点对象并批量插入**(必须先插入顶点,边才能成功):
```java
Vertex marko = new Vertex("person").property("name",
"marko").property("age", 29);
Vertex lop = new Vertex("software").property("name", "lop").property("lang",
"java").property("price", 328);
List<Vertex> vertices = Arrays.asList(marko, lop);
graph.addVertices(vertices);
```
3. **创建边对象并批量插入**:
```java
Edge markoCreateLop = new Edge("created")
.source(marko)
.target(lop)
.property("date", "2017-12-10");
List<Edge> edges = Arrays.asList(markoCreateLop);
graph.addEdges(edges);
```
4. **注意事项**:
- `Edge` 的 source/target 必须是已存在的 `Vertex` 对象。
- 必须先 addVertices,再 addEdges,否则会报错(如 InvalidOperationException)。
- `Edge` 构造时 label、source、target、属性都要明确指定,label 必须是 schema 里定义的
EdgeLabel,source/target 的类型必须和 EdgeLabel 的 sourceLabel/targetLabel 匹配。
- 支持批量插入 List<Edge>,也支持 List<Map<String, Object>>,但推荐用 Edge 对象,类型安全且易于维护。
完整示例可参考
[BatchExample.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/9696d78e4d0b6e10d4e3382248fd8bda61295a91/hugegraph-client/src/main/java/org/apache/hugegraph/example/BatchExample.java)
和
[BatchInsertTest.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/9696d78e4d0b6e10d4e3382248fd8bda61295a91/hugegraph-client/src/test/java/org/apache/hugegraph/functional/BatchInsertTest.java)。
如需更复杂的属性或批量数据,可以循环构造 Vertex 和 Edge 对象,最后分别调用 addVertices 和 addEdges 即可。
<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*
---
<sup>How did I do?
[Good](https://app.dosu.dev/response-feedback/83cea5b0-8b7c-4432-a597-c29c846fbd7a?feedback_type=great_response)
|
[Irrelevant](https://app.dosu.dev/response-feedback/83cea5b0-8b7c-4432-a597-c29c846fbd7a?feedback_type=irrelevant_answer)
|
[Incorrect](https://app.dosu.dev/response-feedback/83cea5b0-8b7c-4432-a597-c29c846fbd7a?feedback_type=incorrect_sources)
|
[Verbose](https://app.dosu.dev/response-feedback/83cea5b0-8b7c-4432-a597-c29c846fbd7a?feedback_type=too_verbose)
|
[Hallucination](https://app.dosu.dev/response-feedback/83cea5b0-8b7c-4432-a597-c29c846fbd7a?feedback_type=hallucination)
| [Report
🐛](https://app.dosu.dev/response-feedback/83cea5b0-8b7c-4432-a597-c29c846fbd7a?feedback_type=bug_report)
|
[Other](https://app.dosu.dev/response-feedback/83cea5b0-8b7c-4432-a597-c29c846fbd7a?feedback_type=other)</sup> [](https://go.dosu.dev/discord-bot) [!
[Share on
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/incubator-hugegraph/issues/2822)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]