Apache Superset 是一个现代化的企业级商业智能 Web 应用程序,快速、轻量和直观。Superset支持接入各类数据源,提供了丰富的图表,所有技能组合的用户都可以轻松地对数据进行探索和可视化。
Superset是基于Python环境开发, 我们需要一套Python的环境。 推荐使用Conda搭建Python环境,对环境进行管理,在本地操作系统可搭建多套环境, 实现对环境的隔离、升级和切换等。
Conda是一个在Windows、macOS和Linux上运行的开源软件包和环境管理系统。Conda可以快速安装、运行和更新软件包及其依赖项。它还可以轻松地在本地计算机上创建、保存、加载和切换环境。它是为Python程序创建的,但它可以为任何语言打包和分发软件。支持的编程语言:Python, R, Ruby, Lua, Scala, Java, JavaScript, C/ C++, FORTRAN等。
Miniconda是conda的简化版本, 仅包括conda、Python及其所依赖的包,以及少量其他有用的包,包括pip、zlib和其他一些包。
Miniconda的官方文档:
我们使用Miniconda管理Superset的环境。
下载链接:
下载好的安装脚本如下:
# ls -lh
总用量 74M
-rw-r--r-- 1 root root 74M 8月 24 11:22 Miniconda3-py39_4.12.0-Linux-x86_64.sh
安装Miniconda:
bash Miniconda3-py39_4.12.0-Linux-x86_64.sh
提示输入回车键(Enter),阅读协议,然后提示是否已接受licence条款,如下所示,输入yes:
Do you accept the license terms? [yes|no]
[no] >>>
Please answer 'yes' or 'no':'
如下提示,输入安装的位置,直接回车:
Miniconda3 will now be installed into this location:
/root/miniconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/root/miniconda3] >>>
是否运行conda init,输入yes,完成安装:
installation finished.
Do you wish the installer to initialize Miniconda3
by running conda init? [yes|no]
[no] >>>
使Superset的环境变量生效:
$ source ~/.bashrc
验证Miniconda是否安装成功, 输入conda help
,显示如下,则表示conda安装成功:
# conda help
usage: conda [-h] [-V] command ...
conda is a tool for managing and deploying applications, environments and packages.
Options:
positional arguments:
command
clean Remove unused packages and caches.
compare Compare packages between conda environments.
config Modify configuration values in .condarc. This is modeled after the git config command. Writes to the user .condarc file (/root/.condarc)
by default.
create Create a new conda environment from a list of specified packages.
help Displays a list of available conda commands and their help strings.
info Display information about current conda install.
init Initialize conda for shell interaction. [Experimental]
install Installs a list of packages into a specified conda environment.
list List linked packages in a conda environment.
package Low-level conda package utility. (EXPERIMENTAL)
remove Remove a list of packages from a specified conda environment.
uninstall Alias for conda remove.
run Run an executable in a conda environment.
search Search for packages and display associated information. The input is a MatchSpec, a query language for conda packages. See examples
below.
update Updates conda packages to the latest compatible version.
upgrade Alias for conda update.
optional arguments:
-h, --help Show this help message and exit.
-V, --version Show the conda version number and exit
指定Python版本为3.9.5
conda create -n superset python=3.9.5
安装过程,是否安装一些新的包,如下。 输入y,然后继续。
The following NEW packages will be INSTALLED:
_libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main
_openmp_mutex pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu
ca-certificates pkgs/main/linux-64::ca-certificates-2022.07.19-h06a4308_0
certifi pkgs/main/linux-64::certifi-2022.6.15-py39h06a4308_0
ld_impl_linux-64 pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1
libffi pkgs/main/linux-64::libffi-3.3-he6710b0_2
libgcc-ng pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1
libgomp pkgs/main/linux-64::libgomp-11.2.0-h1234567_1
libstdcxx-ng pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1
ncurses pkgs/main/linux-64::ncurses-6.3-h5eee18b_3
openssl pkgs/main/linux-64::openssl-1.1.1q-h7f8727e_0
pip pkgs/main/linux-64::pip-22.1.2-py39h06a4308_0
python pkgs/main/linux-64::python-3.9.5-h12debd9_4
readline pkgs/main/linux-64::readline-8.1.2-h7f8727e_1
setuptools pkgs/main/linux-64::setuptools-63.4.1-py39h06a4308_0
sqlite pkgs/main/linux-64::sqlite-3.39.2-h5082296_0
tk pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0
tzdata pkgs/main/noarch::tzdata-2022a-hda174b7_0
wheel pkgs/main/noarch::wheel-0.37.1-pyhd3eb1b0_0
xz pkgs/main/linux-64::xz-5.2.5-h7f8727e_1
zlib pkgs/main/linux-64::zlib-1.2.12-h7f8727e_2
Proceed ([y]/n)? y
安装完成后的界面:
libstdcxx-ng-11.2.0 | 4.7 MB | ################################################################################################################# | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate superset
#
# To deactivate an active environment, use
#
# $ conda deactivate
使用如下命令激活环境:
conda activate superset
使用如下命令退出环境:
conda deactivate
完全移除环境:
conda remove -n superset --all
使用如下命令安装apache-superset:
pip3 install apache-superset==1.4.2 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
-i
指定pip下载的镜像地址。由于镜像地址是http,而不是https,因此,通过–trusted-host
指定受信任的主机。命令如下:
superset db upgrade
这一步可能会报异常,异常信息如下:
# superset db upgrade
Traceback (most recent call last):
File "/root/miniconda3/envs/superset/bin/superset", line 5, in <module>
from superset.cli import superset
File "/root/miniconda3/envs/superset/lib/python3.9/site-packages/superset/__init__.py", line 18, in <module>
from flask import current_app, Flask
File "/root/miniconda3/envs/superset/lib/python3.9/site-packages/flask/__init__.py", line 14, in <module>
from jinja2 import escape
File "/root/miniconda3/envs/superset/lib/python3.9/site-packages/jinja2/__init__.py", line 12, in <module>
from .environment import Environment
File "/root/miniconda3/envs/superset/lib/python3.9/site-packages/jinja2/environment.py", line 25, in <module>
from .defaults import BLOCK_END_STRING
File "/root/miniconda3/envs/superset/lib/python3.9/site-packages/jinja2/defaults.py", line 3, in <module>
from .filters import FILTERS as DEFAULT_FILTERS # noqa: F401
File "/root/miniconda3/envs/superset/lib/python3.9/site-packages/jinja2/filters.py", line 13, in <module>
from markupsafe import soft_unicode
ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/root/miniconda3/envs/superset/lib/python3.9/site-packages/markupsafe/__init__.py)
这是因为MarkupSafe的版本过高导致上面的异常。
查看版本:
# pip list | grep MarkupSafe
MarkupSafe 2.1.1
卸载,安装低版本的MarkupSafe:
$ pip uninstall MarkupSafe
$ pip install MarkupSafe==2.0.1
安装完成之后,再次初始化数据库:
superset db upgrade
在环境变量文件中配置Superset:
export FLASK_APP=superset
创建管理员用户:
superset fab create-admin
指定用户名为admin、密码为123456。
先初始化Superset:
superset init
然后使用如下命令启动Superset:
superset run -h 0.0.0.0 -p 8088 --with-threads --reload --debugger
-h
指定监听的IP地址,-p
指定端口。启动日志如下:
# superset run -h 0.0.0.0 -p 8088 --with-threads --reload --debugger
--------------------------------------------------------------------------------
WARNING
--------------------------------------------------------------------------------
A Default SECRET_KEY was detected, please use superset_config.py to override it.
Use a strong complex alphanumeric string and use a tool to help you generate
a sufficiently random sequence, ex: openssl rand -base64 42
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
logging was configured successfully
2022-08-24 14:04:19,630:INFO:superset.utils.logging_configurator:logging was configured successfully
2022-08-24 14:04:19,641:INFO:root:Configured event logger of type <class 'superset.utils.log.DBEventLogger'>
/root/miniconda3/envs/superset/lib/python3.9/site-packages/flask_caching/__init__.py:119: UserWarning: Flask-Caching: CACHE_TYPE is set to null, caching is effectively disabled.
warnings.warn(
No PIL installation found
2022-08-24 14:04:20,034:INFO:superset.utils.screenshots:No PIL installation found
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
2022-08-24 14:04:22,188:INFO:werkzeug: * Running on http://0.0.0.0:8088/ (Press CTRL+C to quit)
2022-08-24 14:04:22,189:INFO:werkzeug: * Restarting with stat
--------------------------------------------------------------------------------
WARNING
--------------------------------------------------------------------------------
A Default SECRET_KEY was detected, please use superset_config.py to override it.
Use a strong complex alphanumeric string and use a tool to help you generate
a sufficiently random sequence, ex: openssl rand -base64 42
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
logging was configured successfully
2022-08-24 14:04:25,198:INFO:superset.utils.logging_configurator:logging was configured successfully
2022-08-24 14:04:25,208:INFO:root:Configured event logger of type <class 'superset.utils.log.DBEventLogger'>
/root/miniconda3/envs/superset/lib/python3.9/site-packages/flask_caching/__init__.py:119: UserWarning: Flask-Caching: CACHE_TYPE is set to null, caching is effectively disabled.
warnings.warn(
No PIL installation found
2022-08-24 14:04:25,620:INFO:superset.utils.screenshots:No PIL installation found
2022-08-24 14:04:27,573:WARNING:werkzeug: * Debugger is active!
2022-08-24 14:04:27,609:INFO:werkzeug: * Debugger PIN: 144-894-422
在浏览器打开Superset的地址:
http://${your_ip}:8088/
用户名和密码分别为admin和123456 。
界面界面如下:
主页如下:
安装支持ClickHouse的Python库。ClickHouse官方提供的Python库, 支持HTTP协议的库:
pip install clickhouse-connect
重启Superset生效。
然后到界面上创建ClickHouse的连接
(1)、在顶部菜单,选择Data,下拉选择Databases,添加一个新的数据库。
(2)、第一步,选择"ClickHouse Connect"作为数据库的类型。
(3)、第二步,输入:
(4)、点击"CONNECT"和下一步的"FINISH"按钮完成设置向导。 在页面上显示添加的数据库列表。
使用Superset与ClickHouse交互,需要定义一个数据集。
(1)、在顶部菜单,选择Data,下拉选择Datasets,添加一个数据集。
(2)、点击添加数据集的按钮,选择一个数据库作为数据源,下拉将看到定义的库和表。
下拉选择DATABASE、SCHEMA和TABLE:
(3)、点击ADD按钮, 在页面上显示添加的数据集列表。
在Superset中创建charts和dashboard, 一个dashboard由多个charts组成。
(1)、在顶部菜单,选择Charts,点击添加图表按钮CHART。
(2)、选择数据集、图表,点击"CREATE NEW CHART"按钮
(3)、添加Metric
(4)、指定Metric为COUNT(*)
。
(5)、点击左上侧的"RUN"或者右侧的"RUN QUERY"按钮,查看运行的结果。
效果如下:
(6)、点击左上侧的"SAVE"按钮,指定"CHART NAME", 保存chart。
在Charts页面,可以看到创建的Chart列表:
(1)、在顶部菜单,选择Dashboards,点击添加按钮DASHBOARD。
(2)、切换至CHARTS,可以看到创建的charts列表。
(3)、在"bignumber"的chart上面,按住鼠标左键,拖拽至左侧的画布。
(4)、修改dashboard名称,并保存。
Dashboard的效果如下:
刚刚介绍的是基于ClickHouse官方提供的ClickHouse Driver,连接至ClickHouse,使用的是HTTP的协议。Superset官方也提供了ClickHouse的Driver, 使用的TCP的协议。
除了在Superset创建数据库有所差异,其他使用方式与ClickHouse提供的driver相同。
(1)、安装Python库
pip install clickhouse-driver==0.2.4
pip install clickhouse-sqlalchemy==0.1.10
(2)、创建数据库
第一步,选择ClickHouse类型。
第二步, 指定URI:
SQLALCHEMY URI格式:
clickhouse+native://{username}:{password}@{hostname}:{port}/{database}
端口为TCP端口,默认为90000。