【Docker】Deploy Superset

"Data visualization" is an eternal topic in Internet development. The cost of self-development is too high, and it is too expensive to use BI tools that are mature in the market (big companies don't care, small companies don't want to spend this money after all). In fact, our company is also facing this situation. After a long period of selection, we finally selected the open source Superset as a visualization tool. The following explains how the Docker version of Superset is built, and uses an example to illustrate how to load superset in an external project. page.

1. build

First, let's download the 1.4.0 image from DockerHub. (As of February 2022, the officially recommended version is 1.4.0). In fact, I didn't use the 1.4.0 version directly at the beginning, I set up the 1.3.x mirror and passed

pip install apache-superset --upgrade

When command update is found it automatically downloads the latest version 1.4.0 version. In this case, we can just check out the image of this version in DockerHub directly.

docker pull apache/superset:1.4.0

Since I am configuring and starting through Rancher later, there is no so-called script content here, but there is one place that needs attention, as follows:

/data/yzh/docker/superset/data:/app/superset_home

The Superset.db data file of version 1.4.0 is not placed in /var/lib/supersetthe path but /app/superset_homeunder the path. Remember to mount the corresponding path to the host. As for the mounting of the config.py file mentioned in other blog posts on the Internet, it is not necessary, because as long as the mounting path points to the path in the container, it /app/supersetwill not start. Therefore, config.py does not need to be mounted, we modify it and copy it directly.
Note: The storage path of the old version of config.py file is /usr/local/lib/python3.8/site-packages/superset/config.pyand the storage path of the 1.4.0 version is /app/superset.
After Rancher configures the mount directory, it can start the Superset service first. As follows:

[root@front ~]# docker ps | grep superset
134bcdefc578        apache/superset:1.4.0                                                   "/.r/r /usr/bin/dock…"   22 hours ago        Up 22 hours (healthy)

After the service is started, after querying in the server, the container ID number is 134bcdefc578, and then use the docker cp command to copy the file just mentioned /app/superset/config.pyto the host machine. The storage path here is /data/yzh/docker/superset/confas follows:

docker cp 134bcdefc578:/app/superset/config.py /data/yzh/docker/superset/conf/

After copying the config.py file to the host machine, modify the config.py through the local vim. By modifying the config.py file this time, we can achieve two functions:

  1. Realize dynamic SQL query by passing parameters;
  2. Can be cross-domain embedded into other systems through iframe;

First find PUBLIC_ROLE_LIKEthe parameter and modify it as follows:

PUBLIC_ROLE_LIKE: Optional[str] = "Gamma"

Then you need to remove the CORS restriction. The iframe cross-site access problem can be solved by removing it X-Frame-Options, as follows:

HTTP_HEADERS: Dict[str, Any] = {
    
    }

Then WTF_CSRF_ENABLEDset the parameter to False as follows:

WTF_CSRF_ENABLED = False

In addition, it is also necessary to satisfy the parameter query, so it also needs to be ENABLE_TEMPLATE_PROCESSINGset to True, as shown below:

"ENABLE_TEMPLATE_PROCESSING": True,

After that, you can save config.py and restart the docker container. Before restarting, remember to copy the config.py file to the container again.
After restarting the Superset container, enter the container to initialize data through exec, as follows:

docker exec -it 134bcdefc578 bash

First set FLASK_APPthe environment variable as follows:

export FLASK_APP=superset

Then by flask fab create-admincreating an admin user like so:

Username [admin]: admin
User first name [admin]: admin
User last name [user]: user
Email [[email protected]]: [email protected]
Password: 
Repeat for confirmation: 
INFO:root:logging was configured successfully
/usr/local/lib/python3.8/site-packages/flask_caching/__init__.py:191: UserWarning: Flask-Caching: CACHE_TYPE is set to null, caching is effectively disabled.
warnings.warn(
Recognized Database Authentications.
Admin User admin created.

After creating the user, you can initialize the data, execute the following two commands:

# 更新数据版本
superset db upgrade
# 初始化权限
superset init

After that, you can log in to the system with the administrator username and password you just created. As shown in the figure below:
1.png
Click "List Roles" to enter the role configuration page. You don't need to touch other roles here, you only need to configure Public. There is basically no problem with other permissions, just give these two operations, as shown in the figure below:
image.png
At this time, basically all configurations have been configured.

2. Use of external links

To use the external link, you need to create a report first. But creating reports requires the use of data sources and datasets.

2.1 Create a data source

image.png
Enter the configuration page from Data -> Databases, as shown in the figure below:
image.png
After selecting MySQL, fill in the relevant database configuration information, as shown in the figure below:
image.png
After completing the filling, click Save. When saving, it will try to see if the connection is successful, and it does not need to be tested as before. connect.

2.2 Create a dataset

After the data source is created, the data set can be created through the SQL statement, as shown in the figure below:
image.png
Enter the running page in SQL Lab -> SQL Editor, fill in the SQL statement according to the page prompts and execute it, as shown in the figure below:
2.png
After confirming that the query is correct, click " Save” button to save, as shown in the figure below:
3.png
After saving the script, you can click the “EXPLORE” button below to save it to the data set, as shown in the figure below:
4.png
After the save is completed, it will directly jump to the report page.

2.3 Create a report

image.png
Next, we can choose the report type that needs to be displayed. Since the business here is to finally display a data statistics (Count), we can choose "Big Number" here, as shown in the figure below: the report type
image.png
has , but the aggregation (Metric) content has not been defined yet, we still need to make a selection, as shown in the figure below:
image.png
After selection, we click "RUN" above to test the query results, as shown in the figure below:
image.png
After the test script runs successfully, we need to pass parameters Query, so you need to modify the content of the query data set, as shown in the figure below:
5.png
First click the bottom to unlock the page to edit, as shown
6.png
in the figure below: Use Jinja syntax to write the parameter name of the parameter where it needs to be passed ({ { url_param('beginDate') } }), here, since the range search is performed according to the time, it is agreed to pass in two parameters, one named beginDate and the other named endDate.
Since the name of the report cannot be seen after the external link, so here we write the content that needs to be expressed in the subtitle, as shown in the figure below: remember to save
image.png
after filling.

2.4 Create external links

After saving, you can get the iframe link in the upper right corner, as shown in the figure below:
image.png
After returning to our page, pass the beginDate and endDate into src as parameters, as shown in the figure below:
7.png
After refreshing the page, you can see the effect ,As shown below:
8.png

Guess you like

Origin blog.csdn.net/kida_yuan/article/details/128958988