(final String[] args)
| 39 | } |
| 40 | |
| 41 | public static void main(final String[] args) { |
| 42 | DgraphClient dgraphClient = createDgraphClient(false); |
| 43 | |
| 44 | // Initialize |
| 45 | dgraphClient.alter(Operation.newBuilder().setDropAll(true).build()); |
| 46 | |
| 47 | // Set schema |
| 48 | String schema = "name: string @index(exact) ."; |
| 49 | Operation operation = Operation.newBuilder().setSchema(schema).build(); |
| 50 | dgraphClient.alter(operation); |
| 51 | |
| 52 | Gson gson = new Gson(); // For JSON encode/decode |
| 53 | Transaction txn = dgraphClient.newTransaction(); |
| 54 | try { |
| 55 | // Create data |
| 56 | Person p = new Person(); |
| 57 | p.name = "Alice"; |
| 58 | |
| 59 | // Serialize it |
| 60 | String json = gson.toJson(p); |
| 61 | |
| 62 | // Run mutation |
| 63 | Mutation mutation = |
| 64 | Mutation.newBuilder().setSetJson(ByteString.copyFromUtf8(json.toString())).build(); |
| 65 | txn.mutate(mutation); |
| 66 | txn.commit(); |
| 67 | |
| 68 | } finally { |
| 69 | txn.discard(); |
| 70 | } |
| 71 | // Query |
| 72 | String query = |
| 73 | "query all($a: string){ |
| 74 | " + "all(func: eq(name, $a)) { |
| 75 | " + " name |
| 76 | " + " } |
| 77 | " + "}"; |
| 78 | Map<String, String> vars = Collections.singletonMap("$a", "Alice"); |
| 79 | Response res = dgraphClient.newTransaction().queryWithVars(query, vars); |
| 80 | |
| 81 | // Deserialize |
| 82 | People ppl = gson.fromJson(res.getJson().toStringUtf8(), People.class); |
| 83 | |
| 84 | // Print results |
| 85 | System.out.printf("people found: %d |
| 86 | ", ppl.all.size()); |
| 87 | ppl.all.forEach(person -> System.out.println(person.name)); |
| 88 | } |
| 89 | |
| 90 | static class Person { |
| 91 | String name; |
nothing calls this directly
no test coverage detected