samples folder, which contains an end-to-end example of using
the Dgraph Java client. Follow the instructions in the
README
of that project.
DgraphClient and the async client DgraphAsyncClient. A DgraphClient or
DgraphAsyncClient can be initialized by passing it a list of
DgraphBlockingStub clients. The anyClient() API can randomly pick a stub,
which can then be used for gRPC operations.
0) with the following
method:
0. In order to log into a
different namespace, use the loginIntoNamespace method on the client:
dgraphClient object can be used to do any further
operations.
openssl tool.
First, let’s install the openssl tool:
Operation object, set the schema and pass it to
DgraphClient#alter method.
setRunInBackground(true) as shown below before calling
alter. You can find more details
here.
Operation contains other fields as well, including drop predicate and drop
all. Drop all is useful if you wish to discard all the data, and start from a
clean slate, without bringing the instance down.
DgraphClient and the asynchronous
DgraphAsyncClient clients support the two types of transactions by providing
the newTransaction and the newReadOnlyTransaction APIs. Creating a
transaction is a local operation and incurs no network overhead.
In most of the cases, the normal read-write transactions is used, which can have
any number of query or mutate operations. However, if a transaction only has
queries, you might benefit from a read-only transaction, which can share the
same read timestamp across multiple such read-only transactions and can result
in lower latencies.
For normal read-write transactions, it’s a good practice to call
Transaction#discard() in a finally block after running the transaction.
Calling Transaction#discard() after Transaction#commit() is a no-op and you
can call discard() multiple times with no additional side-effects.
Transaction.discard,
which is equivalent to a no-op.
Transaction#mutate runs a mutation. It takes in a Mutation object, which
provides two main ways to set data: JSON and RDF N-Quad. You can choose
whichever way is convenient.
We’re going to use JSON. First we define a Person class to represent a person.
This data is serialized into JSON.
Person object, serialize it and use it in Mutation
object.
CommitNow field in Mutation object to indicate
that the mutation must be immediately committed.
Mutation can be run using the doRequest function as well.
Transaction#commit() method. If your
transaction consisted solely of calls to Transaction#query(), and no calls to
Transaction#mutate(), then calling Transaction#commit() isn’t necessary.
An error is returned if other transactions running concurrently modify the same
data that was modified in this transaction. It is up to the user to retry
transactions when they fail.
Transaction#query(). You need to pass in a DQL
query string, and a map (optional, could be empty) of any variables that you
might want to set in the query.
The response would contain a JSON field, which has the JSON encoded result.
You need to decode it before you can do anything useful with it.
Let’s run the following query:
People class that helps us deserialize the JSON result:
doRequest function to run the query.
queryRDF() or
queryRDFWithVars(). The response contains the getRdf() method, which
provides the RDF encoded output.
uid values only, use a JSON format response.uid is 0x2):
txn.doRequest function allows you to run upserts consisting of one query
and one mutation. Variables can be defined in the query and used in the
mutation. You could also use txn.doRequest to perform a query followed by a
mutation.
@if directive. The mutation is executed only when the specified condition is
true. If the condition is false, the mutation is silently ignored.
See more about Conditional Upsert
Here.
Helpers#deleteEdges to delete
multiple edges corresponding to predicates on a node with the given UID. The
helper method takes an existing mutation, and returns a new mutation with the
deletions applied.
ManagedChannel#shutdown on the gRPC channel
object created when creating a Dgraph client.
DgraphAsyncClient class. The usage is almost exactly the
same as the DgraphClient (show in previous section) class. The main
differences is that the DgraphAsyncClient#newTransacation() returns an
AsyncTransaction class. The API for AsyncTransaction is exactly
Transaction. The only difference is that instead of returning the results
directly, it returns immediately with a corresponding CompletableFuture<T>
object. This object represents the computation which runs asynchronously to
yield the result in the future. Read more about CompletableFuture<T> in the
Java 8 documentation.
Here is the asynchronous version of the preceding code, which runs a query.
samples/concurrent-modification. In order to run
this example, execute the following maven command from the
‘concurrent-modification’ folder.