# nifi-kafka-druid-water-level-data

> Install this demo for a showcase of using Kafka, Druid and Superset to visualize water levels in across Germany.

Install this demo on an existing Kubernetes cluster:

> **Note:** The namespace `nifi-kafka-druid-water-level-data` will be assumed in this guide. This is the default namespace used by `stackablectl` for this demo, so the `-n` argument can be omitted. It will be created if it doesn’t exist.

```console
$ stackablectl demo install nifi-kafka-druid-water-level-data -n nifi-kafka-druid-water-level-data
```

## System requirements

To run this demo, your system needs at least:

-   9 [cpu units](https://kubernetes.io/docs/tasks/debug/debug-cluster/resource-metrics-pipeline/#cpu) (core/hyperthread)

-   42GiB memory

-   75GiB disk storage


## Overview

This demo will

-   Install the required Stackable operators.

-   Spin up the following data products:

    -   **Superset**: A modern data exploration and visualization platform. This demo utilizes Superset to retrieve data from Druid via SQL queries and build dashboards on top of that data.

    -   **Kafka**: A distributed event streaming platform for high-performance data pipelines, streaming analytics and data integration. This demo uses it as an event streaming platform to stream the data in near real-time.

    -   **NiFi**: An easy-to-use, robust system to process and distribute data. This demo uses it to fetch water-level data from the internet and ingest it into Kafka.

    -   **Druid**: A real-time database to power modern analytics applications. This demo uses it to ingest the near real-time data from Kafka, store it and enable access to the data via SQL.

    -   **Garage**: S3 compatible object store. This demo uses it as persistent storage for Druid to store all the data.


-   Ingest water level data from the [PEGELONLINE web service](https://www.pegelonline.wsv.de/webservice/ueberblick) into Kafka. The data contains measured water levels of different measuring stations all around Germany. If the web service is unavailable, this demo will not work, as it needs the web service to ingest the data.

    -   First, historical data from the last 30 days will be fetched and ingested.

    -   Afterwards, the demo will fetch the current measurement of every station approximately every two minutes and ingest it near-real-time into Kafka.


-   Start a Druid ingestion job that ingests the data into the Druid instance.

-   Create Superset dashboards for visualization of the data.


The whole data pipeline will have a very low latency, from putting a record into Kafka to showing up in the dashboard charts. You can see the deployed products and their relationship in the following diagram:

![overview.drawio](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/overview.drawio.png)

## List the deployed Stackable services

To list the installed Stackable services run the following command:

```console
$ stackablectl stacklet list -n nifi-kafka-druid-water-level-data

┌───────────┬───────────┬───────────────────────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────────────┐
│ PRODUCT   │ NAME      │ NAMESPACE                         │ ENDPOINTS                                                                                                             │ CONDITIONS                      │
╞═══════════╪═══════════╪═══════════════════════════════════╪═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╪═════════════════════════════════╡
│ druid     ┆ druid     ┆ nifi-kafka-druid-water-level-data ┆ broker-https                                https://172.19.0.3:32165                                                  ┆ Available, Reconciling, Running │
│           ┆           ┆                                   ┆ coordinator-https                           https://172.19.0.6:31287                                                  ┆                                 │
│           ┆           ┆                                   ┆ router-https                                https://172.19.0.3:31965                                                  ┆                                 │
├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ kafka     ┆ kafka     ┆ nifi-kafka-druid-water-level-data ┆ broker-default-0-listener-broker-kafka-tls  172.19.0.3:31041                                                          ┆ Available, Reconciling, Running │
│           ┆           ┆                                   ┆ broker-default-0-listener-broker-metrics    172.19.0.3:31503                                                          ┆                                 │
│           ┆           ┆                                   ┆ broker-default-bootstrap-kafka-tls          172.19.0.3:30793                                                          ┆                                 │
├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ nifi      ┆ nifi      ┆ nifi-kafka-druid-water-level-data ┆ node-https                                  https://172.19.0.6:32038                                                  ┆ Available, Reconciling, Running │
├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ superset  ┆ superset  ┆ nifi-kafka-druid-water-level-data ┆ node-http                                   http://172.19.0.3:30435                                                   ┆ Available, Reconciling, Running │
├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ zookeeper ┆ zookeeper ┆ nifi-kafka-druid-water-level-data ┆ server-zk                                   zookeeper-server.nifi-kafka-druid-water-level-data.svc.cluster.local:2282 ┆ Available, Reconciling, Running │
└───────────┴───────────┴───────────────────────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────────────┘
```

> **Note:** When a product instance has not finished starting yet, the service will have no endpoint. Depending on your internet connectivity, creating all the product instances might take considerable time. A warning might be shown if the product is not ready yet.

## Inspect the data in Kafka

Kafka is an event streaming platform to stream the data in near real-time. All the messages put in and read from Kafka are structured in dedicated queues called topics. The test data will be put into topics called stations and measurements. The records are produced (put in) by the test data generator and consumed (read) by Druid afterwards in the same order they were created.

To interact with Kafka you will use the client scripts shipped with the Kafka image. Kafka uses mutual TLS, so clients wanting to connect to Kafka must present a valid TLS certificate. The easiest way to obtain this is to shell into the `kafka-broker-default-0` Pod, as we will do in the following section for demonstration purposes. For a production setup, you should spin up a dedicated Pod provisioned with a certificate acting as a Kafka client instead of shell-ing into the Kafka Pod.

### List the available Topics

You can execute a command on the Kafka broker to list the available topics as follows:

```console
$ kubectl exec -n nifi-kafka-druid-water-level-data kafka-broker-default-0 -c kafka -- \
env KAFKA_LOG4J_OPTS=-Dlog4j2.configurationFile=/stackable/kafka/config/tools-log4j2.yaml \
/stackable/kafka/bin/kafka-topics.sh \
--describe \
--bootstrap-server kafka-broker-default-headless.nifi-kafka-druid-water-level-data.svc.cluster.local:9093 \
--command-config /stackable/config/client.properties
Topic: measurements     TopicId: w9qYb3GaTvCMZj4G8pkPPQ PartitionCount: 8       ReplicationFactor: 1    Configs: min.insync.replicas=1,segment.bytes=100000000,retention.bytes=900000000
        Topic: measurements     Partition: 0    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: measurements     Partition: 1    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: measurements     Partition: 2    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: measurements     Partition: 3    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: measurements     Partition: 4    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: measurements     Partition: 5    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: measurements     Partition: 6    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: measurements     Partition: 7    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
Topic: stations TopicId: QkKmvOagQkG4QbeS0IZ_Tg PartitionCount: 8       ReplicationFactor: 1    Configs: min.insync.replicas=1,segment.bytes=100000000,retention.bytes=900000000
        Topic: stations Partition: 0    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: stations Partition: 1    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: stations Partition: 2    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: stations Partition: 3    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: stations Partition: 4    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: stations Partition: 5    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: stations Partition: 6    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
        Topic: stations Partition: 7    Leader: 1243966388      Replicas: 1243966388    Isr: 1243966388 Elr:    LastKnownElr:
```

You can see that Kafka consists of one broker, and the topics `stations` and `measurements` have been created with eight partitions each.

### Show Sample Records

To see some records sent to Kafka, run the following commands. You can change the number of records to print via the `--max-messages` parameter.

```console
$ kubectl exec -n nifi-kafka-druid-water-level-data kafka-broker-default-0 -c kafka -- \
env KAFKA_LOG4J_OPTS=-Dlog4j2.configurationFile=/stackable/kafka/config/tools-log4j2.yaml \
/stackable/kafka/bin/kafka-console-consumer.sh \
--bootstrap-server kafka-broker-default-headless.nifi-kafka-druid-water-level-data.svc.cluster.local:9093 \
--command-config /stackable/config/client.properties \
--topic stations \
--offset earliest \
--partition 0 \
--max-messages 2
```

Below is an example of the output of two records:

```json
{
  "uuid": "47174d8f-1b8e-4599-8a59-b580dd55bc87",
  "number": 48900237,
  "shortname": "EITZE",
  "longname": "EITZE",
  "km": 9.56,
  "agency": "VERDEN",
  "longitude": 9.2767694354,
  "latitude": 52.9040654474,
  "water": {
    "shortname": "ALLER",
    "longname": "ALLER"
  }
}
{
  "uuid": "5aaed954-de4e-4528-8f65-f3f530bc8325",
  "number": 48900204,
  "shortname": "RETHEM",
  "longname": "RETHEM",
  "km": 34.22,
  "agency": "VERDEN",
  "longitude": 9.3828408101,
  "latitude": 52.7890975921,
  "water": {
    "shortname": "ALLER",
    "longname": "ALLER"
  }
}
```

```console
$ kubectl exec -n nifi-kafka-druid-water-level-data kafka-broker-default-0 -c kafka -- \
env KAFKA_LOG4J_OPTS=-Dlog4j2.configurationFile=/stackable/kafka/config/tools-log4j2.yaml \
/stackable/kafka/bin/kafka-console-consumer.sh \
--bootstrap-server kafka-broker-default-headless.nifi-kafka-druid-water-level-data.svc.cluster.local:9093 \
--command-config /stackable/config/client.properties \
--topic measurements \
--offset earliest \
--partition 0 \
--max-messages 3
```

Below is an example of the output of three records:

```json
{
  "timestamp": 1658151900000,
  "value": 221,
  "station_uuid": "47174d8f-1b8e-4599-8a59-b580dd55bc87"
}
{
  "timestamp": 1658152800000,
  "value": 220,
  "station_uuid": "47174d8f-1b8e-4599-8a59-b580dd55bc87"
}
{
  "timestamp": 1658153700000,
  "value": 220,
  "station_uuid": "47174d8f-1b8e-4599-8a59-b580dd55bc87"
}
```

The records of the two topics only contain the needed data. The measurement records contain a `station_uuid` for the measuring station. The relationship is illustrated below.

![topics](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/topics.png)

The reason for splitting the data up into two different topics is the improved performance. One more straightforward solution would be to use a single topic and produce records like the following:

```json
{
  "uuid": "47174d8f-1b8e-4599-8a59-b580dd55bc87",
  "number": 48900237,
  "shortname": "EITZE",
  "longname": "EITZE",
  "km": 9.56,
  "agency": "VERDEN",
  "longitude": 9.2767694354,
  "latitude": 52.9040654474,
  "water": {
    "shortname": "ALLER",
    "longname": "ALLER"
  },
  "timestamp": 1658151900000,
  "value": 221
}
```

Notice the two last attributes that differ from the previously shown `stations` records. The obvious downside is that every measurement (multiple millions of it) has to contain all the data known about the station it was measured at. This often leads to transmitting and storing duplicated information, e.g., the longitude of a station, resulting in increased network traffic and storage usage. The solution is only to send a station’s known/needed data or measurement data. This process is called data normalization. The downside is that when analyzing the data, you need to combine the records from multiple tables in Druid (`stations` and `measurements`).

If you are interested in how many records have been produced to the Kafka topic so far, use the following command. It prints the current offset for each partition, which tells you how many records have been produced to it.

```console
$ kubectl exec -n nifi-kafka-druid-water-level-data kafka-broker-default-0 -c kafka -- \
env KAFKA_LOG4J_OPTS=-Dlog4j2.configurationFile=/stackable/kafka/config/tools-log4j2.yaml \
/stackable/kafka/bin/kafka-get-offsets.sh \
--bootstrap-server kafka-broker-default-headless.nifi-kafka-druid-water-level-data.svc.cluster.local:9093 \
--command-config /stackable/config/client.properties \
--topic measurements
measurements:0:1366665
measurements:1:1364930
measurements:2:1395607
measurements:3:1390762
measurements:4:1368829
measurements:5:1362539
measurements:6:1344362
measurements:7:1369651
```

Summing all partition offsets gives the total number of records produced. In this example, that is roughly 10.9 million measurement records.

To inspect the last produced records, use the following command. Here, we consume the last three records from partition `0` of the `measurements` topic.

```console
$ kubectl exec -n nifi-kafka-druid-water-level-data kafka-broker-default-0 -c kafka -- \
env KAFKA_LOG4J_OPTS=-Dlog4j2.configurationFile=/stackable/kafka/config/tools-log4j2.yaml \
/stackable/kafka/bin/kafka-console-consumer.sh \
--bootstrap-server kafka-broker-default-headless.nifi-kafka-druid-water-level-data.svc.cluster.local:9093 \
--command-config /stackable/config/client.properties \
--topic measurements \
--offset latest \
--partition 0 \
--max-messages 3
{"timestamp":"2025-10-21T11:00:00+02:00","value":369.54,"station_uuid":"5cdc6555-87d7-4fcd-834d-cbbe24c9d08b"}
{"timestamp":"2025-10-21T11:15:00+02:00","value":369.54,"station_uuid":"5cdc6555-87d7-4fcd-834d-cbbe24c9d08b"}
{"timestamp":"2025-10-21T11:00:00+02:00","value":8.0,"station_uuid":"7deedc21-2878-40cc-ab47-f6da0d9002f1"}
```

## NiFi

NiFi fetches water-level data from the internet and ingests it into Kafka in real time. This demo includes a workflow ("process group") that fetches the last 30 days of historical measurements and produces the records in Kafka. It also keeps streaming near-real-time updates for every available measuring station.

### View the testdata-generation Job

You can look at the ingestion job running in NiFi by opening the endpoint `https` from your `stackablectl stacklet list` command output. You have to use the endpoint from your command output. In this case, it is [https://172.19.0.6:32038](https://172.19.0.6:32038). Open it with your favourite browser. Suppose you get a warning regarding the self-signed certificate generated by the [Secret Operator](https://docs.stackable.tech/home/stable/secret-operator/index.md) (e.g. Warning: Potential Security Risk Ahead). In that case, you must tell your browser to trust the website and continue.

![nifi 1](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_1.png)

Log in with the username `admin` and password `adminadmin`.

![nifi 13](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_13.png)

The NiFi workflow contains the `IngestWaterLevelsToKafka` Process Group. Enter the Process Group by double-clicking on it or right-clicking and selecting `Enter Group`.

![nifi 2](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_2.png)

As you can see, the Process Group consists of lots of components. It is split into three main components:

1.  On the left is the first part bulk-loading all the known stations

2.  In the middle is the second part bulk-loading the historical data for the last 30 days

3.  On the right is the third part iterating over all stations and emitting the current measurement in an endless loop


You can zoom in by using your mouse and mouse wheel.

![nifi 3](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_3.png)

![nifi 4](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_4.png)

The left workflows works as follows:

1.  The `Get station list` processors fetch the current list of stations as JSON via HTTP from the [PEGELONLINE web service](https://www.pegelonline.wsv.de/webservice/ueberblick).

2.  `Produce stations records` takes the list and produces a Kafka record for every station into the topic `stations`.

3.  `SplitRecords` simultaneously takes the single FlowFile (NiFi Record) containing all the stations and creates a new FlowFile for every station.

4.  `Extract station_uuid` takes every FlowFile representing a station and extract the attribute `station_uuid` into the metadata of the FlowFile.

5.  `Get historic measurements` calls the [PEGELONLINE web service](https://www.pegelonline.wsv.de/webservice/ueberblick) for every station and fetches the measurements of the last 30 days.

6.  `Add station_uuid` will add the attribute `station_uuid` to the JSON list of measurements returned from the [PEGELONLINE web service](https://www.pegelonline.wsv.de/webservice/ueberblick), which is missing.

7.  `PublishKafka` finally emits every measurement as a Kafka record to the topic `measurements`.


The right side works similarly but is executed in an endless loop to stream the data in near-realtime. Double-click on the `Get station list` processor to show the processor details.

![nifi 5](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_5.png)

Head over to the `Properties` tab.

![nifi 6](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_6.png)

Here, you can see the setting `HTTP URL`, which specifies the download URL from where the JSON file containing the stations is retrieved. Close the processor details popup by clicking `Close`. You can also have a detailed view of the `Produce station records` processor by double-clicking it.

![nifi 7](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_7.png)

The Kafka connection details within this processor - like broker addresses and topic names - are specified. It uses the `JsonTreeReader` to parse the downloaded JSON and the `JsonRecordSetWriter` to split it into individual JSON records before writing it out. Double-click the `Get 30 days historic data` processor.

![nifi 8](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_8.png)

This processor fetched the historical data for every station. Click on the `HTTP URL` property.

![nifi 9](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_9.png)

The `HTTP URL` does contain the `${station_uuid}` placeholder, which gets replaced for every station.

Double-click the `PublishKafka` processor.

![nifi 10](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_10.png)

You can also see the number of produced records by right-clicking on `PublishKafka` and selecting `View status history`.

![nifi 11](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_11.png)

After selecting `Messages Sent (5 mins)` in the top right corner, you can see that ~670k records were produced in ~5 minutes, corresponding to ~22k measurements/s. Keep in mind that the demo uses a single-node NiFi setup. The performance can be increased by using multiple nodes.

Regarding the NiFi resources, use the hamburger menu icon on the top right corner and select `Node Status History`.

![nifi 12](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/nifi_12.png)

The diagram shows the used heap size of the NiFi node. You can also select other metrics to show in the top right corner.

## Druid

Druid is used to ingest the near real-time data from Kafka, store it and enable SQL access. The demo has started two ingestion jobs - one reading from the topic `stations` and the other from `measurements` - and saving it into Druid’s deep storage. The Druid deep storage is based on the S3 store provided by Garage.

### View the Ingestion job

You can have a look at the ingestion jobs running in Druid by opening the Druid endpoint `router-https` from your `stackablectl stacklet list` command output ([https://172.19.0.3:31965](https://172.19.0.3:31965) in this case).

![druid 1](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/druid_1.png)

By clicking on `Supervisors` at the top you can see the running ingestion jobs.

![druid 2](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/druid_2.png)

After clicking on the magnification glass to the right side of the `RUNNING` supervisor, you can see additional information (here, the supervisor `measurements` was chosen). On the tab `Task stats` on the left, you can see the number of processed records as well as the number of errors.

![druid 3](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/druid_3.png)

The statistics show how many records that Druid ingested during the last minute and the total already ingested. All records have been ingested successfully, indicated by having no `processWithError`, `thrownAway` or `unparseable` records in the output of the `View raw` button at the top right.

### Query the Data Source

The started ingestion jobs have automatically created the Druid data sources `stations` and `measurements`. You can see the available data sources by clicking on `Datasources` at the top.

![druid 4](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/druid_4.png)

The `Avg. row size (bytes)` column shows that a typical `measurement` record has `4` bytes, while a station record has `222`, more than 50 times the size. So, by choosing two dedicated topics over a single topic, this demo saved 50x of storage and computation costs.

Click `Segments` in the menu bar at the top to see the segments of the data source. In this case, the `measurements` data source is partitioned by the measurement day, resulting in 32 segments.

![druid 5](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/druid_5.png)

Druid offers a web-based way of querying the data sources via SQL. To achieve this you first have to click on `Query` at the top.

![druid 6](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/druid_6.png)

You can now enter any arbitrary SQL statement, to e.g. list 10 stations run

```sql
select * from stations limit 10
```

![druid 7](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/druid_7.png)

To count the measurements per day run

```sql
select
  time_format(__time, 'YYYY/MM/dd') as "day",
  count(*) as measurements
from measurements
group by 1
order by 1 desc
```

![druid 8](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/druid_8.png)

## Superset

Superset provides the ability to execute SQL queries and build dashboards. Open the Superset endpoint `node-http` in your browser ([http://172.19.0.3:30435](http://172.19.0.3:30435) in this case).

![superset 1](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_1.png)

Log in with the username `admin` and password `adminadmin`.

![superset 2](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_2.png)

### View the dashboard

The demo has created a Dashboard to visualize the water level data. To open it, click on the tab `Dashboards` at the top.

![superset 3](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_3.png)

Click on the dashboard called `Water level data`. It might take some time until the dashboard renders all the included charts.

![superset 4](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_4.png)

### View the charts

The dashboard `Water level data` consists of multiple charts. To list the charts, click on the tab `Charts` at the top.

![superset 5](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_5.png)

Click on the Chart `Measurements / hour`. On the left side, you can modify the chart and click on `Update Chart` to see the effect.

![superset 6](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_6.png)

### View the Station Distribution on the World Map

To look at the stations' geographical distribution, you have to click on the tab `Charts` at the top again. Afterwards, click on the chart `Stations distribution`.

![superset 7](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_7.png)

The stations are, of course, placed alongside waterways. They are coloured by the waters they measure, so all stations alongside a body of water have the same colour. You can move and zoom the map with your mouse to interactively explore the map. You can, e.g. have a detailed look at the water [Rhein](https://en.wikipedia.org/wiki/Rhine).

![superset 8](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_8.png)

### Execute arbitrary SQL statements

Within Superset, you can create dashboards and run arbitrary SQL statements. On the top, click on the tab `SQL` → `SQL Lab`.

![superset 9](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_9.png)

On the left select the database `druid`, the schema `druid` and select `stations` or `measurements` from the `druid` folder. Click on `Add a new tab`.

![superset 10](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_10.png)

In the right textbox, enter the desired SQL statement. We need to join the two tables to get exciting results. Run the following query to determine the number of measurements the stations made:

```sql
select
  stations.longname as station,
  count(*) as measurements
from measurements inner join stations on stations.uuid = measurements.station_uuid
group by 1
order by 2 desc
```

![superset 11](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_11.png)

You can also find the number of measurements for every body of water:

```sql
select
  stations.water_longname as water,
  count(*) as measurements
from measurements inner join stations on stations.uuid = measurements.station_uuid
group by 1
order by 2 desc
```

![superset 12](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_12.png)

What might also be interesting is the average and current measurement of the stations:

```sql
select
  stations.longname as station,
  avg("value") as avg_measurement,
  latest_by("value", measurements."__time") as current_measurement,
  latest_by("value", measurements."__time") - avg("value") as diff
from measurements inner join stations on stations.uuid = measurements.station_uuid
group by 1
order by 2 desc
```

![superset 13](https://docs.stackable.tech/home/stable/demos/_images/nifi-kafka-druid-water-level-data/superset_13.png)

## S3

The S3 provided by Garage is used as persistent deep storage for Druid to store all the data used.

Garage has no web interface, so the buckets are inspected with an S3 client.

Read the endpoint and credentials out of the cluster:

```console
$ KUBECTL_NAMESPACE="default" # or whichever you set when running stackablectl demo install
$ NODE_IP="$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="InternalIP")].address}' | cut -d' ' -f1)" # a dual-stack node reports several
$ case "$NODE_IP" in *:*) NODE_IP="[$NODE_IP]";; esac # an IPv6 address needs brackets in a URL
$ NODE_PORT="$(kubectl -n "$KUBECTL_NAMESPACE" get service garage -o jsonpath='{.spec.ports[0].nodePort}')"
$ export S3_ENDPOINT="https://$NODE_IP:$NODE_PORT"
$ export S3_ACCESS_KEY="$(kubectl -n "$KUBECTL_NAMESPACE" get secret s3-credentials -o jsonpath='{.data.accessKey}' | base64 -d)"
$ export S3_SECRET_KEY="$(kubectl -n "$KUBECTL_NAMESPACE" get secret s3-credentials -o jsonpath='{.data.secretKey}' | base64 -d)"
$ export S3_REGION=region-1
```

The region is not optional. Garage rejects any request signed for a different region than the one it is configured with, and most clients fall back to an AWS region when none is set.

The certificate is issued by the Stackable secret-operator, whose CA your machine does not trust, so the commands below skip certificate verification.

**Tab: AWS CLI**

```console
$ export AWS_ENDPOINT_URL="$S3_ENDPOINT"
$ export AWS_ACCESS_KEY_ID="$S3_ACCESS_KEY"
$ export AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY"
$ export AWS_DEFAULT_REGION="$S3_REGION"
$ aws configure set default.s3.addressing_style path
```

The commands below are written for `rclone`. The `aws` equivalents are `aws s3 ls` to list buckets and `aws s3 ls s3://<bucket>/ --recursive --human-readable` to list objects, both with `--no-verify-ssl`.

**Tab: rclone**

`rclone` needs no installation, it runs straight from a container image:

```console
$ rclone() {
    docker run --rm -i --network host \
      -e RCLONE_S3_PROVIDER=Other \
      -e RCLONE_S3_ENDPOINT="$S3_ENDPOINT" \
      -e RCLONE_S3_REGION="$S3_REGION" \
      -e RCLONE_S3_ACCESS_KEY_ID="$S3_ACCESS_KEY" \
      -e RCLONE_S3_SECRET_ACCESS_KEY="$S3_SECRET_KEY" \
      -e RCLONE_S3_FORCE_PATH_STYLE=true \
      -e RCLONE_NO_CHECK_CERTIFICATE=true \
      rclone/rclone:1.75.1 "$@"
  }
```

`--network host` is there for anyone trying the demos out on a local cluster, where the endpoint is only reachable from the host itself.

In the commands below, `lsd` lists buckets, `lsf` lists names only, and `lsl` adds sizes and modification times.

Druid writes its segments into the `demo` bucket, under `data`, with one directory per data source:

```console
$ rclone lsf --dirs-only :s3:demo/data
```

Look at `measurements`:

```console
$ rclone lsf --dirs-only :s3:demo/data/measurements
```

Druid saved roughly 51.5 MiB of data across 33 prefixes. One prefix corresponds to one segment, which contains all the measurements of a day. If nothing is listed, Druid still needs to save its data from memory to the deep storage. After waiting for roughly an hour, the data should show up.

If you don’t want to wait, you can make Druid publish the data to the deep storage right away:

1.  Open the Druid web interface (the endpoint `router-https` from your `stackablectl stacklet list` command output).

2.  Click on `Supervisors` at the top.

3.  Open the actions menu of the `measurements` supervisor and click on `Suspend`. The running ingestion task stops reading from Kafka, publishes its segments to S3 and finishes.

4.  Run the `rclone lsf` command for `measurements` from above again. The prefixes should show up after a short while.

5.  Open the actions menu of the supervisor again and click on `Resume` to continue the ingestion.


Opening up a prefix for a specific day shows the file containing that day’s data:

```console
$ rclone lsl :s3:demo/data/measurements
```

`size` totals the segments up:

```console
$ rclone size :s3:demo/data/measurements
```

## Summary

The demo puts station records into the Kafka stream pipeline topic `station`. Over time it will stream ~30,000 measurements/s for a total of ~11 million measurements into the topic `measurements`. Druid ingested the data near real-time into its data source and enabled SQL access to it. Superset was used as a web-based frontend to execute SQL statements and build dashboards.

## Where to go from here

There are multiple paths to go from here. The following sections give you some ideas on what to explore next. You can find the description of the water level data on the [PEGELONLINE REST API documentation (German only)](https://www.pegelonline.wsv.de/webservice/dokuRestapi).

### Execute arbitrary SQL statements

Within Superset (or the Druid web interface), you can execute arbitrary SQL statements to explore the water level data.

### Create additional dashboards

You also can create additional charts and bundle them together in a Dashboard. Have a look at [the Superset documentation](https://superset.apache.org/docs/using-superset/creating-your-first-dashboard/#creating-charts-in-explore-view) on how to do that.

### Load additional data

You can use the NiFi web interface to collect arbitrary data and write it to Kafka (it’s recommended to use new Kafka topics for that). Alternatively, you can use a Kafka client like [kcat](https://github.com/edenhill/kcat) to create new topics and ingest data. Using the Druid web interface, you can start an ingestion job that consumes and stores the data in an internal data source. There is an excellent [tutorial](https://druid.apache.org/docs/latest/tutorials/tutorial-kafka.html#loading-data-with-the-data-loader) from Druid on how to do this. Afterwards, the data source can be analyzed within Druid and Superset, like the water level data.
