Superset
# Superset概述
Apache Superset是一个开源的、现代的、轻量级BI分析工具,能够对接多种数据源、拥有丰富的图表展示形式、支持自定义仪表盘,且拥有友好的用户界面,十分易用。
# Superset应用场景
由于Superset能够对接常用的大数据分析工具,如Hive、Kylin、Druid等,且支持自定义仪表盘,故可作为数仓的可视化工具。
# Superset安装及使用
Superset官网地址:http://superset.apache.org/
# 安装Python环境
Superset是由Python语言编写的Web应用,要求Python3.7的环境。
# 安装Miniconda
conda是一个开源的包、环境管理器,可以用于在同一个机器上安装不同Python版本的软件包及其依赖,并能够在不同的Python环境之间切换,Anaconda包括Conda、Python以及一大堆安装好的工具包,比如:numpy、pandas等,Miniconda包括Conda、Python。
此处,我们不需要如此多的工具包,故选择MiniConda。
下载Miniconda(Python3版本)
下载地址:https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
安装Miniconda
执行以下命令进行安装,并按照提示操作,直到安装完成。
bash Miniconda3-latest-Linux-x86_64.sh
1在安装过程中,出现以下提示时,可以指定安装路径
出现以下字样,即为安装完成
加载环境变量配置文件,使之生效
source ~/.bashrc
1取消激活base环境
Miniconda安装完成后,每次打开终端都会激活其默认的base环境,我们可通过以下命令,禁止激活默认base环境。
conda config --set auto_activate_base false
1
# 创建Python3.7环境
配置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 (base) [atguigu@hadoop102 ~]$ conda config --set show_channel_urls yes
1
2
3创建Python3.7环境
(base) [atguigu@hadoop102 ~]$ conda create --name superset python=3.7
1**说明:**conda环境管理常用命令
**创建环境:**conda create -n env_name
**查看所有环境:**conda info --envs
**删除一个环境:**conda remove -n env_name --all
激活superset环境
(base) [atguigu@hadoop102 ~]$ conda activate superset
1激活后效果如下图所示
说明:退出当前环境
conda deactivate
1执行python命令查看python版本
# Superset部署
# 安装依赖
安装Superset之前,需安装以下所需依赖
(superset) [atguigu@hadoop102 ~]$ sudo yum install -y gcc gcc-c++ libffi-devel python-devel python-pip python-wheel python-setuptools openssl-devel cyrus-sasl-devel openldap-devel
# 安装Superset
安装(更新)setuptools和pip
(superset) [atguigu@hadoop102 ~]$ pip install --upgrade setuptools pip -i https://pypi.douban.com/simple/
1
**说明:**pip是python的包管理工具,可以和centos中的yum类比
安装Supetset
(superset) [atguigu@hadoop102 ~]$ pip install apache-superset -i https://pypi.douban.com/simple/
1说明:-i的作用是指定镜像,这里选择国内镜像
**注:**如果遇到网络错误导致不能下载,可尝试更换镜像
(superset) [atguigu@hadoop102 ~]$ pip install apache-superset --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple
1初始化Supetset数据库
(superset) [atguigu@hadoop102 ~]$ superset db upgrade
1创建管理员用户
(superset) [atguigu@hadoop102 ~]$ export FLASK_APP=superset (superset) [atguigu@hadoop102 ~]$ superset fab create-admin
1
2**说明:**flask是一个python web框架,Superset使用的就是flask
Superset初始化
(superset) [atguigu@hadoop102 ~]$ superset init
1
# 启动Supterset
安装gunicorn
(superset) [atguigu@hadoop102 ~]$ pip install gunicorn -i https://pypi.douban.com/simple/
1**说明:**gunicorn是一个Python Web Server,可以和java中的TomCat类比
启动Superset
确保当前conda环境为superset,及下图所示
启动
(superset) [atguigu@hadoop102 ~]$ gunicorn --workers 5 --timeout 120 --bind ha01:8787 "superset.app:create_app()" --daemon
1说明:
--workers:指定进程个数
--timeout:worker进程超时时间,超时会自动重启
--bind:绑定本机地址,即为Superset访问地址
--daemon:后台运行
登录Superset
登录Superset
访问http://ha01:8787,并使用2.2.2节中第4步创建的管理员账号进行登录
# superset启停脚本
创建superset.sh文件
vim superset.sh
1#!/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 ha01: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
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添加执行权限
测试
superset.sh start superset.sh stop
1
2
3
# 对接MySQL数据源
# 安装依赖
(superset) [atguigu@hadoop102 ~]$ conda install mysqlclient
说明:对接不同的数据源,需安装不同的依赖,以下地址为官网说明
https://superset.apache.org/docs/databases/installing-database-drivers (opens new window)
# 重启Superset
# 数据源配置
Database配置
**Step1:**点击Data/Databases
Step2:点击+DATABASE
Step3:点击填写Database及SQL Alchemy URI
注:SQL Alchemy URI编写规范:mysql://用户名:密码@主机名:端口号/数据库名称
此处填写:
mysql://root:000000@ha01:3306/gmall_report?charset=utf8
Step4:点击 Connect
Table配置
Step1:点击Data/Datasets
Step2: Dataset
Step3:配置Table
# 创建图表
点击Charts/+CHART
选则数据源及图表类型
选择何使的图表类型
创建图表
按照说明配置图表
点击“Run Query”
如配置无误,可出现以下图标
命名该图表,并保存至仪表盘
# 编辑仪表盘
打开仪表盘,点击编辑按钮
调整图表大小以及图表盘布局
点击下图中箭头,可调整仪表盘自动刷新时间