Update data

Apache Druid stores data and indexes in segment files partitioned by time. After Druid creates a segment, its contents can’t be modified. You can either replace data for the whole segment, or, in some cases, overshadow a portion of the segment data.

In Druid, use time ranges to specify the data you want to update, as opposed to a primary key or dimensions often used in transactional databases. Data outside the specified replacement time range remains unaffected. You can use this Druid functionality to perform data updates, inserts, and deletes, similar to UPSERT functionality for transactional databases.

This tutorial shows you how to use the Druid SQL REPLACE function with the OVERWRITE clause to update existing data.

The tutorial walks you through the following use cases:

All examples use the multi-stage query (MSQ) task engine to executes SQL statements.

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.

In the Druid web console, go to the Query view and run the following query:

  1. REPLACE INTO "update_tutorial" OVERWRITE ALL
  2. WITH "ext" AS (
  3. SELECT *
  4. FROM TABLE(
  5. EXTERN(
  6. '{"type":"inline","data":"{\"timestamp\":\"2024-01-01T07:01:35Z\",\"animal\":\"octopus\", \"number\":115}\n{\"timestamp\":\"2024-01-01T05:01:35Z\",\"animal\":\"mongoose\", \"number\":737}\n{\"timestamp\":\"2024-01-01T06:01:35Z\",\"animal\":\"snake\", \"number\":1234}\n{\"timestamp\":\"2024-01-01T01:01:35Z\",\"animal\":\"lion\", \"number\":300}\n{\"timestamp\":\"2024-01-02T07:01:35Z\",\"animal\":\"seahorse\", \"number\":115}\n{\"timestamp\":\"2024-01-02T05:01:35Z\",\"animal\":\"skunk\", \"number\":737}\n{\"timestamp\":\"2024-01-02T06:01:35Z\",\"animal\":\"iguana\", \"number\":1234}\n{\"timestamp\":\"2024-01-02T01:01:35Z\",\"animal\":\"opossum\", \"number\":300}"}',
  7. '{"type":"json"}'
  8. )
  9. ) EXTEND ("timestamp" VARCHAR, "animal" VARCHAR, "number" BIGINT)
  10. )
  11. SELECT
  12. TIME_PARSE("timestamp") AS "__time",
  13. "animal",
  14. "number"
  15. FROM "ext"
  16. PARTITIONED BY DAY

In the resulting update_tutorial datasource, individual rows are uniquely identified by __time, animal, and number. To view the results, open a new tab and run the following query:

  1. SELECT * FROM "update_tutorial"

View the results

__timeanimalnumber
2024-01-01T01:01:35.000Zlion300
2024-01-01T05:01:35.000Zmongoose737
2024-01-01T06:01:35.000Zsnake1234
2024-01-01T07:01:35.000Zoctopus115
2024-01-02T01:01:35.000Zopossum300
2024-01-02T05:01:35.000Zskunk737
2024-01-02T06:01:35.000Ziguana1234
2024-01-02T07:01:35.000Zseahorse115

The results contain records for eight animals over two days.

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.

In the web console, open a new tab and run the following query to overwrite timestamp data for the entire update_tutorial datasource:

  1. REPLACE INTO "update_tutorial" OVERWRITE ALL
  2. WITH "ext" AS (SELECT *
  3. FROM TABLE(
  4. EXTERN(
  5. '{"type":"inline","data":"{\"timestamp\":\"2024-01-02T07:01:35Z\",\"animal\":\"octopus\", \"number\":115}\n{\"timestamp\":\"2024-01-02T05:01:35Z\",\"animal\":\"mongoose\", \"number\":737}\n{\"timestamp\":\"2024-01-02T06:01:35Z\",\"animal\":\"snake\", \"number\":1234}\n{\"timestamp\":\"2024-01-02T01:01:35Z\",\"animal\":\"lion\", \"number\":300}\n{\"timestamp\":\"2024-01-03T07:01:35Z\",\"animal\":\"seahorse\", \"number\":115}\n{\"timestamp\":\"2024-01-03T05:01:35Z\",\"animal\":\"skunk\", \"number\":737}\n{\"timestamp\":\"2024-01-03T06:01:35Z\",\"animal\":\"iguana\", \"number\":1234}\n{\"timestamp\":\"2024-01-03T01:01:35Z\",\"animal\":\"opossum\", \"number\":300}"}',
  6. '{"type":"json"}'
  7. )
  8. ) EXTEND ("timestamp" VARCHAR, "animal" VARCHAR, "number" BIGINT))
  9. SELECT
  10. TIME_PARSE("timestamp") AS "__time",
  11. "animal",
  12. "number"
  13. FROM "ext"
  14. PARTITIONED BY DAY

View the results

__timeanimalnumber
2024-01-02T01:01:35.000Zlion300
2024-01-02T05:01:35.000Zmongoose737
2024-01-02T06:01:35.000Zsnake1234
2024-01-02T07:01:35.000Zoctopus115
2024-01-03T01:01:35.000Zopossum300
2024-01-03T05:01:35.000Zskunk737
2024-01-03T06:01:35.000Ziguana1234
2024-01-03T07:01:35.000Zseahorse115

Note that the values in the __time column have changed to one day later.

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.

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.

  1. REPLACE INTO "update_tutorial"
  2. OVERWRITE WHERE "__time" >= TIMESTAMP'2024-01-03 00:00:00' AND "__time" < TIMESTAMP'2024-01-04 00:00:00'
  3. WITH "ext" AS (SELECT *
  4. FROM TABLE(
  5. EXTERN(
  6. '{"type":"inline","data":"{\"timestamp\":\"2024-01-03T01:01:35Z\",\"animal\":\"tiger\", \"number\":300}\n{\"timestamp\":\"2024-01-03T07:01:35Z\",\"animal\":\"seahorse\", \"number\":500}\n{\"timestamp\":\"2024-01-03T05:01:35Z\",\"animal\":\"polecat\", \"number\":626}\n{\"timestamp\":\"2024-01-03T06:01:35Z\",\"animal\":\"iguana\", \"number\":300}\n{\"timestamp\":\"2024-01-03T01:01:35Z\",\"animal\":\"flamingo\", \"number\":999}"}',
  7. '{"type":"json"}'
  8. )
  9. ) EXTEND ("timestamp" VARCHAR, "animal" VARCHAR, "number" BIGINT))
  10. SELECT
  11. TIME_PARSE("timestamp") AS "__time",
  12. "animal",
  13. "number"
  14. FROM "ext"
  15. PARTITIONED BY DAY

View the results

__timeanimalnumber
2024-01-02T01:01:35.000Zlion300
2024-01-02T05:01:35.000Zmongoose737
2024-01-02T06:01:35.000Zsnake1234
2024-01-02T07:01:35.000Zoctopus115
2024-01-03T01:01:35.000Zflamingo999
2024-01-03T01:01:35.000Ztiger300
2024-01-03T05:01:35.000Zpolecat626
2024-01-03T06:01:35.000Ziguana300
2024-01-03T07:01:35.000Zseahorse500

Note the changes in the resulting datasource:

  • There is now a new row called flamingo.
  • The opossum row has the value tiger.
  • The skunk row has the value polecat.
  • The iguana and seahorse rows have different numbers.

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.

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 number row.
  • 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.
  1. REPLACE INTO "update_tutorial"
  2. OVERWRITE
  3. WHERE "__time" >= TIMESTAMP'2024-01-03 05:00:00' AND "__time" < TIMESTAMP'2024-01-03 06:00:00'
  4. SELECT
  5. "__time",
  6. "animal",
  7. CAST(486 AS BIGINT) AS "number"
  8. FROM "update_tutorial"
  9. WHERE TIME_IN_INTERVAL("__time", '2024-01-03T05:01:35Z/PT1S')
  10. PARTITIONED BY FLOOR(__time TO HOUR)

View the results

__timeanimalnumber
2024-01-02T01:01:35.000Zlion300
2024-01-02T05:01:35.000Zmongoose737
2024-01-02T06:01:35.000Zsnake1234
2024-01-02T07:01:35.000Zoctopus115
2024-01-03T01:01:35.000Zflamingo999
2024-01-03T01:01:35.000Ztiger300
2024-01-03T05:01:35.000Zpolecat486
2024-01-03T06:01:35.000Ziguana300
2024-01-03T07:01:35.000Zseahorse500

Note that the number for polecat has changed from 626 to 486.

When you perform partial segment overshadowing multiple times, you can create segment fragmentation that could affect query performance. Use compaction to correct any fragmentation.

Learn more

See the following topics for more information: