Hi,
I need to add new edges during streaming process. what I need to do is that inter an edge if there is not an edge between two nodes. If there is already then do something else. If the nodes does not exist, then these nodes should be created as well.
I am trying to following the example codes here https://docs.dgraph.io/clients/#create-the-client. But I am quite new to Golang. How to represent an edge in Golang ? I understand that an edge can be represented in RDF form in a file. But I did not want to use file as intermediate step.
In this example Person type is defined. I guess following codes are used to create Person and other attribute nodes. How edges are created? I guess Friends should be an edge predicate, right ? How does this piece of codes work ?
In particular, how to define an edge in the type struct?
For example here
type DirectedEdge struct {
Entity uint64
Attr string
Value byte
ValueType uint32
ValueId uint64
Label string
Lang string Op DirectedEdge_Op // Set or Delete Facets []*facetsp.Facet
}
A struct in Go client is marshalled to JSON format. This struct would be marshalled to something like.
{"name":"Alice","age":26,"dob":"1980-01-01T23:00:00Z","married":true,"raw_bytes":"cmF3X2J5dGVz","friend":[{"name":"Bob","age":24},{"name":"Charlie","age":29}],"school":[{"name":"Crown Public School"}]}
Since the root node, doesn’t have a uid, a new node would be created with the properties name, married etc. Two new nodes would also be created for Bob and Charlie and connected to Alice using the friend edge.
You would have to query for the edge and if it doesn’t exist then you create one. You don’t need to think in terms of DirectedEdge as that is an internal data structure that we use.
Thx @pawan
What if I want to insert a new edge between an existing node and a new node? Can I create an instance of Person struct like following ? Does it automatically check whether the node with uid 0x100 and the new node (Bob) exist and insert an edge between them after creating Bot node?
type Person struct {
Uid string `json:"uid,omitempty"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Dob *time.Time `json:"dob,omitempty"`
Married bool `json:"married,omitempty"`
Raw []byte `json:"raw_bytes",omitempty`
Friends []Person `json:"friend,omitempty"`
Location loc `json:"loc,omitempty"`
School []School `json:"school,omitempty"`
}
.....
.....
_**p := Person{**_
_** UID: "0X100"**_
_** Friends: []Person{{**_
_** Name: "Bob",**_
_** Age: 24,**_
_** }},**_
pb, err := json.Marshal(person)
mu.SetJson = pb
assigned, err := dg.NewTxn().Mutate(ctx, mu)
}
No, the Go struct representation is not related to the dgraph schema. In Dgraph a uid edge can always be connected to multiple uids. Though, we have an open issue to only allow only one uid.