我需要能够在不使用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);
但是我在运行时遇到错误:“在解析协议消息时,输入意外终止于字段中间。这可能意味着输入已被截断,或者嵌入式消息错误地报告了自己的长度。”
答案 0 :(得分:1)
在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/
答案 1 :(得分:0)
我找到了一些解决方案,它不是字符串,但是可以工作。
JSONObject query = new JSONObject();
query.put("label", "USER");
query.put("userType", userType);
query.put("email", email);
Mutation mu = Mutation.newBuilder().setSetJson(ByteString.copyFromUtf8(query.toJSONString())).build();