Skip to main content
Upstream deployment example. This page uses community images, charts, or an upstream lab. Get Enterprise Edition installation files and confirm the supported image, chart, and operator versions for your release. This tutorial covers:
  • Running the database in a single Docker container
  • Loading two public datasets including basic transformation of the data
  • Analyzing the data with SELECT and JOIN
  • Basic data transformation (the T in ETL)

Follow along with the video if you prefer

The data used is provided by NYC OpenData and the National Centers for Environmental Information. Both of these datasets are very large, and because this tutorial is intended to help you get exposed to working with the database we are not going to load data for the past 120 years. You can run the Docker image and load this data on a machine with 4Ā GB RAM assigned to Docker. For larger fault-tolerant and scalable deployments we have other documentation and will provide that later. There is a lot of information in this document, and it is presented with the step by step content at the beginning, and the technical details at the end. This is done to serve these purposes in this order:
  1. Allow the reader to load data in the database and analyze that data.
  2. Explain the basics of data transformation during loading.

Prerequisites

Docker

  • Docker
  • 4Ā GB RAM assigned to Docker
  • 10Ā GB free disk space assigned to Docker

SQL client

You can use the SQL client provided in the Docker environment, or use one on your system. Many MySQL compatible clients will work, and this guide covers the configuration of DBeaver and MySQL Workbench.

curl

curl is used to issue the data load job to the database, and to download the datasets. Check to see if you have it installed by running curl or curl.exe at your OS prompt. If curl is not installed, get curl here.

Terminology

FE

Frontend nodes are responsible for metadata management, client connection management, query planning, and query scheduling. Each FE stores and maintains a complete copy of metadata in its memory, which guarantees indiscriminate services among the FEs.

BE

Backend nodes are responsible for both data storage and executing query plans.

Launch the database


SQL clients



These three clients are tested with this tutorial, you only need one:
  • mysql CLI: You can run this from the Docker environment or your machine.
  • DBeaver is available as a community version and a Pro version.
  • MySQL Workbench

Configuring the client

mysql CLI The easiest way to use the mysql CLI is to run it from the container starrocks-fe:
If you would like to install the mysql CLI expand mysql client install below: mysql client install
  • macOS: If you use Homebrew and do not need MySQL Server run brew install mysql-client@8.0 to install the CLI.
  • Linux: Check your repository system for the mysql client. For example, yum install mariadb.
  • Microsoft Windows: Install the MySQL Community Server and run the provided client, or run mysql from WSL.
DBeaver
  • Install DBeaver, and add a connection: Add a connection
  • Configure the port, IP, and username. Test the connection, and click Finish if the test succeeds: Configure and test
MySQL Workbench
  • Install the MySQL Workbench, and add a connection.
  • Configure the port, IP, and username and then test the connection: Connection settings
  • You will see warnings from the Workbench as it is checking for a specific MySQL version. You can ignore the warnings and when prompted, you can configure Workbench to stop displaying the warnings: Ignore warning

Download the data

Download these two datasets to your machine. You can download them to the host machine where you are running Docker, they do not need to be downloaded inside the container.

New York City crash data

Weather data


Connect to the database with a SQL client

Tip If you are using a client other than the mysql CLI, open that now. This command will run the mysql command in the Docker container:

Create some tables



Create a database

Type these two lines in at the StarRocks > prompt and press enter after each:

Create two tables

Crashdata

The crash dataset contains many more fields than these, the schema has been trimmed down to include only the fields that might be useful to answer questions about the impact weather has on driving conditions.

Weatherdata

Similar to the crash data, the weather dataset has many more columns (a total of 125 columns) and only the ones that are expected to answer the questions are included in the database.

Load two datasets

There are many ways to load data into the database. For this tutorial the simplest way is to use curl and Stream Load. Tip Open a new shell as these curl commands are run at the operating system prompt, not in the mysql client. The commands refer to the datasets that you downloaded, so run them from the directory where you downloaded the files. You will be prompted for a password. You probably have not assigned a password to the MySQL root user, so just hit enter. The curl commands look complex, but they are explained in detail at the end of the tutorial. For now, we recommend running the commands and running some SQL to analyze the data, and then reading about the data loading details at the end.

New York City collision data - Crashes

Here is the output of the preceding command. The first highlighted section shows what you should expect to see (OK and all but one row inserted). One row was filtered out because it does not contain the correct number of columns.
If there was an error the output provides a URL to see the error messages. Open this in a browser to find out what happened. Expand the detail to see a sample error message: Reading error messages in the browser

Weather data

Load the weather dataset in the same manner as you loaded the crash data.

Answer some questions



These queries can be run in your SQL client. All of the queries use the quickstart database.

How many crashes are there per hour in NYC?

Here is part of the output. Note that I am looking closer at January 6th and 7th as this is Monday and Tuesday of a non-holiday week. Looking at New Years Day is probably not indicative of a normal morning during rush-hour traffic.
It looks like about 40 accidents on a Monday or Tuesday morning during rush hour traffic, and around the same at 17:00 hours.

What is the average temperature in NYC?

Output: Note that this is data from 2014, NYC has not been this cold lately.

Is it safe to drive in NYC when visibility is poor?

Let’s look at the number of crashes when visibility is poor (between 0 and 1.0 miles). To answer this question use a JOIN across the two tables on the DATETIME column.
The highest number of crashes in a single hour during low visibility is 129. There are multiple things to consider:
  • February 3rd 2014 was a Monday
  • 8AM is rush hour
  • It was raining (0.12 inches or precipitation that hour)
  • The temperature is 32 degrees F (the freezing point for water)
  • Visibility is bad at 0.25 miles, normal for NYC is 10 miles

What about driving in icy conditions?

Water vapor can desublimate to ice at 40 degrees F; this query looks at temps between 0 and 40 degrees F.
The results for freezing temperatures suprised me a little, I did not expect too much traffic on a Sunday morning in the city on a cold January day.A quick look at weather.com showed that there was a big storm with many crashes that day, just like what can be seen in the data.
Drive carefully!

Summary

In this tutorial you:
  • Deployed the database in Docker
  • Loaded crash data provided by New York City and weather data provided by NOAA
  • Analyzed the data using SQL JOINs to find out that driving in low visibility or icy streets is a bad idea
There is more to learn; we intentionally glossed over the data transformation done during the Stream Load. The details on that are in the notes on the curl commands below.

Notes on the curl commands



Stream Load and curl take many arguments. Only the ones used in this tutorial are described here, the rest will be linked to in the more information section.

--location-trusted

This configures curl to pass credentials to any redirected URLs.

-u root

The username used to log in to the database

-T filename

T is for transfer, the filename to transfer.

label:name-num

The label to associate with this Stream Load job. The label must be unique, so if you run the job multiple times you can add a number and keep incrementing that.

column_separator:,

If you load a file that uses a single , then set it as shown above, if you use a different delimiter then set that delimiter here. Common choices are \t, ,, and |.

skip_header:1

Some CSV files have a single header row with all of the column names listed, and some add a second line with datatypes. Set skip_header to 1 or 2 if you have one or two header lines, and set it to 0 if you have none.

enclose:\"

It is common to enclose strings that contain embedded commas with double-quotes. The sample datasets used in this tutorial have geo locations that contain commas and so the enclose setting is set to \". Remember to escape the " with a \.

max_filter_ratio:1

This allows some errors in the data. Ideally this would be set to 0 and the job would fail with any errors. It is set to 1 to allow all rows to fail during debugging.

columns:

The mapping of CSV file columns to table columns. You will notice that there are many more columns in the CSV files than columns in the table. Any columns that are not included in the table are skipped. You will also notice that there is some transformation of data included in the columns: line for the crash dataset. It is very common to find dates and times in CSV files that do not conform to standards. This is the logic for converting the CSV data for the time and date of the crash to a DATETIME type:
The columns line
This is the beginning of one data record. The date is in MM/DD/YYYY format, and the time is HH:MI. Since DATETIME is generally YYYY-MM-DD HH:MI:SS we need to transform this data.
This is the beginning of the columns: parameter:
This instructs the database to:
  • Assign the content of the first column of the CSV file to tmp_CRASH_DATE
  • Assign the content of the second column of the CSV file to tmp_CRASH_TIME
  • concat_ws() concatenates tmp_CRASH_DATE and tmp_CRASH_TIME together with a space between them
  • str_to_date() creates a DATETIME from the concatenated string
  • store the resulting DATETIME in the column CRASH_DATE

More information

table design Stream Load The Motor Vehicle Collisions - Crashes dataset is provided by New York City subject to these terms of use and privacy policy. The Local Climatological Data(LCD) is provided by NOAA with this disclaimer and this privacy policy.