# trino-taxi-data

> Install and demo Trino with NYC taxi data: Query with SQL, visualize with Superset, and explore data in S3 and Trino on Kubernetes.

Install this demo on an existing Kubernetes cluster:

```console
$ stackablectl demo install trino-taxi-data
```

> **Warning:** This demo should not be run alongside other demos.

## System requirements

To run this demo, your system needs at least:

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

-   18GiB memory

-   28GiB 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 Trino via SQL queries and build dashboards on top of that data.

    -   **Trino**: A fast distributed SQL query engine for big data analytics that helps you explore your data universe. This demo uses it to enable SQL access to the data.

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

    -   **Hive metastore**: A service that stores metadata related to Apache Hive and other services. This demo uses it as metadata storage for Trino.

    -   **Open policy agent** (OPA): An open-source, general-purpose policy engine unifying policy enforcement across the stack. This demo uses it as the authorizer for Trino, which decides which user can query which data.


-   Load test data into S3. It contains 2.5 years of [New York City taxi trips](https://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page).

-   Make data accessible via SQL in Trino.

-   Create Superset dashboards for visualization of the data.


You can see the deployed products and their relationship in the following diagram:

![overview.drawio](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/overview.drawio.png)

## List the deployed Stackable services

To list the installed Stackable services, run the following command:

```console
$ stackablectl stacklet list

┌──────────┬───────────────┬───────────┬──────────────────────────────────────────────────────────────────┬─────────────────────────────────┐
│ PRODUCT  ┆ NAME          ┆ NAMESPACE ┆ ENDPOINTS                                                        ┆ CONDITIONS                      │
╞══════════╪═══════════════╪═══════════╪══════════════════════════════════════════════════════════════════╪═════════════════════════════════╡
│ hive     ┆ hive          ┆ default   ┆ metastore-hive     hive-metastore.default.svc.cluster.local:9083 ┆ Available, Reconciling, Running │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ opa      ┆ opa           ┆ default   ┆                                                                  ┆ Available, Reconciling, Running │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ superset ┆ superset      ┆ default   ┆ node-http          http://172.18.0.2:31312                       ┆ Available, Reconciling, Running │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ trino    ┆ trino         ┆ default   ┆ coordinator-https  https://172.18.0.2:30755                      ┆ 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 S3

The S3 provided by Garage is used as a persistent storage 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.

The test data is in the `demo` bucket, under `ny-taxi-data/raw`:

```console
$ rclone lsl :s3:demo/ny-taxi-data/raw
```

The demo uploaded 1GB of parquet files, one file per month. The data contains taxi rides in New York City. The file size (and therefore the number of rides) decreased drastically because of the COVID-19 pandemic starting from `2020-03`. [Parquet](https://parquet.apache.org/) is an open-source, column-oriented data file format for efficient storage and retrieval.

## Use the Trino web interface

Trino offers SQL access to the data within S3. Open the endpoint `coordinator-https` in your browser (`[https://172.18.0.2:30755](https://172.18.0.2:30755)` in this case). If you get a warning regarding the self-signed certificate (e.g. `Warning: Potential Security Risk Ahead`), you must tell your browser to trust the website and continue.

![trino 1](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/trino_1.png)

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

![trino 2](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/trino_2.png)

When you start executing SQL queries, you will see the queries getting processed here.

## Use the Superset web interface

Superset gives the ability to execute SQL queries and build dashboards. Open the endpoint `external-http` in your browser (`[http://172.18.0.2:31312](http://172.18.0.2:31312)` in this case).

![superset 1](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/superset_1.png)

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

![superset 2](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/superset_2.png)

### View the dashboard

On the top, click on the `Dashboards` tab.

![superset 3](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/superset_3.png)

Click on the dashboard called `Taxi data`. It might take some time until the dashboards render all included charts.

![superset 4](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/superset_4.png)

You can clearly see the impact of COVID-19 on the taxi business.

### 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 5](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/superset_5.png)

On the left, select the database `Trino`, the schema `demo` and set `See table schema` to `ny_taxi_data`.

![superset 6](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/superset_6.png)

In the right textbox, enter the desired SQL statement. If you do not want to make on up, you can use the following:

```sql
select
  format_datetime(tpep_pickup_datetime, 'YYYY/MM') as month,
  count(*) as trips,
  sum(total_amount) as sales,
  avg(duration_min) as avg_duration_min
from ny_taxi_data
group by 1
order by 1
```

![superset 7](https://docs.stackable.tech/home/stable/demos/_images/trino-taxi-data/superset_7.png)

## Summary

The demo loaded 2.5 years of taxi trip data from New York City with 68 million records and a total size of 1GB in parquet files. The data was put into the S3 storage. Trino enables you to query the data via SQL. 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 can give you some ideas on what to explore next. You can find the description of the taxi data [on the New York City website](https://www1.nyc.gov/assets/tlc/downloads/pdf/data_dictionary_trip_records_yellow.pdf).

### Execute arbitrary SQL statements

Within Superset you can execute arbitrary SQL statements to explore the taxi data. Can you answer the following questions by executing SQL statements? The [Trino documentation on their SQL language](https://trino.io/docs/current/language.html) might help you.

How many taxi trips took place in the year 2021?

See the answer

```sql
select
  count(*) as trips
from ny_taxi_data
where year(tpep_pickup_datetime) = 2021
```

returns 30.903.982 trips.

What was the maximum amount of passengers?

See the answer

```sql
select
  max(passenger_count) as max_passenger_count
from ny_taxi_data;
```

Returns `112` passengers. Well that’s weird. Let’s examine the passengers distribution.

```sql
select
  passenger_count,
  count(*) as frequency
from ny_taxi_data
group by 1
order by 1 desc
limit 100
```

returns

```
 passenger_count | frequency
-----------------+-----------
           112.0 |         1
            96.0 |         1
             9.0 |        98
             8.0 |       156
             7.0 |       229
             6.0 |   1089568
             5.0 |   1715439
             4.0 |   1052834
             3.0 |   2504112
             2.0 |   9575299
             1.0 |  48133494
             0.0 |   1454268
            NULL |   2698591
```

We can see that one trip had 112 and another one 96 passengers. All the other trips start with a more "realistic" number of 9 passengers. As a bonus question: What **exactly** did the large number of passenger do?

```sql
select *
from ny_taxi_data
where passenger_count > 50
```

returns

```
 vendorid |  tpep_pickup_datetime   |  tpep_dropoff_datetime  | duration_min | passenger_count | trip_distance | payment_type | fare_amount | tip_amount | total_amount
----------+-------------------------+-------------------------+--------------+-----------------+---------------+--------------+-------------+------------+--------------
        2 | 2021-08-01 19:47:43.000 | 2021-08-01 19:57:54.000 |           10 |           112.0 |           1.8 | Credit card  |         9.0 |       2.46 |        14.76
        2 | 2021-08-03 11:51:58.000 | 2021-08-03 12:09:29.000 |           17 |            96.0 |          1.56 | Credit card  |        11.5 |       2.22 |        17.02
```

Pretty cheap for that amount of people! These are probably invalid records.

What was the highest tip (measured in percentage of the original fee) ever given?

See the answer

```sql
select
  total_amount as fee,
  tip_amount as tip,
  tip_amount / total_amount * 100 as tip_percentage
from ny_taxi_data
where total_amount > 0
order by 3 desc
limit 5
```

returns

```
 fee  | tip  |   tip_percentage
------+------+--------------------
  4.2 | 10.0 |  238.0952380952381
 18.2 | 25.0 | 137.36263736263737
 8.24 | 9.24 | 112.13592233009709
 0.66 | 0.66 |              100.0
 0.01 | 0.01 |              100.0
```

### Create Additional Dashboards

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

### Load Additional Data

You can upload additional data with any S3 client, such as the AWS CLI or `rclone` configured as described above. It is recommended to put the data into a folder (prefix) in the `demo` bucket.

Have a look at the defined tables inside the `hive.demo` schema on how to inform Trino about the newly available data.

Table Definitions

```sql
show create table hive.demo.ny_taxi_data_raw
```

produces something like

```sql
CREATE TABLE hive.demo.ny_taxi_data_raw (
  VendorID BIGINT,
  tpep_pickup_datetime TIMESTAMP(3),
  tpep_dropoff_datetime TIMESTAMP(3),
  passenger_count DOUBLE,
  trip_distance DOUBLE,
  payment_type BIGINT,
  Fare_amount DOUBLE,
  Tip_amount DOUBLE,
  Total_amount DOUBLE
) WITH (
  external_location = 's3a://demo/ny-taxi-data/raw/',
  format = 'parquet'
)
```

If you want to transform or filter your data in any way before using it e.g. in Superset you can create a view as follows:

```sql
show create view hive.demo.ny_taxi_data
```

produces something like

```sql
CREATE VIEW hive.demo.ny_taxi_data SECURITY DEFINER AS
SELECT
  vendorid,
  tpep_pickup_datetime,
  tpep_dropoff_datetime,
  date_diff('minute', tpep_pickup_datetime, tpep_dropoff_datetime) duration_min,
  passenger_count,
  trip_distance,
  (CASE payment_type WHEN 1 THEN 'Credit card' WHEN 2 THEN 'Cash' WHEN 3 THEN 'No charge' WHEN 4 THEN 'Dispute' WHEN 6 THEN 'Voided trino' ELSE 'Unknown' END) payment_type,
  fare_amount,
  tip_amount,
  total_amount
FROM hive.demo.ny_taxi_data_raw
WHERE tpep_pickup_datetime >= from_iso8601_timestamp('2019-12-01T00:00:00')
AND tpep_pickup_datetime <= from_iso8601_timestamp('2022-05-31T00:00:00')
```

### Connect to Trino via CLI, Python or DBeaver

If you prefer running your SQL statements via command-line, a Python script or a graphical database manager like DBeaver, consult the [the Trino documentation](https://trino.io/docs/current/client.html) on how to do that.
