- Overwrite all data
- Overwrite records for a specific time range
- Update a row using partial segment overshadowing
Prerequisites
Before you follow the steps in this tutorial, download Druid as described in Quickstart (local) and have it running on your local machine. You don’t need to load any data into the Druid cluster. You should be familiar with data querying in Druid. If you haven’t already, go through the Query data tutorial first.Load sample data
Load a sample dataset using REPLACE and EXTERN functions. In Druid SQL, the REPLACE function can create a new datasource or update an existing datasource.Load initial data
In the Druid web console, go to the Query view and run the following query:In the resulting
update_tutorial datasource, individual rows are uniquely identified by __time, animal, and number.Overwrite all data
You can use the REPLACE function with OVERWRITE ALL to replace the entire datasource with new data while dropping the old data.Run overwrite query
In the web console, open a new tab and run the following query to overwrite timestamp data for the entire
update_tutorial datasource:Overwrite records for a specific time range
You can use the REPLACE function to overwrite a specific time range of a datasource. When you overwrite a specific time range, that time range must align with the granularity specified in the PARTITIONED BY clause.Run time range overwrite query
In the web console, open a new tab and run the following query to insert a new row and update specific rows. Note that the OVERWRITE WHERE clause tells the query to only update records for the date 2024-01-03.
Update a row using partial segment overshadowing
In Druid, you can overlay older data with newer data for the entire segment or portions of the segment within a particular partition. This capability is called overshadowing. You can use partial overshadowing to update a single row by adding a smaller time granularity segment on top of the existing data. It’s a less common variation on a more common approach where you replace the entire time chunk.Run partial overshadowing query
The following example demonstrates how update data using partial overshadowing with mixed segment granularity. Note the following important points about the example:
- The query updates a single record for a specific
numberrow. - The original datasource uses DAY segment granularity.
- The new data segment is at HOUR granularity and represents a time range that’s smaller than the existing data.
- The OVERWRITE WHERE and WHERE TIME_IN_INTERVAL clauses specify the destination where the update occurs and the source of the update, respectively.
- The query replaces everything within the specified interval. To update only a subset of data in that interval, you have to carry forward all records, changing only what you want to change. You can accomplish that by using the CASE function in the SELECT list.
Learn more
See the following topics for more information:- Data updates for an overview of updating data in Druid.
- Load files with SQL-based ingestion for generating a query that references externally hosted data.
- Overwrite data with REPLACE for details on how the MSQ task engine executes SQL REPLACE queries.