Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CRUD Operation With Dgraph


Published on

The session is about how you can use dgraph in your project as a database with java. It has detail information's about the dgraph sync client and overview of the asynchronous client. It also has information about dgraph HTTP endpoints.

Published in: Software
  • Be the first to comment

  • Be the first to like this

CRUD Operation With Dgraph

  1. 1. CRUD Operation with Dgraph Priyanka Thakur Software Consultant
  2. 2. Agenda ● Brief Introduction to DGraph ● Client in dgraph ● Different type of Dgraph client. ● How to perform CRUD operation with dgraph using java
  3. 3. Brief Introduction about Dgraph ● DGraph is a scalable, distributed, highly available and fast graph database. ● RDF Triple - Triple is any statement in form of SPO, i.e. "subject/predicate/object", every part of the triple has a URI associated with it. ● Schema - represent types of the predicates that are stored by the DGraph. ● Mutation - Adding, deleting or updating data in DGraph, Can be done using RDF triples or JSON. ● GQL+/- rich language that is used to interact with the graph databases.
  4. 4. Client in Dgraph. Clients can communicate with the server in two different ways: ● Via gRPC. Internally this uses Protocol Buffers (the proto file used by Dgraph is located at api.proto). ● Via HTTP. There are various endpoints, each accepting and returning JSON. There is a one to one correspondence between the HTTP endpoints and the gRPC service methods.
  5. 5. Raw HTTP localhost:8080/query localhost:8080/mutate localhost:8080/alter localhost:8080/commit
  6. 6. Alter & Query Alter API endpoint to add the schema. curl -X POST localhost:8080/alter -d 'name: string @index(term) .' Query API endpoint to get balance information for a user whose name is “Alice” or “Bob”. curl -X POST localhost:8080/query -d $' { balances(func: anyofterms(name, "Alice Bob")) { uid name balance } }' | jq
  7. 7. Mutation Mutation API endpoint to add the balance information of two user. curl -X POST localhost:8080/mutate/4 -d $' { set { <0x1> <balance> "110" . <0x2> <balance> "60" . } } ' | jq
  8. 8. Mutation Output { "data": { "code": "Success", "message": "Done", "uids": {} }, "extensions": { "server_latency": { "parsing_ns": 17000, "processing_ns": 4722207 }, "txn": { "start_ts": 4, "keys": [ "i4elpex2rwx3", "nkvfdz3ltmvv" ] "preds": [ "1-balance", "1-_predicate_" ] } }
  9. 9. Commit Commit API endpoint to persists the previous mutation query data. curl -X POST localhost:8080/commit/4 -d $' [ "i4elpex2rwx3", "nkvfdz3ltmvv" ]' | jq
  10. 10. Communcation of your application through gRPC. This library supports two styles of clients, ● synchronous client DgraphClient ● async client DgraphAsyncClient.
  11. 11. Using the Synchronous Client Create the client ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build(); DgraphStub stub = DgraphGrpc.newStub(channel); DgraphClient dgraphClient = new DgraphClient(Collections.singletonList(stub));
  12. 12. Alter the database Alter the database String schema = "name: string @index(exact) ."; Operation op = Operation.newBuilder().setSchema(schema).build(); dgraphClient.alter(op); dgraphClient.alter(Operation.newBuilder().setDropAll(true).build());
  13. 13. Transaction There are two types of transactions in dgraph. ● read-only transactions. ● the transactions that change data in dgraph with mutate operations. Transaction txn = dgraphClient.newReadOnlyTransaction(); Transaction txn = dgraphClient.newTransaction();
  14. 14. Run a mutation We have a person class. class Person { String name Person() {} }
  15. 15. Run a mutation // Create data Person p = new Person(); = "Alice"; // Serialize it Gson gson = new Gson(); String json = gson.toJson(p); // Run mutation Mutation mu = Mutation.newBuilder() .setSetJson(ByteString.copyFromUtf8(json)) .build(); txn.mutate(mu);
  16. 16. Commit a transaction A transaction can be committed using the Transaction#commit() method. txn.commit();
  17. 17. Reference ● DGraph docs