Ubuntu 22.04部署Apache Superset

安装依赖

使用以下命令安装依赖

1
sudo apt-get install build-essential libssl-dev libffi-dev python3-dev python3-pip libsasl2-dev libldap2-dev default-libmysqlclient-dev 

配置Superset元数据库

本教程使用MySQL数据库作为Superset的数据库(Superset支持MySQL和PostgreSQL),安装完成后需进行以下配置。

  1. 在MySQL中创建Superset元数据库
1
CREATE DATABASE superset DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
  1. 创建Superset用户,用户名为superset,密码为superset。superset用户拥有所有数据库的全部权限。
1
2
3
create user superset@'%' identified WITH mysql_native_password BY 'superset';
grant all privileges on *.* to superset@'%' with grant option;
flush privileges;

为Superset创建Python虚拟环境

  1. 更新pip
1
sudo pip install --upgrade pip
  1. 下载必要的虚拟环境包
1
sudo pip install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple
  1. 创建虚拟环境
1
virtualenv superset
  1. 激活虚拟环境
1
source superset/bin/activate
  1. 安装Superset
1
pip install apache-superset -i https://pypi.tuna.tsinghua.edu.cn/simple
  1. 安装其他Python依赖
1
pip install gunicorn pymysql mysqlclient -i https://pypi.tuna.tsinghua.edu.cn/simple

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

配置Superset

在~/superset/bin目录下创建superset配置文件superset_config.py,详细配置可参考官方文档(https://superset.apache.org/docs/installation/configuring-superset/)或者GitHub(https://github.com/apache/superset/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Superset specific config
ROW_LIMIT = 5000

#SUPERSET_WEBSERVER_PORT = 8088

# Flask App Builder configuration
# Your App secret key will be used for securely signing the session cookie
# and encrypting sensitive information on the database
# Make sure you are changing this key for your deployment with a strong key.
# You can generate a strong key using `openssl rand -base64 42`

'''
使用命令“openssl rand -base64 42”创建SECRET_KEY填写到下面'''
SECRET_KEY = ''

# The SQLAlchemy connection string to your database backend
# This connection defines the path to the database that stores your
# superset metadata (slices, connections, tables, dashboards, ...).
# Note that the connection information to connect to the datasources
# you want to explore are managed directly in the web UI
'''
数据库连接,我是用的是MySQL数据库
链接字符串:mysql+pymysql://<数据库用户>:<密码>@<主机名/ip>/<数据库名>'''
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://superset:superset@hadoop01:3306/superset?charset=utf8'
ENABLE_CSRF_PROTECTION = True
# Flask-WTF flag for CSRF

WTF_CSRF_ENABLED = True
WTF_CSRF_CHECK_DEFAULT = True
# Add endpoints that need to be exempt from CSRF protection
#WTF_CSRF_EXEMPT_LIST = []
# A CSRF token that expires in 1 year
#WTF_CSRF_TIME_LIMIT = 60 * 60 * 24 * 365

#不填这个会出现登录界面输入正确的用户名和密码后登录无反应的现象
#但是关掉这个可能会降低安全性,可能是superset版本太新(3.0.0),旧版本貌似没有这个问题
TALISMAN_ENABLED=False

# Set this API key to enable Mapbox visualizations
MAPBOX_API_KEY = ''

COMPRESS_REGISTER = False

#默认中文
BABEL_DEFAULT_LOCALE = "zh"
#superset支持的语言
LANGUAGES = {
    "en": {"flag": "us", "name": "English"},
    "es": {"flag": "es", "name": "Spanish"},
    "it": {"flag": "it", "name": "Italian"},
    "fr": {"flag": "fr", "name": "French"},
    "zh": {"flag": "cn", "name": "Chinese"},
    "ja": {"flag": "jp", "name": "Japanese"},
    "de": {"flag": "de", "name": "German"},
    "pt": {"flag": "pt", "name": "Portuguese"},
    "pt_BR": {"flag": "br", "name": "Brazilian Portuguese"},
    "ru": {"flag": "ru", "name": "Russian"},
    "ko": {"flag": "kr", "name": "Korean"},
    "sk": {"flag": "sk", "name": "Slovak"},
    "sl": {"flag": "si", "name": "Slovenian"},
    "nl": {"flag": "nl", "name": "Dutch"},
}

SHOW_STACKTRACE = False

DEBUG = False

APP_NAME = "Superset"

#不填这个会导致报错如下
#ModuleNotFoundError: No module named 'MySQL_db'
SQLALCHEMY_TRACK_MODIFICATIONS = False

import logging

LOG_LEVEL = 'DEBUG'  # 设置日志级别为DEBUG可以获得最详细的日志信息
LOG_FILE = '/home/rust/superset/superset_logfile.log'  # 指定日志文件路径

logging.basicConfig(
    filename=LOG_FILE,
    level=LOG_LEVEL,
    format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s',
)

初始化Superset

  1. 初始化数据库
1
2
(superset) rust@hadoop01:~$ export FLASK_APP=superset
(superset) rust@hadoop01:~$ superset db upgrade
  1. 在元数据库中创建一个superset管理员用户
1
(superset) rust@hadoop01:~$ superset fab create-admin
  1. 初始化superset
1
(superset) rust@hadoop01:~$ superset init

启动Superset

1
 (superset) rust@hadoop01:~$gunicorn --workers 5 --timeout 120 --bind hadoop01:8787  "superset.app:create_app()" --daemon

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

登录Supperset

访问http://hadoop102:8787,并使用初始化Supperset中创建的管理员账户进行登录。

停止Superset

  1. 停止gunicorn进程
1
(superset) rust@hadoop01:~$ ps -ef | awk '/superset/ && !/awk/{print $2}' | xargs kill -9
  1. 退出superset虚拟环境
1
2
3
(superset) rust@hadoop01:~$ ps -ef |grep superst
#查到进程号后杀掉进程
(superset) rust@hadoop01:~$ kill -9 pid

Superset启停脚本

  1. 创建superset.sh文件
1
vim superset.sh

内容如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash

superset_status(){
    result=`ps -ef | awk '/gunicorn/ && !/awk/{print $2}' | wc -l`
    if [[ $result -eq 0 ]]; then
        return 0
    else
        return 1
    fi
}
superset_start(){
        source ~/.bashrc
        superset_status >/dev/null 2>&1
        if [[ $? -eq 0 ]]; then
            source ~/superset/bin/activate ; gunicorn --workers 5 --timeout 120 --bind hadoop01:8787 --daemon 'superset.app:create_app()'
        else
            echo "superset正在运行"
        fi

}

superset_stop(){
    superset_status >/dev/null 2>&1
    if [[ $? -eq 0 ]]; then
        echo "superset未在运行"
    else
        ps -ef | awk '/gunicorn/ && !/awk/{print $2}' | xargs kill -9
    fi
}


case $1 in
    start )
        echo "启动Superset"
        superset_start
    ;;
    stop )
        echo "停止Superset"
        superset_stop
    ;;
    restart )
        echo "重启Superset"
        superset_stop
        superset_start
    ;;
    status )
        superset_status >/dev/null 2>&1
        if [[ $? -eq 0 ]]; then
            echo "superset未在运行"
        else
            echo "superset正在运行"
        fi
esac
  1. 加执行权限
1
sudo chmod +x superset.sh
  1. 测试
1
2
3
4
#启动superset
superset.sh start
#停止superset
superset.sh stop
网站总访客数:Loading
网站总访问量:Loading
使用 Hugo 构建
主题 StackJimmy 设计