Delete operation in Dgraph using GRPC

Table of contents
Reading Time: 2 minutes

Deletion in dgraph is an easy operation we just have to keep few things in mind before deleting anything in dgraph.
Before I explain how can we delete I am going to explain the different scenario.

For example,

  1. Delete the edge
  2. Delete the node.
  3. Delete the one value in a list

We will take a scenario where we have a person who has the following attribute

  • uid – string
  • personType – string
  • name – string
  • age – int
  • hobbies – list of string
  • address – User-defined Class Address(houseNumber, street, city, state, addressType, uid)

Where the address is an edge from Person node to address node. Now that we are aware of the structure of the dgraph so no more waiting we will go ahead and do some deletion.

Deleting the edge

We have to delete the address edge. To delete it we will need the uid of the person whose address we have to delete and the uid of the address node.

So the POJO structure should be similar to it.

{ “uid”: “0x123”, “address”: { “uid”: “0x456” } }

So POJO structure that we have made.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


@Builder
@Getter
@AllArgsConstructor
public final class DeleteAddressPojo {
private final String uid;
private final DeleteAddress address;
}
@Builder
@Getter
public final class DeleteAddress {
private final String uid;
}
view raw

deleteAddress

hosted with ❤ by GitHub

Calling the method to finally delete the edge.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


DeleteAddressPojo deletePerson = DeleteAddressPojo.builder()
.address(DeleteAddress.builder().uid(addressUid).build()).uid(uid).build();
if (daoOperation.deleteNode(deletePerson)) {
System.out.println("Delete Transaction completed");
}
view raw

deleteCommand

hosted with ❤ by GitHub

Delete method which creates the transaction to do the mutation.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


public <T> boolean deleteNode(final T element) {
try (Transaction txn = dgraphClient.newTransaction()) {
try {
final DgraphProto.Mutation mutation = getDeleteMutation(element);
txn.mutate(mutation);
txn.commit();
return true;
} catch (StatusRuntimeException | TxnConflictException | JsonProcessingException dgraphException) {
txn.discard();
throw new RuntimeException("Unable to persist the transaction ", dgraphException);
}
}
}
view raw

DeleteMethod

hosted with ❤ by GitHub

This process only deletes the edge between person and address. But if we want completely remove the address node then we have to delete the address node as well.

Now see how to delete the Node.

Delete the Node

We just have to provide the uid of the address node. The deleteNode method is the same as mentioned above.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


if (daoOperation.deleteNode(Address.builder().uid(addressDeleteUid).build())) {
System.out.println("Delete Transaction for address completed");
}
view raw

deletenode

hosted with ❤ by GitHub

Delete the value in the List

To delete the value of the list then we have to make the POJO similar to

{ “uid”: “0xd”, #UID of person. “hobbies”: “Apple” }



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


@Builder
@Getter
public final class DeleteListValue {
private final String uid;
private final String hobbies;
}
view raw

gistfile1.txt

hosted with ❤ by GitHub

we will call the method to delete the particular value of the list. we just need the person uid and the value that we need to delete in the list. In the list of hobbies, I want to delete the Drawing hobby.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


DeleteListValue deleteList = DeleteListValue.builder().uid(uid)
.hobbies("Drawing").build();
if (daoOperation.deleteNode(deleteList)) {
]System.out.println("Update Transaction completed");
]}
view raw

DeleteValue

hosted with ❤ by GitHub

Conclusion

We have learned the deletion of node, edge, and value in the list using the GRPC client. So if you need more understanding about GRPC client you can refer to the How to use Dgraph in your java application!! blog. For the complete code reference, you can visit GitHub repo.

Reference

Dgraph Offical Site 

Knoldus-Scala-Spark-Services

Written by 

Priyanka Thakur is a Software Consultant having 6 months of experience currently working at Knoldus Software LLP. She has done MCA at BVICAM, GGSIPU. She has a graduation degree in BCA from JIMS, GGSIPU. She is familiar with programming language' s such as C, Java, and Scala. She also has interest in Angular and Docker and currently working on Logam. She is dedicated, focused and hardworking person. Her interests are in to face new programming challenges because she believes these challenges produce opportunity. She is keen to learn new technologies. In her leisure time, she prefers reading about mythology, watching movies.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading