CentOS 7 安装配置Superset教程

  • 一 .环境说明
  • 二. Superset安装
  • 2.1 安装 Python 环境
  • 2.1.1 安装 Miniconda
  • 2.1.2 创建 Python3.7 环境
  • 2.2 Superset 部署
  • 2.2.1 安装依赖
  • 2.2.2 安装 Superset
  • 2.2.3 启动 Supterset
  • 三 .设置启动脚本
  • 四 .对接 MySQL 数据源
  • 4.1 安装依赖
  • 4.2 数据源配置


一 .环境说明

本课程使用的服务器操作系统为 CentOS 7,Superset 对接的数据源为 MySQL 数据库。

二. Superset安装

Superset 官网地址:http://superset.apache.or

2.1 安装 Python 环境

Superset 是由 Python 语言编写的 Web 应用,要求 Python3.7 的环境。

2.1.1 安装 Miniconda

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

下载地址:https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

2)安装 Miniconda
(1)执行以下命令进行安装(在你安装包的目录),并按照提示操作,直到安装完成。

[atguigu@hadoop102 lib]$ bash Miniconda3-latest-Linux-x86_64.sh

(2)在安装过程中,出现以下提示时,可以指定安装路径

centos7的su_centos


(3)出现以下字样,即为安装完成

centos7的su_centos7的su_02


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

[atguigu@hadoop102 lib]$ source ~/.bashrc

4)取消激活 base 环境
Miniconda 安装完成后,每次打开终端都会激活其默认的 base 环境,我们可通过以下命
令,禁止激活默认 base 环境。

[atguigu@hadoop102 lib]$ conda config --set auto_activate_base false

2.1.2 创建 Python3.7 环境

1)配置 conda 国内镜像

(base) [atguigu@hadoop102 ~]$ conda config --add channels 
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
(base) [atguigu@hadoop102 ~]$ conda config --add channels 
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

2)创建 Python3.7 环境

(base) [atguigu@hadoop102 ~]$ conda create --name superset python=3.7

3)激活 superset 环境

(base) [atguigu@hadoop102 ~]$ conda activate superset

centos7的su_centos_03

4)退出当前环境

(superset) [atguigu@hadoop102 ~]$ conda deactivate

2.2 Superset 部署

2.2.1 安装依赖

安装 Superset 之前,需安装以下所需依赖

(superset) [atguigu@hadoop102 ~]$ sudo yum install -y gcc gcc-c++ libffi-devel python-devel python-pip python-wheel pythonsetuptools openssl-devel cyrus-sasl-devel openldap-devel

2.2.2 安装 Superset

1)安装(更新)setuptools 和 pip
(superset) [atguigu@hadoop102 ~]$ pip install apache-superset -i https://pypi.tuna.tsinghua.edu.cn/simple/

说明:pip 是 python 的包管理工具,可以和 centos 中的 yum 类比
2)安装 Supetset

(superset) [atguigu@hadoop102 ~]$ pip install apache-superset -i https://pypi.douban.com/simple/

说明:-i 的作用是指定镜像,这里选择国内镜像
注:如果遇到网络错误导致不能下载,可尝试更换镜像

(superset) [atguigu@hadoop102 ~]$ pip install apache-superset --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple

3)初始化 Supetset 数据库

(superset) [atguigu@hadoop102 ~]$ superset db upgrade

这里如果出现错误,可能是Flask和Werkzeug框架的版本不统一,可执行:

pip uninstall Flask
pip uninstall Werkzeug

pip install Flask==2.0.2
pip install Werkzeug==2.0.2

统一版本后再次尝试。

4)创建管理员用户

(superset) [atguigu@hadoop102 ~]$ export FLASK_APP=superset
(superset) [atguigu@hadoop102 ~]$ superset fab create-admin

说明:flask 是一个 python web 框架,Superset 使用的就是 flask
执行操作后设置用户名和密码。设置完成后记得初始化。
5)Superset 初始化

(superset) [atguigu@hadoop102 ~]$ superset init

2.2.3 启动 Supterset

1)安装 gunicorn

(superset) [atguigu@hadoop102 ~]$ pip install gunicorn -i https://pypi.douban.com/simple/

说明:gunicorn 是一个 Python Web Server,可以和 java 中的 TomCat 类比
2)启动 Superset
(1)确保当前 conda 环境为 superset
(2)启动

(superset) [atguigu@hadoop102 ~]$ gunicorn --workers 5 --timeout 120 --bind hadoop102:9870 "superset.app:create_app()" --daemon

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

http://hadoop102:9870

使用我们上面设置的账号和密码登录。
3)停止 superset
停掉 gunicorn 进程

(superset) [atguigu@hadoop102 ~]$ ps -ef | awk '/superset/ && !/awk/{print $2}' | xargs kill -9

退出 superset 环境

(superset) [atguigu@hadoop102 ~]$ conda deactivate

三 .设置启动脚本

1)再bin目录下创建 superset.sh 文件

[atguigu@hadoop102 bin]$ vim superset.sh

内容如下

#!/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
 conda activate superset ; gunicorn --workers 5 --timeout 120 -bind hadoop102: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

加执行权限

[atguigu@hadoop102 bin]$ chmod +x superset.sh

测试成功表示脚本没问题。

四 .对接 MySQL 数据源

4.1 安装依赖

(superset) [atguigu@hadoop102 ~]$ conda install mysqlclient

说明:对接不同的数据源,需安装不同的依赖,以下地址为官网说明

https://superset.apache.org/docs/databases/installing-database-driver

安装完成后重启superset

(superset) [atguigu@hadoop102 ~]$ superset.sh restart

4.2 数据源配置

1)Database 配置

Step1:点击 Data/Databases

centos7的su_centos_04


Step2:点击+DATABASE

.

centos7的su_linux_05


Step3:点击填写 Database 及 SQL Alchemy URI

注:SQL Alchemy URI 编写规范:

mysql://用户名:密码@主机名:端口号/数据库名称

此处填写:

mysql://root:000000@hadoop102:3306/gmall_report?charset=utf8

Step4:点击 Test Connection,出现“Connection looks good!”提示即表示连接成功

2)Table 配置

Step1:点击 Data/Datasets

centos7的su_linux_06


然后点击右上角的添加

Step2:配置 Table

centos7的su_hadoop_07


将数据库的中的表添加进去。

后续就可以进行绘制了。