I would like to build a graph for keeping user phone and phone graph. The node is defined as a phone number, and the Edge could be defined as from_phone call to_phone with earliest call time. The constraint is every phone node should be unique, and every edge should also be unique.
For each edge to be inserted into DGraph, first check and persist the phone node, and then insert the edge.
But the problem is I could not find the way to retrieve edges from the UID of given phone node. Can anyone help me out? Thanks.
The demo could can be simply as below:
import com.google.gson.Gson;
import com.google.protobuf.ByteString;
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GraphDemo {
public static String getUidForPhone(String phone, DgraphClient dgraphClient) {
DgraphClient.Transaction txn = dgraphClient.newTransaction();
String query =
"\n"
+ " {\n"
+ " get(func: eq(phone_number, \"%s\")) {\n"
+ " uid\n"
+ " }\n"
+ " }\n";
query = String.format(query, phone);
String uid = "";
try {
DgraphProto.Response resp = txn.query(query);
Gson gson = new Gson();
Demo.Decode1 decode1 = gson.fromJson(resp.getJson().toStringUtf8(), Demo.Decode1.class);
// assertTrue(decode1.get.size() <= 1);
if (decode1.get.size() == 1) {
uid = decode1.get.get(0).uid;
} else {
String nqs =
"\n"
+ " _:phone <phone_number> \"%s\" .\n";
nqs = String.format(nqs, phone);
DgraphProto.Mutation mu = DgraphProto.Mutation.newBuilder().setSetNquads(ByteString.copyFromUtf8(nqs)).build();
DgraphProto.Assigned assigned = txn.mutate(mu);
uid = assigned.getUidsOrThrow("phone");
}
System.out.println("phone=" + phone + " uid=" + uid);
txn.commit();
} finally {
txn.discard();
}
return uid;
}
public static void main(String[] args) throws Exception {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphGrpc.DgraphBlockingStub blockingStub = DgraphGrpc.newBlockingStub(channel);
DgraphClient dgraphClient = new DgraphClient(Collections.singletonList(blockingStub));
System.out.println(dgraphClient);
String schema =
" phone_number: string @index(exact) .\n"
+ " is_black: int @index(int) .\n";
DgraphProto.Operation op = DgraphProto.Operation.newBuilder().setSchema(schema).build();
dgraphClient.alter(op);
List<String> phoneList = new ArrayList<>();
phoneList.add("13675878111");
phoneList.add("13312345678");
phoneList.add("13876541234");
//persisteThePhoneNodes(phoneList, dgraphClient);
List<String> edgeList = new ArrayList<>();
edgeList.add("13675878111,13312345678,called,2017-10-12T12:12:20");
edgeList.add("13675878111,13876541234,contact,2017-08-21T15:21:02");
for (String edge : edgeList) {
String[] t = edge.split(",");
String fromPhone = t[0].trim();
String toPhone = t[1].trim();
String edgeType = t[2];
String createTime = t[3];
String fromUid = getUidForPhone(fromPhone, dgraphClient);
String toUid = getUidForPhone(toPhone, dgraphClient);
DgraphClient.Transaction txn = dgraphClient.newTransaction();
String query = "{\n" +
"\tget (func: eq(phone_number, \"" + fromPhone + "\")) {\n" +
"_predicate_" +
"\t}\n" +
"}";
DgraphProto.Response resp = txn.query(query);
System.out.println(resp);
// _:x <friend> _:y .
String nqs = " <" + fromUid + "> <" + edgeType + "> <" + toUid + "> (createTime=" + createTime + ") .";
System.out.println("nqs=" + nqs);
DgraphProto.Mutation mu = DgraphProto.Mutation.newBuilder().setSetNquads(ByteString.copyFromUtf8(nqs)).build();
DgraphProto.Assigned assigned = txn.mutate(mu);
txn.commit();
}
}
static class Decode1 {
ArrayList<Uids> get;
static class Uids {
String uid;
}
}
static class Decode2 {
List<Entry> all;
static class Entry {
String first;
String last;
int age;
}
}
}