Skip to main content

Append data

This tutorial shows you how to use the Apache Druid SQL INSERT function to append data to a datasource without changing the existing data. The examples in the tutorial 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 INSERT and EXTERN functions. The EXTERN function lets you read external data or write to an external location.

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

INSERT INTO "append_tutorial"
SELECT
TIME_PARSE("timestamp") AS "__time",
"animal",
"number"
FROM TABLE(
EXTERN(
'{"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}"}',
'{"type":"json"}'
)
) EXTEND ("timestamp" VARCHAR, "animal" VARCHAR, "number" BIGINT)
PARTITIONED BY DAY

The resulting append_tutorial datasource contains records for eight animals over two days. To view the results, open a new tab and run the following query:

SELECT * FROM "append_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

Append data

You can use the INSERT function to append data to the datasource without changing the existing data. In a new tab, run the following query to ingest and append data to the append_tutorial datasource:

INSERT INTO "append_tutorial"
SELECT
TIME_PARSE("timestamp") AS "__time",
"animal",
"number"
FROM TABLE(
EXTERN(
'{"type":"inline","data":"{\"timestamp\":\"2024-01-03T01:09:35Z\",\"animal\":\"zebra\", \"number\":233}\n{\"timestamp\":\"2024-01-04T07:01:35Z\",\"animal\":\"bear\", \"number\":577}\n{\"timestamp\":\"2024-01-04T05:01:35Z\",\"animal\":\"falcon\", \"number\":848}\n{\"timestamp\":\"2024-01-04T06:01:35Z\",\"animal\":\"giraffe\", \"number\":113}\n{\"timestamp\":\"2024-01-04T01:01:35Z\",\"animal\":\"rhino\", \"number\":473}"}',
'{"type":"json"}'
)
) EXTEND ("timestamp" VARCHAR, "animal" VARCHAR, "number" BIGINT)
PARTITIONED BY DAY

Druid adds rows for the subsequent days after seahorse. When the task completes, open a new tab and run the following query to view the results:

SELECT * FROM "append_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
2024-01-03T01:09:35.000Zzebra233
2024-01-04T01:01:35.000Zrhino473
2024-01-04T05:01:35.000Zfalcon848
2024-01-04T06:01:35.000Zgiraffe113
2024-01-04T07:01:35.000Zbear577

Learn more

See the following topics for more information: