@@ -49,7 +49,6 @@ grab via Maven:
49
49
<artifactId >dgraph4j</artifactId >
50
50
<version >2.0.1</version >
51
51
</dependency >
52
-
53
52
```
54
53
or Gradle:
55
54
``` groovy
@@ -140,10 +139,10 @@ side-effects.
140
139
``` java
141
140
Transaction txn = dgraphClient. newTransaction();
142
141
try {
143
- // Do something here
144
- // ...
142
+ // Do something here
143
+ // ...
145
144
} finally {
146
- txn. discard();
145
+ txn. discard();
147
146
}
148
147
```
149
148
@@ -173,8 +172,8 @@ This data will be serialized into JSON.
173
172
174
173
``` java
175
174
class Person {
176
- String name
177
- Person() {}
175
+ String name
176
+ Person() {}
178
177
}
179
178
```
180
179
@@ -189,10 +188,9 @@ person.name = "Alice";
189
188
Gson gson = new Gson ();
190
189
String json = gson. toJson(p);
191
190
// Run mutation
192
- Mutation mu =
193
- Mutation . newBuilder()
194
- .setSetJson(ByteString . copyFromUtf8(json. toString()))
195
- .build();
191
+ Mutation mu = Mutation . newBuilder()
192
+ .setSetJson(ByteString . copyFromUtf8(json. toString()))
193
+ .build();
196
194
txn.mutate (mu );
197
195
```
198
196
@@ -204,8 +202,8 @@ Mutation can be run using the `doRequest` function as well.
204
202
205
203
```java
206
204
Request request = Request . newBuilder()
207
- .addMutations(mu)
208
- .build();
205
+ .addMutations(mu)
206
+ .build();
209
207
txn.doRequest (request );
210
208
```
211
209
@@ -221,17 +219,17 @@ modified in this transaction. It is up to the user to retry transactions when th
221
219
Transaction txn = dgraphClient. newTransaction();
222
220
223
221
try {
224
- // …
225
- // Perform any number of queries and mutations
226
- // …
227
- // and finally …
228
- txn. commit()
222
+ // …
223
+ // Perform any number of queries and mutations
224
+ // …
225
+ // and finally …
226
+ txn. commit()
229
227
} catch (TxnConflictException ex ) {
230
- // Retry or handle exception.
228
+ // Retry or handle exception.
231
229
} finally {
232
- // Clean up. Calling this after txn.commit() is a no-op
233
- // and hence safe.
234
- txn. discard();
230
+ // Clean up. Calling this after txn.commit() is a no-op
231
+ // and hence safe.
232
+ txn. discard();
235
233
}
236
234
```
237
235
@@ -257,8 +255,8 @@ First we must create a `People` class that will help us deserialize the JSON res
257
255
258
256
```java
259
257
class People {
260
- List<Person > all;
261
- People () {}
258
+ List<Person > all;
259
+ People () {}
262
260
}
263
261
```
264
262
@@ -294,8 +292,8 @@ You can also use `doRequest` function to run the query.
294
292
295
293
```java
296
294
Request request = Request . newBuilder()
297
- .setQuery(query)
298
- .build();
295
+ .setQuery(query)
296
+ .build();
299
297
txn.doRequest (request );
300
298
```
301
299
@@ -312,15 +310,14 @@ https://docs.dgraph.io/mutations/#upsert-block.
312
310
String query = " query {\n " +
313
311
" user as var(func: eq(email, \" [email protected] \" ))\n " +
314
312
" }\n " ;
315
- Mutation mu =
316
- Mutation . newBuilder()
317
- .setSetNquads(
ByteString . copyFromUtf8(
" uid(user) <email> \" [email protected] \" ." ))
318
- .build();
313
+ Mutation mu = Mutation . newBuilder()
314
+ .setSetNquads(
ByteString . copyFromUtf8(
" uid(user) <email> \" [email protected] \" ." ))
315
+ .build();
319
316
Request request = Request . newBuilder()
320
- .setQuery(query)
321
- .addMutations(mu)
322
- .setCommitNow(true )
323
- .build();
317
+ .setQuery(query)
318
+ .addMutations(mu)
319
+ .setCommitNow(true )
320
+ .build();
324
321
txn.doRequest (request );
325
322
```
326
323
@@ -335,11 +332,10 @@ See more about Conditional Upsert [Here](https://docs.dgraph.io/mutations/#condi
335
332
String query = " query {\n " +
336
333
" user as var(func: eq(email, \" [email protected] \" ))\n " +
337
334
" }\n " ;
338
- Mutation mu =
339
- Mutation . newBuilder()
340
- .setSetNquads(
ByteString . copyFromUtf8(
" uid(user) <email> \" [email protected] \" ." ))
341
- .setCond(" @if(eq(len(user), 1))" )
342
- .build();
335
+ Mutation mu = Mutation . newBuilder()
336
+ .setSetNquads(
ByteString . copyFromUtf8(
" uid(user) <email> \" [email protected] \" ." ))
337
+ .setCond(" @if(eq(len(user), 1))" )
338
+ .build();
343
339
Request request = Request . newBuilder()
344
340
.setQuery(query)
345
341
.addMutations(mu)
@@ -357,11 +353,11 @@ Read [this forum post][deadline-post] for more details.
357
353
channel = ManagedChannelBuilder . forAddress(" localhost" , 9080 ). usePlaintext(true ). build();
358
354
DgraphGrpc . DgraphStub stub = DgraphGrpc . newStub(channel);
359
355
ClientInterceptor timeoutInterceptor = new ClientInterceptor (){
360
- @Override
361
- public <ReqT , RespT > ClientCall<ReqT , RespT > interceptCall (
362
- MethodDescriptor<ReqT , RespT > method , CallOptions callOptions , Channel next ) {
363
- return next. newCall(method, callOptions. withDeadlineAfter(500 , TimeUnit . MILLISECONDS ));
364
- }
356
+ @Override
357
+ public <ReqT , RespT > ClientCall<ReqT , RespT > interceptCall (
358
+ MethodDescriptor<ReqT , RespT > method , CallOptions callOptions , Channel next ) {
359
+ return next. newCall(method, callOptions. withDeadlineAfter(500 , TimeUnit . MILLISECONDS ));
360
+ }
365
361
};
366
362
stub. withInterceptors(timeoutInterceptor);
367
363
DgraphClient dgraphClient = new DgraphClient (stub);
@@ -381,7 +377,7 @@ DgraphStub stub = DgraphGrpc.newStub(channel);
381
377
// use MetadataUtils to augment the stub with headers
382
378
Metadata metadata = new Metadata ();
383
379
metadata. put(
384
- Metadata . Key . of(" auth-token" , Metadata . ASCII_STRING_MARSHALLER ), " the-auth-token-value" );
380
+ Metadata . Key . of(" auth-token" , Metadata . ASCII_STRING_MARSHALLER ), " the-auth-token-value" );
385
381
stub = MetadataUtils . attachHeaders(stub, metadata);
386
382
387
383
// create the DgraphClient wrapper around the stub
@@ -399,9 +395,9 @@ The helper method takes an existing mutation, and returns a new mutation
399
395
with the deletions applied.
400
396
401
397
```java
402
- Mutation mu = Mutation .newBuilder(). build()
403
- mu = Helpers . deleteEdges(mu, uid, " friends" , " loc" );
404
- dgraphClient. newTransaction(). mutate(mu);
398
+ Mutation mu = Mutation .newBuilder(). build()
399
+ mu = Helpers . deleteEdges(mu, uid, " friends" , " loc" );
400
+ dgraphClient. newTransaction(). mutate(mu);
405
401
```
406
402
407
403
### Closing the DB Connection
@@ -456,16 +452,16 @@ If you would like to see the latency for either a mutation or
456
452
query request, the latency field in the returned result can be helpful. Here is an example to log
457
453
the latency of a query request:
458
454
```java
459
- Response resp = txn. query(query);
460
- Latency latency = resp. getLatency();
461
- logger. info(" parsing latency:" + latency. getParsingNs());
462
- logger. info(" processing latency:" + latency. getProcessingNs());
463
- logger. info(" encoding latency:" + latency. getEncodingNs());
455
+ Response resp = txn. query(query);
456
+ Latency latency = resp. getLatency();
457
+ logger. info(" parsing latency:" + latency. getParsingNs());
458
+ logger. info(" processing latency:" + latency. getProcessingNs());
459
+ logger. info(" encoding latency:" + latency. getEncodingNs());
464
460
```
465
461
Similarly you can get the latency of a mutation request:
466
462
```java
467
- Assigned assignedIds = dgraphClient. newTransaction(). mutate(mu);
468
- Latency latency = assignedIds. getLatency();
463
+ Assigned assignedIds = dgraphClient. newTransaction(). mutate(mu);
464
+ Latency latency = assignedIds. getLatency();
469
465
```
470
466
471
467
## Development
0 commit comments