1 Superset概述

https://superset.apache.org
https://github.com/apache/superset

superset是一个开源的、现代的、轻量级BI分析工具,支持多种数据源、拥有丰富的图表展示形式、支持自定义仪表盘。
superset能够对接常用的大数据分析工具,如Hive、Kylin、Durid等,支持自定义仪表盘,可作为数仓的可视化工具。

2 Superset安装部署

superset是由python语言开发的web应用,要求python3.6环境

2.1 安装Miniconda

conda是一个开源的包、环境管理器,可以用于在一个机器上安装不同python版本的软件包及其依赖,并且能够在不同的python环境之间切换。
Anaconda包括Conda、Python以及一大堆安装好的工具包,比如:numpy、pandas等,Miniconda包括Conda、Python。我们不需要如此多的工具包,故选择MiniConda。


1.下载Miniconda(python3版本)

  1. wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

2.安装 Miniconda

  1. bash Miniconda3-latest-Linux-x86_64.sh
  1. Please, press ENTER to continue 回车即可
  2. Do you accept the license terms? [yes|no] 输入yes
  3. Miniconda3 will now be installed into this location:
  4. /root/miniconda3
  5. - Press ENTER to confirm the location
  6. - Press CTRL-C to abort the installation
  7. - Or specify a different location below
  8. [/root/miniconda3] >>> 可以直接回车,使用默认安装路径
  9. Do you wish the installer to initialize Miniconda3
  10. by running conda init? [yes|no]
  11. [no] >>> 输入 yes

加载环境变量配置文件,使之生效

  1. source ~/.bashrc

取消激活base环境
miniconda安装完成后,再次启动会话窗口会进入到base环境,需要禁止激活base环境

  1. conda config --set auto_activate_base false

2.2 创建Python3.6环境

superset开发测试使用的是python3.6环境,支持python3.6和python3.7的环境

1.配置conda国内镜像

  1. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  2. conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  3. conda config --set show_channel_urls yes

2.创建python3.6环境

  1. conda create --name superset python=3.6

3.激活superset环境

  1. -- 列出所有conda 中的python环境
  2. conda info --envs
  3. (base) [root@localhost ~]# conda info --envs
  4. # conda environments:
  5. #
  6. base * /root/miniconda3
  7. superset /root/miniconda3/envs/superset

切换到superset

  1. conda activate superset

退出当前环境

  1. conda deactivate

2.3 superset 部署

1.安装依赖

  1. sudo yum install -y python-setuptools
  2. sudo yum install -y gcc gcc-c++ libffi-devel python-devel python-pip python-wheel openssl-devel cyrus-sasl-devel openldap-devel

2.安装(更新)setuptools和pip
pip是python的包管理工具,可以和centos中的yum类比

  1. pip install --upgrade setuptools pip -i https://pypi.douban.com/simple/

3.安装superset
-i的作用是指定镜像,这里选择的是国内的镜像

  1. pip install apache-superset -i https://pypi.douban.com/simple/
  1. pip3 install sqlalchemy==1.3.24 -i https://pypi.douban.com/simple/
  2. pip3 install dataclasses -i https://pypi.douban.com/simple/

4.初始化superset数据库

  1. superset db upgrade

5.创建管理员账户
flask是一个python web框架,superset使用的就是flask

  1. export FLASK_APP=superset
  2. flask fab create-admin

6.superset初始化

  1. superset init

2.4 启动superset

1.安装gunicorn

说明:gunicorn是一个Python Web Server,可以和java中的TomCat类比

  1. pip install gunicorn -i https://pypi.douban.com/simple/

2.启动superset
注意!确保当前conda环境为superset

  • –workers:指定进程个数
  • –timeout:worker进程超时时间,超时会自动重启
  • –bind:绑定本机地址,即为superset访问地址
  • –daemon:后台运行

IP 地址要写服务器IP,不要写localhost 或127.0.0.1

  1. gunicorn --workers 5 --timeout 120 --bind 10.0.6.76:8787 "superset.app:create_app()" --daemon

3.登录
http://IP:8787

4.停止superset
1.停止superset进程

  1. ps -ef | awk '/superset/ && !/awk/{print $2}' | xargs kill -9

2.退出superset环境

  1. conda deactivate

2.5 superset 启停脚本

  1. #!/bin/bash
  2. superset_status(){
  3. result=`ps -ef | awk '/gunicorn/ && !/awk/{print $2}' | wc -l`
  4. if [[ $result -eq 0 ]]; then
  5. return 0
  6. else
  7. return 1
  8. fi
  9. }
  10. superset_start(){
  11. # 该段内容取自~/.bashrc,所用是进行conda初始化
  12. # >>> conda initialize >>>
  13. # !! Contents within this block are managed by 'conda init' !!
  14. __conda_setup="$('/opt/module/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
  15. if [ $? -eq 0 ]; then
  16. eval "$__conda_setup"
  17. else
  18. if [ -f "/opt/module/miniconda3/etc/profile.d/conda.sh" ]; then
  19. . "/opt/module/miniconda3/etc/profile.d/conda.sh"
  20. else
  21. export PATH="/opt/module/miniconda3/bin:$PATH"
  22. fi
  23. fi
  24. unset __conda_setup
  25. # <<< conda initialize <<<
  26. superset_status >/dev/null 2>&1
  27. if [[ $? -eq 0 ]]; then
  28. conda activate superset ; gunicorn --workers 5 --timeout 120 --bind hadoop102:8787 --daemon 'superset.app:create_app()'
  29. else
  30. echo "superset正在运行"
  31. fi
  32. }
  33. superset_stop(){
  34. superset_status >/dev/null 2>&1
  35. if [[ $? -eq 0 ]]; then
  36. echo "superset未在运行"
  37. else
  38. ps -ef | awk '/gunicorn/ && !/awk/{print $2}' | xargs kill -9
  39. fi
  40. }
  41. case $1 in
  42. start )
  43. echo "启动Superset"
  44. superset_start
  45. ;;
  46. stop )
  47. echo "停止Superset"
  48. superset_stop
  49. ;;
  50. restart )
  51. echo "重启Superset"
  52. superset_stop
  53. superset_start
  54. ;;
  55. status )
  56. superset_status >/dev/null 2>&1
  57. if [[ $? -eq 0 ]]; then
  58. echo "superset未在运行"
  59. else
  60. echo "superset正在运行"
  61. fi
  62. esac

3.使用

说明:对接不同的数据源,需安装不同的依赖,以下地址为官网说明
http://superset.apache.org/installation.html#database-dependencies

以下以 starrocks 为例
starrocks 使用的是mysql 驱动,需要在conda环境中安装 mysql 驱动

  1. conda install mysqlclient

重启 superset

  1. ./superset.sh restart

数据源配置,例子为starrocks URI

  1. mysql://root:123456@10.0.6.173:9030/ssb?charset=utf8