简体   繁体   English

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

[英]Dgraph in Java. How to run raw string mutation query?

I need do be able to run raw string mutation queries without using of newBuilder(): 我需要能够在不使用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);

I want to run: 我要跑步:

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);

But I get the error on run-time: "While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length." 但是我在运行时遇到了一个错误:“在解析协议消息时,输入在字段中间意外终止。这可能意味着输入已被截断,或者嵌入式消息错误地报告了其自身的长度。”

When setting N-Quad Triples for mutations in gRPC clients such as dgraph4j, you only need to specify the newline-separated triples themselves and pass them to Mutation#setSetNquads . 在gRPC客户端(例如dgraph4j)中为突变设置N-Quad三元组时,您只需指定换行符分隔的三元组本身,然后将其传递给Mutation#setSetNquads They are not surrounded by set . 他们没有被set包围。 In other words, instead of this: 换句话说,代替这个:

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

You only need the triples: 您只需要三元组:

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

Here's how it would look like in your Java code: 这是您的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);

The first mutation format with { set { ... } } is for HTTP clients, which includes mutations within Dgraph Ratel or with curl . 带有{ set { ... } }的第一种突变格式适用于HTTP客户端,包括Dgraph Ratel内或curl突变。

More information about Dgraph mutations is available in the mutation docs: https://docs.dgraph.io/mutations/ 有关Dgraph突变的更多信息,请参见突变文档: https ://docs.dgraph.io/mutations/

I've found some solution, it is not string but it works. 我找到了一些解决方案,它不是字符串,但可以工作。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM