Skip to content

Commit

Permalink
Tweak example to add serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakjois committed Nov 16, 2017
1 parent 59ecdba commit ab4d772
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
3 changes: 2 additions & 1 deletion samples/DgraphJavaSample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ For more configuration options, and other details, refer to [docs.dgraph.io]
```
$ ./gradlew run
> Task :run
> Task :run
people found: 1
Alice
Expand Down
101 changes: 66 additions & 35 deletions samples/DgraphJavaSample/src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.protobuf.ByteString;
import io.dgraph.DgraphClient.Transaction;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphGrpc.DgraphBlockingStub;
import io.dgraph.DgraphProto.Mutation;
Expand All @@ -9,43 +12,71 @@
import io.dgraph.DgraphClient;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class App {
private static final String TEST_HOSTNAME = "localhost";
private static final int TEST_PORT = 9080;

public static void main(final String[] args) {
ManagedChannel channel = ManagedChannelBuilder.forAddress(TEST_HOSTNAME, TEST_PORT).usePlaintext(true).build();
DgraphBlockingStub blockingStub = DgraphGrpc.newBlockingStub(channel);
DgraphClient dgraphClient = new DgraphClient(Collections.singletonList(blockingStub));

// Set schema
Operation op = Operation.newBuilder().setSchema("name: string @index(exact) .").build();
dgraphClient.alter(op);

// Add data
JsonObject json = new JsonObject();
json.addProperty("name", "Alice");

Mutation mu =
Mutation.newBuilder()
.setCommitNow(true)
.setSetJson(ByteString.copyFromUtf8(json.toString()))
.build();
dgraphClient.newTransaction().mutate(mu);

// Query
String query = "{\n" + "me(func: eq(name, $a)) {\n" + " name\n" + " }\n" + "}";
Map<String, String> vars = Collections.singletonMap("$a", "Alice");
Response res = dgraphClient.newTransaction().query(query, vars);

// Verify data as expected
JsonParser parser = new JsonParser();
json = parser.parse(res.getJson().toStringUtf8()).getAsJsonObject();
String name = json.getAsJsonArray("me").get(0).getAsJsonObject().get("name").getAsString();
System.out.println(name);
}
private static final String TEST_HOSTNAME = "localhost";
private static final int TEST_PORT = 9080;

public static void main(final String[] args) {
ManagedChannel channel = ManagedChannelBuilder.forAddress(TEST_HOSTNAME, TEST_PORT).usePlaintext(true).build();
DgraphBlockingStub blockingStub = DgraphGrpc.newBlockingStub(channel);
DgraphClient dgraphClient = new DgraphClient(Collections.singletonList(blockingStub));

// Initialize
dgraphClient.alter(Operation.newBuilder().setDropAll(true).build());

// Set schema
String schema = "name: string @index(exact) .";
Operation op = Operation.newBuilder().setSchema(schema).build();
dgraphClient.alter(op);

Gson gson = new Gson(); // For JSON encode/decode

Transaction txn = dgraphClient.newTransaction();
try {
// Create data
Person p = new Person();
p.name = "Alice";

// Serialize it
String json = gson.toJson(p);

// Run mutation
Mutation mu =
Mutation.newBuilder()
.setSetJson(ByteString.copyFromUtf8(json.toString()))
.build();
txn.mutate(mu);
txn.commit();

} finally {
txn.discard();
}
// Query
String query = "{\n" + "all(func: eq(name, $a)) {\n" + " name\n" + " }\n" + "}";
Map<String, String> vars = Collections.singletonMap("$a", "Alice");
Response res = dgraphClient.newTransaction().query(query, vars);

// Deserialize
People ppl = gson.fromJson(res.getJson().toStringUtf8(), People.class);

// Print results
System.out.printf("people found: %d\n", ppl.all.size());
ppl.all.forEach(person -> System.out.println(person.name));
}

static class Person {
String name;
Person() {}
}

static class People {
List<Person> all;

People() {}
}
}

0 comments on commit ab4d772

Please sign in to comment.