Welcome to Our House!
Come on in and stay a while!! We would love to introduce ourselves and say "Hello".
Analyzing Covid-19 Data with Superset and ClickHouse
Duration: 30 minutes
Audience: Anyone interested in learning how to connect Superset to ClickHouse for building visualizations and dashboards. The assumption is that you are new to ClickHouse.
Issues/Questions: Contact us at [email protected]
Overview: This tutorial is more of a journey…it has many moving parts to get to the final result - which is the ability to analyze data using ClickHouse and Superset. But the end result is worth the effort! You will insert some Covid-19 data in a CSV format into ClickHouse, then analyze the data visually using charts in Superset:
Let’s get started!
You will be installing ClickHouse and Superset on your local machine.
- You will need a Linux or Mac OS X machine
- Windows users will need to either:
- run ClickHouse within the WSL (Windows Subsystem for Linux)
- run Linux in a virtual machine using something like VirtualBox
- create a Linux instance using your favorite cloud provider
1. Installing ClickHouse
There are several ways to install ClickHouse, including DEB and RPM packages. In this tutorial, we will simply download a pre-built binary and execute it. For simplicity, I performed all of the tasks in my home directory.
Start by opening a terminal and creating a folder for the ClickHouse binary:
mkdir clickhouse cd clickhouse
Find your OS in the following table, then copy-and-paste the corresponding command to download a pre-built ClickHouse binary and make it executable:
|Operating System||Run this command:|
2. Defining a database and table
Complete the following steps to startup the ClickHouse server and use the ClickHouse client to define a new database and table.
First, you need to start the ClickHouse server:
It won’t take long for ClickHouse to start - but wait for the following message:
<Information> Application: Ready for connections.
In a new terminal, cd into the clickhouse folder and use the clickhouse client to define a new database named covid19db. Notice this command demonstrates how to submit a SQL command to ClickHouse from the command line:
cd clickhouse ./clickhouse client --query "CREATE DATABASE covid19db"
The Covid-19 data has over 60 columns, and most of them are decimal numbers. It’s a lot of information, but it allows us to answer a lot of questions about the pandemic. But because the schema is large, we are not going to send it using the –query flag. You will now learn how to submit a SQL command that is saved in a text file to a ClickHouse database. Start by creating a new text file in your home folder named ~/daily_totals.sql that contains the following CREATE TABLE command:
CREATE TABLE IF NOT EXISTS covid19db.daily_totals ( iso_code String, continent String, location String, date Date, total_cases Float32, new_cases Float32, new_cases_smoothed Float32, total_deaths Float32, new_deaths Float32, new_deaths_smoothed Float32, total_cases_per_million Float32, new_cases_per_million Float32, new_cases_smoothed_per_million Float32, total_deaths_per_million Float32, new_deaths_per_million Float32, new_deaths_smoothed_per_million Float32, reproduction_rate Float32, icu_patients Float32, icu_patients_per_million Float32, hosp_patients Float32, hosp_patients_per_million Float32, weekly_icu_admissions Float32, weekly_icu_admissions_per_million Float32, weekly_hosp_admissions Float32, weekly_hosp_admissions_per_million Float32, new_tests Float32, total_tests Float32, total_tests_per_thousand Float32, new_tests_per_thousand Float32, new_tests_smoothed Float32, new_tests_smoothed_per_thousand Float32, positive_rate Float32, tests_per_case Float32, tests_units Float32, total_vaccinations Float32, people_vaccinated Float32, people_fully_vaccinated Float32, total_boosters Float32, new_vaccinations Float32, new_vaccinations_smoothed Float32, total_vaccinations_per_hundred Float32, people_vaccinated_per_hundred Float32, people_fully_vaccinated_per_hundred Float32, total_boosters_per_hundred Float32, new_vaccinations_smoothed_per_million Float32, stringency_index Float32, population Float32, population_density Float32, median_age Float32, aged_65_older Float32, aged_70_older Float32, gdp_per_capita Float32, extreme_poverty Float32, cardiovasc_death_rate Float32, diabetes_prevalence Float32, female_smokers Float32, male_smokers Float32, handwashing_facilities Float32, hospital_beds_per_thousand Float32, life_expectancy Float32, human_development_index Float32, excess_mortality_cumulative Float32, excess_mortality Float32 ) ENGINE = MergeTree() ORDER BY (date)
Now run the following command from the clickhouse folder, which executes the command in daily_tables.sql:
./clickhouse client < ../daily_totals.sql
Verify the table exists by viewing its details:
./clickhouse client --query "DESCRIBE covid19db.daily_totals"
You should see the names and data types of all the columns in
3. Ingesting CSV files into ClickHouse
The Covid-19 data contains daily numbers from countries all over the world and was downloaded from Github at https://github.com/owid/covid-19-data/tree/master/public/data, but for some reason they decided to add random string values in some of the numeric columns, so those have been removed in a cleaned-up CSV file provided below.
Complete the following steps to download and insert the data into your ClickHouse table.
- Download the owid-covid-data.csv.zip file into your ~/clickhouse folder (for convenience) and unzip it.
License All visualizations, data, and code produced by Our World in Data are completely open access under the Creative Commons BY license. You have the permission to use, distribute, and reproduce these in any medium, provided the source and authors are credited.
In the case of our vaccination dataset, please give the following citation:
Mathieu, E., Ritchie, H., Ortiz-Ospina, E. et al. A global database of COVID-19 vaccinations. Nat Hum Behav (2021). https://doi.org/10.1038/s41562-021-01122-8
In the case of our testing dataset, please give the following citation:
Hasell, J., Mathieu, E., Beltekian, D. et al. A cross-country database of COVID-19 testing. Sci Data 7, 345 (2020). https://doi.org/10.1038/s41597-020-00688-8
The data produced by third parties and made available by Our World in Data is subject to the license terms from the original third-party authors. We will always indicate the original source of the data in our database, and you should always check the license of any such third-party data before use.
- To insert the data into your table, run the following command:
cat owid-covid-data.csv | ./clickhouse client --query "INSERT INTO covid19db.daily_totals FORMAT CSV"
- Now run the following command to verify it worked and view some of the data:
./clickhouse client --query "SELECT * FROM covid19db.daily_totals LIMIT 100"
You should 100 rows of mostly-numeric Covid-19 data from various countries.
4. Installing Superset
In this step, you will install and run Superset in a Python virtual environment:
- Make a new subfolder in your home folder:
mkdir superset cd superset
- Make sure you have virtualenv installed:
pip install virtualenv
- Create a new virtual environment:
python3 -m venv venv . venv/bin/activate
- Make sure pip and setuptools are up-to-date:
pip install --upgrade setuptools pip
- Install Apache Superset:
pip install apache-superset
- Install the Superset database:
superset db upgrade
- Create an admin user. I just used admin for both the username and password:
export FLASK_APP=superset superset fab create-admin
- The following command creates the defaults roles and permissions:
- Don’t miss this step! It installs the ClickHouse database driver for Superset, as well as a SQLAlchemy dialect that is needed by Superset:
pip install clickhouse-driver==0.2.0 && pip install clickhouse-sqlalchemy==0.1.6
- And now you are finally ready to start Superset. Feel free to choose a different port if needed:
superset run -p 8088 --with-threads --reload --debugger
- Open your web browser to http://localhost:8088. Login and you will see the welcome page for Superset:
5. Connecting Superset to ClickHouse
Now that you have both ClickHouse and Superset up and running, let’s connect the two of them:
Select Data from the top menu and then Databases from the drop-down menu. You do not have any databases defined yet, but notice there is a button to add a new one - click it:
In the first step of the wizard that starts, select ClickHouse as the type of database:
Enter “Covid19 Database” for the DISPLAY NAME.
Enter the following URI in the SQLALCHEMY URI field. The default before the @ is actually the username:password for ClickHouse. In this tutorial we did not define a ClickHouse user, and the default user does not have a password.
Try the TEST CONNECTION button and verify that Superset is connecting to your ClickHouse database properly:
Click the CONNECT button to complete the setup wizard, and you should now see your Covid19 Database in the list of databases.
To define new charts (visualizations) in Superset, you need to define the source of the data used in the charts - which is accomplished using datasets. From the top menu in Superset, select Data, then Datasets from the drop-down menu. You should see an empty list - let’s define one!
Click the button for adding a dataset. Select your new database as the datasource, covid19db for the schema, and daily_totals for the table:
Click the ADD button at the bottom of the dialog window and you should see daily_totals in the list of datasets. Congratulations!! You are ready to build a dashboard and analyze the data.
6. Creating charts and a dashboard in Superset
If you are familiar with Superset, then you will feel right at home with this next section. If you are new to Superset, well…it’s like a lot of the other cool visualization tools out there in the world - it doesn’t take long to get started, but the details and nuances get learned over time as you use the tool.
In this section, you will define a new dashboard and add charts (visualizations) to it.
Let’s start by creating a new dashboard to display our charts. From the top menu in Superset, select Dashboards. You should see an empty list.
Click the button in the upper-right to add a new dashboard. Name it Covid-19 Dashboard and click the SAVE button:
Now let’s create a new chart. Select Charts from the top menu and click the button to add a new chart. You will be shown a lot of options. For starters, select the Big Number chart. You will need to also choose a dataset, so select daily_totals from the CHOOSE A DATASET drop-down. When you are ready, click the CREATE NEW CHART button in the bottom-right corner:
You need to add a metric. Let’s display the total number of the
new_casesfield. Notice there is a column named DATA and a section named Query with a METRIC field that currently has a red warning (because it is not defined yet). Click where it says Add metric and a small dialog window appears:
Select the SIMPLE tab, then select new_cases for the column and SUM for the aggregation:
To view the actual number, click the RUN QUERY button. You will see a big number!
Change the title to Total New Cases, then click the SAVE button. Select Covid-19 Dashboard under the ADD TO DASHBOARD drop-down, then select SAVE & GO TO DASHBOARD. This will save the chart, add it to the dashboard, and display the dashboard:
That’s it! You have successfully built a dashboard in Superset based on data in ClickHouse. Feel free to play around with Superset and add more charts to your dashboard.
Congratulations on connecting Superset to ClickHouse!! You have opened up a whole new world of blazing fast data analytics.
What’s next: Check out the following lessons to continue your journey:
- The Ingest Nginx Logs into ClickHouse using Vector lesson demonstrates how to stream a log file into ClickHouse
- Check out What’s New in ClickHouse 21.10
- View all of our lessons on the Learn ClickHouse home page