# First steps

> Set up a Druid cluster using the Stackable Operator by installing ZooKeeper, HDFS, and Druid. Ingest and query example data via the web UI or API.

With the operators installed, deploy a Druid cluster and its dependencies. Afterward you can [verify that it works](https://docs.stackable.tech/home/stable/druid/getting_started/first_steps/index.md) by ingesting example data and subsequently query it.

## Setup

Three things need to be installed to have a Druid cluster:

-   A ZooKeeper instance for internal use by Druid

-   An HDFS instance to be used as a backend for deep storage

-   A PostgreSQL database to store the metadata of Druid

-   The Druid cluster itself


Create them in this order, each one is created by applying a manifest file. The operators you just installed then create the resources according to the manifests.

### ZooKeeper

Create a file named `zookeeper.yaml` with the following content:

```yaml
---
apiVersion: zookeeper.stackable.tech/v1alpha1
kind: ZookeeperCluster
metadata:
  name: simple-zk
spec:
  image:
    productVersion: 3.9.5
  servers:
    roleGroups:
      default:
        replicas: 1
---
apiVersion: zookeeper.stackable.tech/v1alpha1
kind: ZookeeperZnode
metadata:
  name: simple-druid-znode
spec:
  clusterRef:
    name: simple-zk
---
apiVersion: zookeeper.stackable.tech/v1alpha1
kind: ZookeeperZnode
metadata:
  name: simple-hdfs-znode
spec:
  clusterRef:
    name: simple-zk
```

Then create the resources by applying the manifest file:

```bash
kubectl apply -f zookeeper.yaml
```

### HDFS

Create `hdfs.yaml` with the following contents:

```yaml
---
apiVersion: hdfs.stackable.tech/v1alpha1
kind: HdfsCluster
metadata:
  name: simple-hdfs
spec:
  image:
    productVersion: 3.5.0
  clusterConfig:
    dfsReplication: 1
    zookeeperConfigMapName: simple-hdfs-znode
  nameNodes:
    config:
      listenerClass: external-stable # This exposes this role outside of Kubernetes. Remove this configuration if this is not desired
    roleGroups:
      default:
        replicas: 2
  dataNodes:
    config:
      listenerClass: external-unstable # This exposes this role outside of Kubernetes. Remove this configuration if this is not desired
    roleGroups:
      default:
        replicas: 1
  journalNodes:
    roleGroups:
      default:
        replicas: 1
```

And apply it:

```
kubectl apply -f hdfs.yaml
```

### PostgreSQL

Install a PostgreSQL database using `helm`. If you already have a PostgreSQL instance, you can skip this step and use your own below.

```bash
helm install postgresql-druid oci://registry-1.docker.io/bitnamicharts/postgresql \
  --version 16.5.0 \
  --set image.repository=bitnamilegacy/postgresql \
  --set volumePermissions.image.repository=bitnamilegacy/os-shell \
  --set metrics.image.repository=bitnamilegacy/postgres-exporter \
  --set global.security.allowInsecureImages=true \
  --set auth.database=druid \
  --set auth.username=druid \
  --set auth.password=druid \
  --wait
```

### Druid

Create a file named `druid.yaml` with the following contents:

```yaml
---
apiVersion: druid.stackable.tech/v1alpha1
kind: DruidCluster
metadata:
  name: simple-druid
spec:
  image:
    productVersion: 37.0.0
  clusterConfig:
    zookeeperConfigMapName: simple-druid-znode
    deepStorage:
      hdfs:
        configMapName: simple-hdfs
        directory: /druid
    metadataDatabase:
      postgresql:
        host: postgresql-druid
        database: druid
        credentialsSecretName: druid-db-credentials
  brokers:
    roleGroups:
      default:
        replicas: 1
    roleConfig:
      listenerClass: external-stable # This exposes this role outside of Kubernetes. Remove this configuration if this is not desired
  coordinators:
    roleGroups:
      default:
        replicas: 1
    roleConfig:
      listenerClass: external-stable # This exposes this role outside of Kubernetes. Remove this configuration if this is not desired
  historicals:
    roleGroups:
      default:
        replicas: 1
  middleManagers:
    roleGroups:
      default:
        replicas: 1
  routers:
    roleGroups:
      default:
        replicas: 1
    roleConfig:
      listenerClass: external-stable # This exposes this role outside of Kubernetes. Remove this configuration if this is not desired
---
apiVersion: v1
kind: Secret
metadata:
  name: druid-db-credentials
stringData:
  username: druid
  password: druid
```

And apply it:

```
kubectl apply --server-side -f druid.yaml
```

This creates the actual Druid Stacklet.

> **Warning:** This Druid instance uses Derby (`dbType: derby`) as a metadata store, which is an interal SQL database. It is not persisted and not suitable for production use! Consult the [Druid documentation](https://druid.apache.org/docs/latest/design/metadata-storage#available-metadata-stores) for a list of supported databases and setup instructions for production instances.

## Verify that it works

Submit an ingestion job and then query the ingested data — either through the web interface or the API.

First, make sure that all the Pods in the StatefulSets are ready:

```bash
kubectl get statefulset
```

The output should show all pods ready:

```
NAME                                 READY   AGE
simple-druid-broker-default          1/1     5m
simple-druid-coordinator-default     1/1     5m
simple-druid-historical-default      1/1     5m
simple-druid-middlemanager-default   1/1     5m
simple-druid-router-default          1/1     5m
simple-hdfs-datanode-default         1/1     6m
simple-hdfs-journalnode-default      1/1     6m
simple-hdfs-namenode-default         2/2     6m
simple-zk-server-default             3/3     7m
```

### Ingest example data

Next, ingest some example data using the web interface. You can either use `stackablectl stacklet list` to find out the address the Druid router is reachable at and use that address, or you can create a port-forward for the Druid Router:

```bash
kubectl port-forward svc/simple-druid-router 9088 > /dev/null 2>&1 &
```

If you prefer to use the command line instead, follow the instructions in the collapsed section below.

Alternative: Using the command line

If you prefer to not use the web interface and instead interact with the API, create a file `ingestion_spec.json` with the following contents:

```json
{
  "type": "index_parallel",
  "spec": {
    "ioConfig": {
      "type": "index_parallel",
      "inputSource": {
        "type": "http",
        "uris": [
          "https://druid.apache.org/data/wikipedia.json.gz"
        ]
      },
      "inputFormat": {
        "type": "json"
      }
    },
    "dataSchema": {
      "granularitySpec": {
        "segmentGranularity": "day",
        "queryGranularity": "none",
        "rollup": false
      },
      "dataSource": "wikipedia",
      "timestampSpec": {
        "column": "timestamp",
        "format": "iso"
      },
      "dimensionsSpec": {
        "dimensions": [
          "isRobot",
          "channel",
          "flags",
          "isUnpatrolled",
          "page",
          "diffUrl",
          {
            "type": "long",
            "name": "added"
          },
          "comment",
          {
            "type": "long",
            "name": "commentLength"
          },
          "isNew",
          "isMinor",
          {
            "type": "long",
            "name": "delta"
          },
          "isAnonymous",
          "user",
          {
            "type": "long",
            "name": "deltaBucket"
          },
          {
            "type": "long",
            "name": "deleted"
          },
          "namespace",
          "cityName",
          "countryName",
          "regionIsoCode",
          "metroCode",
          "countryIsoCode",
          "regionName"
        ]
      }
    },
    "tuningConfig": {
      "type": "index_parallel",
      "partitionsSpec": {
        "type": "dynamic"
      }
    }
  }
}
```

Submit the file with the following `curl` command:

```bash
  kubectl exec simple-druid-coordinator-default-0 -i -- \
    curl -s -k -X POST -H 'Content-Type:application/json' --data-binary @- \
    "https://${COORDINATOR}:8281/druid/indexer/v1/task" < ingestion_spec.json
```

Continue with the [next section](https://docs.stackable.tech/home/stable/druid/getting_started/first_steps/index.md).

To open the web interface navigate your browser to [https://localhost:9088/](https://localhost:9088/) to find the dashboard:

![dashboard](https://docs.stackable.tech/home/stable/druid/_images/getting_started/dashboard.png)

Now load the example data:

![load example](https://docs.stackable.tech/home/stable/druid/_images/getting_started/load_example.png)

Click through all pages of the load process. You can also follow the [Druid Quickstart Guide](https://druid.apache.org/docs/latest/tutorials/index.html#load-data).

Once you finished the ingestion dialog you should see the ingestion overview with the job, which eventually shows SUCCESS:

![load success](https://docs.stackable.tech/home/stable/druid/_images/getting_started/load_success.png)

### Query the data

Query from the user interface by navigating to the "Query" interface in the menu and query the `wikipedia` table:

Alternative: Using the command line

To query from the commandline, create a file called `query.json` with the query:

```json
{
  "query": "SELECT page, COUNT(*) AS Edits FROM wikipedia GROUP BY page ORDER BY Edits DESC LIMIT 10"
}
```

and execute it:

```bash
  kubectl exec simple-druid-broker-default-0 -i -- \
    curl -s -k -X POST -H 'Content-Type:application/json' --data-binary @- \
    "https://${BROKER}:8282/druid/v2/sql" < query.json
```

The result should be similar to:

![query](https://docs.stackable.tech/home/stable/druid/_images/getting_started/query.png)

Great! You’ve set up your first Druid cluster, ingested some data and queried it in the web interface.

## What’s next

Have a look at the [Usage guide](https://docs.stackable.tech/home/stable/druid/usage-guide/index.md) page to find out more about the features of the operator, such as S3-backed deep storage (as opposed to the HDFS backend used in this guide) or OPA-based authorization.
