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,
- Delete the edge
- Delete the node.
- 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.
@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; | |
} |
Calling the method to finally delete the edge.
DeleteAddressPojo deletePerson = DeleteAddressPojo.builder() | |
.address(DeleteAddress.builder().uid(addressUid).build()).uid(uid).build(); | |
if (daoOperation.deleteNode(deletePerson)) { | |
System.out.println("Delete Transaction completed"); | |
} | |
Delete method which creates the transaction to do the mutation.
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); | |
} | |
} | |
} |
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.
if (daoOperation.deleteNode(Address.builder().uid(addressDeleteUid).build())) { | |
System.out.println("Delete Transaction for address completed"); | |
} |
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” }
@Builder | |
@Getter | |
public final class DeleteListValue { | |
private final String uid; | |
private final String hobbies; | |
} | |
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.
DeleteListValue deleteList = DeleteListValue.builder().uid(uid) | |
.hobbies("Drawing").build(); | |
if (daoOperation.deleteNode(deleteList)) { | |
]System.out.println("Update Transaction completed"); | |
]} |
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
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.