dosubot[bot] commented on issue #2776:
URL:
https://github.com/apache/incubator-hugegraph/issues/2776#issuecomment-2908711864
<!-- Answer -->
在 HugeGraph 中,如果顶点的 idStrategy 是 PRIMARY_KEY,顶点的 ID
实际上是由「顶点类型ID」和「primaryKeys 的值」拼接生成的。插入边时,source 和 target 需要填写顶点的 ID,你可以用
primaryKeys 的值来拼接出顶点 ID,无需额外查询。
### 具体实现方式
#### 1. 顶点 schema 示例
假设有如下顶点定义:
```java
schema.vertexLabel("person")
.properties("name", "age", "city")
.primaryKeys("name")
.create();
```
#### 2. 构造顶点 ID
顶点 ID 的格式为:`<vertexLabelId>:<primaryKeyValues>`
比如,`person` 顶点的 primaryKey 是 `name`,那么 "marko" 这个顶点的 ID 可能是 `person:marko`。
在 Java 代码中可以这样拼接:
```java
String personLabelId =
graph.schema().getVertexLabel("person").id().asString();
String markoId = String.format("%s:%s", personLabelId, "marko");
String joshId = String.format("%s:%s", personLabelId, "josh");
```
参考:[Example2.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-example/src/main/java/org/apache/hugegraph/example/Example2.java)
#### 3. 插入边时使用顶点 ID
插入边时,直接用拼接出来的顶点 ID 作为 source 和 target:
```java
graph.addEdge("knows")
.source(markoId)
.target(joshId)
.property("date", "2016-01-10")
.property("weight", 0.5)
.create();
```
#### 4. Loader 批量导入场景
如果用 loader 批量导入,建议在边数据中直接写 primaryKey 的值(如 name),然后通过 mapping 文件配置
source/target 字段如何拼接顶点 ID。
详细说明见官方文档:[loader.md](https://github.com/apache/incubator-hugegraph-doc/blob/master/content/en/docs/quickstart/hugegraph-loader.md)
> “The id strategy of person and software is still specified as PRIMARY_KEY,
but the id column of the person table and software table is used as the primary
key attribute of the vertex, so that the id can be generated by directly
splicing p_id and s_id with the label of the vertex when importing an edge.”
#### 5. 源码实现参考
源码中,`getVertexId` 方法会根据 primaryKeys 拼接顶点 ID,具体见
[VertexAPI.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/graph/VertexAPI.java):
```java
if (idStrategy == IdStrategy.PRIMARY_KEY) {
// 拼接 primaryKeys 的值
String value = ConditionQuery.concatValues(pkValues);
return SplicingIdGenerator.splicing(labelId, value);
}
```
---
**总结**:
插入边时,source 和 target 需要的是顶点 ID。对于 PRIMARY_KEY
策略的顶点,你只需用「顶点类型ID」+「primaryKeys 的值」拼接即可,无需额外查表。
如需更详细的批量导入配置或其他语言示例,可以补充说明。
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://go.dosu.dev/dosubot), mention
@dosu-bot.*
---
<sup>Help Dosu learn! Give it feedback: [Great
Response](https://app.dosu.dev/response-feedback/5746c55c-81ad-4f28-8ed8-733c0d0429e9?feedback_type=great_response)
| [Irrelevant
Answer](https://app.dosu.dev/response-feedback/5746c55c-81ad-4f28-8ed8-733c0d0429e9?feedback_type=irrelevant_answer)
| [Incorrect
Sources](https://app.dosu.dev/response-feedback/5746c55c-81ad-4f28-8ed8-733c0d0429e9?feedback_type=incorrect_sources)
| [Too
Verbose](https://app.dosu.dev/response-feedback/5746c55c-81ad-4f28-8ed8-733c0d0429e9?feedback_type=too_verbose)
|
[Hallucination](https://app.dosu.dev/response-feedback/5746c55c-81ad-4f28-8ed8-733c0d0429e9?feedback_type=hallucination)
| [Bug
Report](https://app.dosu.dev/response-feedback/5746c55c-81ad-4f28-8ed8-733c0d0429e9?feedback_type=bug_report)
|
[Other](https://app.dosu.dev/response-feedback/5746c55c-81ad-4f28-8ed8-733c0d0429e9?feedback_type=other)</sup>
--
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]