Headstart With Apache Superset

Headstart With Apache Superset

Download apache superset, configure the database, and learn to apply filters as URLs.

Apache Superset:

Apache Superset is a modern data exploration and visualization platform that makes it easier to visualize data and make different types of charts. It is one of the few free open-source tools available for data visualization and in this article, I aim to provide you with all the basic configurations and techniques required to headstart you with apache superset.


Installation of apache superset with docker:

To download the apache superset we first need to install docker. You can check the link for the installation of docker depending on your OS.

Once docker has been installed open the terminal and follow the steps as specified below: (Apache superset download)

  • Download the apache superset docker image
docker run -d -p 8088:8088 --name superset apache/superset
  • Add the local admin account with specifying the username, password, etc details:
docker exec -it superset superset fab create-admin --username admin  --firstname Superset   --lastname Admin   --email admin@superset.com  --password admin
  • Migrate to the latest db
docker exec -it superset superset db upgrade
  • Load the sample examples
docker exec -it superset superset load_examples
  • Initialize roles- (also run if any new role is added)
docker exec -it superset superset init

Superset Login Page

Superset Login Page

Creating a database connection and dataset:

Once installed you can view some existing dashboards examples loaded to get a sense of the types of charts and visualizations available. Apache superset first makes a database connection to the database to view the tables and data used for visualizations. In this blog, we will use a population dataset that consists of the population recorded every 5 years from 1955 to 2020 and load it into MySQL database.

For the docker container of apache superset to connect with MySQL we first have to create a common network between them as follows:

  • Download MySQL with docker:
docker run -p 3306:3306 --name mysql -d -e MYSQL_DATABASE=superset -e MYSQL_ROOT_PASSWORD=root mysql
  • Create a docker network to connect superset and MySQL together:
docker network create superset_sql
  • Add the MySQL and superset running containers to the network named “superset_sql”:
docker network connect superset_sql mysql
docker network connect superset_sql superset

Now both the containers are finally on the same network and we can connect the database through superset UI. To connect to a database go to Settings -> Database Connections -> Add a new database connection by adding connection details as shown below:

Database connection

Note: The hostname is the docker container name of MySQL and the database name is also the same as specified while running the MySQL container.

Once the database connection is done we can import the dataset CSV file. Before importing go to the Advanced Settings -> Security and check the file uploads tab and finally click on upload file to the database -> Upload CSV. Next, add the CSV file and give a table name for the dataset as shown below:

File uploads tab

CSV upload to the database

Creating a simple Dashboard:

Once the dataset is added we can use that to create some charts and dashboards.

In Apache Superset chart creation and dashboarding can be understood by dividing them into the following components:

  • Dataset details contain all the column details of the dataset used for the chart.

  • Chart options include details regarding different types of charts and their customization options available.

  • Query processing options include query operations like aggregation, group by, order by, etc but in a visual drag-and-drop manner instead of writing the query for the same.

  • Query Results is the part where the results of the query are displayed.

  • The Output Area is where the final chart is displayed which in turn is also part of a dashboard.
    Some examples of charts in dashboards are as follows:

Total Population in 2020 (Big Number chart)

The population within countries in 2020 (Treemap chart)

Population in India over the years (table/bar chart)

Filters as URL in apache superset:

In Apache Superset, the Native filter's scope is limited to that of the same datasets, and in some cases for different datasets, only if the column name is the same.

To pass the filter as a URL along with full-screen mode which can be directly used to embed in an application follow the syntax:

http://localhost:8088/superset/dashboard/{'dashboard_ID'}/?native_filters=
(
NATIVE_FILTER-{'filter_ID'}:
(__cache:(label:{'filter_value'},validateStatus:!f,value:!({'filter_value'})),
extraFormData:(filters:!((col:{'filter_column_name'},op:IN,val:!({'filter_value'})))),
filterState:(label:{'filter_value'},validateStatus:!f,value:!({'filter_value'})),
id:NATIVE_FILTER-{'filter_ID'},ownState:())
)
&standalone=3&show_filters=0

As an example in the sample dashboard, we can create a native filter on different years available as:

Native filter for year column

Now we get the filter id by first clicking on Edit Dashboard -> Edit Properties -> Advanced tab where the dashboard JSON file is available as :

Highlighted text displays FILTER ID

Now we can pass the filter as follows to view the dashboard in full-screen mode with filter values passed from the URL:

http://localhost:8088/superset/dashboard/11/?native_filters=
(
NATIVE_FILTER-F9htsowEo:
(__cache:(label:'2020',validateStatus:!f,value:!('2020')),
extraFormData:(filters:!((col:year,op:IN,val:!('2020')))),
filterState:(label:'2020',validateStatus:!f,value:!('2020')),
id:NATIVE_FILTER-F9htsowEo,ownState:())
)
&standalone=3&show_filters=0

Hope you learned about apache superset from downloading and creating a simple dashboard to learning about filters as URLs.

Thank you for taking the time in reading this article.

P.S. this is my first time writing a blog so would love to receive feedback:)

Did you find this article valuable?

Support Mushrifah Hasan by becoming a sponsor. Any amount is appreciated!