gpt4 book ai didi

java - Java 中的 Dgraph。如何运行原始字符串变异查询?

转载 作者:行者123 更新时间:2023-12-02 09:47:21 27 4
gpt4 key购买 nike

我需要能够在不使用 newBuilder() 的情况下运行原始字符串突变查询:

Gson gson = new Gson();
String json = gson.toJson(newEmployer);
Transaction newTransaction = this.dgraphClient.newTransaction();
Mutation mu = Mutation.newBuilder().setSetJson(ByteString.copyFromUtf8(json.toString())).build();
newTransaction.mutate(mu);

我想运行:

String email = "ba@a.aa";
String userType = "JOB_SEEKER";
Transaction newTransaction = this.dgraphClient.newTransaction();
String query =
"{\n" +
" set { \n" +
" _:user <label> \"USER\" . \n" +
" _:user <userType> \"" + email + "\" . \n" +
" _:user <email> \"" + userType + "\" . \n" +
" }\n" +
"}";
Mutation mu = Mutation.parseFrom(ByteString.copyFromUtf8(query));
newTransaction.mutate(mu);

但是我在运行时收到错误:“在解析协议(protocol)消息时,输入在字段中间意外结束。这可能意味着输入已被截断,或者嵌入式消息误报了自己的长度”

最佳答案

在 gRPC 客户端(例如 dgraph4j)中为突变设置 N-Quad 三元组时,您只需指定换行符分隔的三元组本身并将它们传递给 Mutation#setSetNquads。它们没有被 set 包围。换句话说,而不是这个:

{
set {
_:user <label> "USER" .
_:user <userType> "USER_TYPE" .
_:user <email> "ba@a.aa" .
}
}

您只需要三元组:

_:user <label> "USER" .
_:user <userType> "USER_TYPE" .
_:user <email> "ba@a.aa" .

它在 Java 代码中的样子如下:

String email = "ba@a.aa";
String userType = "JOB_SEEKER";
Transaction newTransaction = this.dgraphClient.newTransaction();
String triples =
"_:user <label> \"USER\" .\n" +
"_:user <userType> \"" + email + "\" .\n" +
"_:user <email> \"" + userType + "\" .";
Mutation mu =
Mutation.newBuilder()
.setSetNquads(ByteString.copyFromUtf8(triples))
.build();
Assigned assigned = newTransaction.mutate(mu);

第一个带有 { set { ... } } 的突变格式适用于 HTTP 客户端,其中包括 Dgraph Ratel 内的突变或带有 curl 的突变。

有关 Dgraph 突变的更多信息可在突变文档中找到:https://docs.dgraph.io/mutations/

关于java - Java 中的 Dgraph。如何运行原始字符串变异查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56550043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com