Deletes are supported at a record level in Hudi with 0.5.1 release. This blog is a “how to” blog on how to delete records in hudi. Deletes can be done with 3 flavors: Hudi RDD APIs, with Spark data source and with DeltaStreamer.
Delete using RDD Level APIs
If you have embedded HoodieWriteClient , then deletion is as simple as passing in a _JavaRDD_ to the delete api.
// Fetch list of HoodieKeys from elsewhere that needs to be deleted// convert to JavaRDD if required. JavaRDD<HoodieKey> toBeDeletedKeysList<WriteStatus>statuses=writeClient.delete(toBeDeletedKeys,commitTime);
Deletion with Datasource
Now we will walk through an example of how to perform deletes on a sample dataset using the Datasource API. Quick Start has the same example as below. Feel free to check it out.
Step 4 : Query data. Load the data files into a DataFrame.
valroViewDF=spark.read.format("org.apache.hudi").load(basePath+"/*/*/*/*")roViewDF.createOrReplaceTempView("hudi_ro_table")spark.sql("select count(*) from hudi_ro_table").show()// should return 10 (number of records inserted above)
valriderValue=spark.sql("select distinct rider from hudi_ro_table").show()// copy the value displayed to be used in next step
Step 5 : Fetch records that needs to be deleted, with the above rider value. This example is just to illustrate how to delete. In real world, use a select query using spark sql to fetch records that needs to be deleted and from the result we could invoke deletes as given below. Example rider value used is “rider-213”.
valdf=spark.sql("select uuid, partitionPath from hudi_ro_table where rider = 'rider-213'")
// Replace the above query with any other query that will fetch records to be deleted.
Step 7 : Reload the table and verify that the records are deleted
valroViewDFAfterDelete=spark.read.format("org.apache.hudi").load(basePath+"/*/*/*/*")roViewDFAfterDelete.createOrReplaceTempView("hudi_ro_table")spark.sql("select uuid, partitionPath from hudi_ro_table where rider = 'rider-213'").show()// should not return any rows
Deletion with HoodieDeltaStreamer
Deletion with HoodieDeltaStreamer takes the same path as upsert and so it relies on a specific field called “_hoodie_is_deleted” of type boolean in each record.
If a record has the field value set to false or it’s not present, then it is considered a regular upsert
if not (if the value is set to true ), then its considered to be deleted record.
This essentially means that the schema has to be changed for the source, to add this field and all incoming records are expected to have this field set. We will be working to relax this in future releases.
These are one time changes. Once these are in, then the DeltaStreamer pipeline will handle both upserts and deletions within every batch. Each batch could contain a mix of upserts and deletes and no additional step or changes are required after this. Note that this is to perform hard deletion instead of soft deletion.