若在此时, 我发现我的数据弄错了
用户nameOFen="ZhaoYao"的用户age实际应该为20, 此时, 同样是用NQuad来更新数据
func main() { flag.Parse() conn, err := grpc.Dial(*dgraph, grpc.WithInsecure()) if err != nil { log.Fatal(err) } defer conn.Close() dg := dgo.NewDgraphClient(api.NewDgraphClient(conn)) ctx := context.Background() mu := &api.Mutation{ CommitNow: true, } type arrays struct{ Uids []Person `json:"info"` } var1 := make(map[string]string) var1["$enname"] = "ZhaoYao" q1 := `query wanghainfo($enname: string){ info(func: eq(nameOFen, $enname)){ uid } }` resp, err := dg.NewTxn().QueryWithVars(ctx,q1,var1) if err != nil { log.Println(err) } var r1 arrays err = json.Unmarshal(resp.Json, &r1) if err != nil{ log.Println(err) } Uid_wangha := r1.Uids[0].Uid log.Println(Uid_wangha) t := fmt.Sprintf("<%s> <age> \"%s\" .",Uid_wangha,"20") mu.SetNquads = []byte(t) assign,err := dg.NewTxn().Mutate(ctx,mu) if err != nil{ log.Println(err) } fmt.Printf("assign: %v",assign) }可以看到结果:
更新成功
删除用户删除用户时, dgraph规定了无法删除Uid <subject> , 只能删除<predicate>和<object>
使用了&api.Mutation内规定的方法去删除
func main() { flag.Parse() conn, err := grpc.Dial(*dgraph, grpc.WithInsecure()) if err != nil { log.Fatal(err) } defer conn.Close() dg := dgo.NewDgraphClient(api.NewDgraphClient(conn)) ctx := context.Background() vars := make(map[string]string) vars["$enname"] = "ZhaoYao" q := `query wanghainfo($enname: string){ info(func: eq(nameOFen, $enname)){ uid } }` resp, err := dg.NewTxn().QueryWithVars(ctx,q,vars) if err != nil { log.Println(err) } type arrays struct{ Uids []Person `json:"info"` } var r arrays err = json.Unmarshal(resp.Json, &r) if err != nil{ log.Println(err) } log.Println(string(resp.Json)) log.Println(r.Uids[0].Uid) d := map[string]string{"uid":string(r.Uids[0].Uid)} pb, err := json.Marshal(d) mu := &api.Mutation{ CommitNow: true, DeleteJson: pb, } assign,err := dg.NewTxn().Mutate(ctx, mu) if err != nil{ log.Println(err) } fmt.Printf("assign: %v \n",assign) }再次查询, 数据已经消失
写在最后dgraph 是一个非常强的分布式 NoSql , 想弄懂了真的得看很多遍技术文档
以下链接可能在你学习的时候会有所帮助
Mutate : https://docs.dgraph.io/mutations/
Query : https://docs.dgraph.io/query-language/
Client : https://docs.dgraph.io/clients/
Dgo : https://gowalker.org/github.com/dgraph-io/dgo
Api :
Grpc : https://godoc.org/google.golang.org/grpc
RDF : https://www.w3.org/TR/n-quads/