Back to Druid

Append data

docs/tutorials/tutorial-append-data.md

latest5.5 KB
Original Source
<!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more contributor license agreements. See the NOTICE file ~ distributed with this work for additional information ~ regarding copyright ownership. The ASF licenses this file ~ to you under the Apache License, Version 2.0 (the ~ "License"); you may not use this file except in compliance ~ with the License. You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, ~ software distributed under the License is distributed on an ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~ KIND, either express or implied. See the License for the ~ specific language governing permissions and limitations ~ under the License. -->

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:

sql
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:

sql
SELECT * FROM "append_tutorial"
<details> <summary> View the results</summary>
__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
</details>

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:

sql
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:

sql
SELECT * FROM "append_tutorial"
<details> <summary> View the results</summary>
__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
</details>

Learn more

See the following topics for more information: