可视化报表

  • 一、Superset入门
  • 1.1 Superset概述
  • 1.2 Superset应用场景
  • 二、Superset安装及使用
  • 2.1 安装Python环境
  • 2.1.1 安装Miniconda
  • 2.2 Superset部署
  • 2.2.1 安装依赖
  • 2.2.2 安装Superset
  • 2.2.3 启动Supterset
  • 2.2.4 superset启停脚本
  • 三、Superset使用
  • 3.1 对接MySQL数据源
  • 3.1.1 安装依赖
  • 3.1.2 重启Superset
  • 3.1.3 数据源配置


一、Superset入门

1.1 Superset概述

Apache Superset是一个开源的、现代的、轻量级BI分析工具,能够对接多种数据源、拥有丰富的图表展示形式、支持自定义仪表盘,且拥有友好的用户界面,十分易用。

1.2 Superset应用场景

由于Superset能够对接常用的大数据分析工具,如Hive、Kylin、Druid等,且支持自定义仪表盘,故可作为数仓的可视化工具。

二、Superset安装及使用

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

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)执行以下命令进行安装,并按照提示操作,直到安装完成。

superset 能对接MongoDB吗_教育电商


superset 能对接MongoDB吗_ico_02


superset 能对接MongoDB吗_python_03


superset 能对接MongoDB吗_python_04


superset 能对接MongoDB吗_ico_05


superset 能对接MongoDB吗_ico_06


superset 能对接MongoDB吗_ico_07


创建python3.7环境

superset 能对接MongoDB吗_ico_08


激活

superset 能对接MongoDB吗_教育电商_09


superset 能对接MongoDB吗_big data_10

2.2 Superset部署

2.2.1 安装依赖

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

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

确保当前conda环境为superset

superset 能对接MongoDB吗_教育电商_11

2.2.2 安装Superset

1)安装(更新)setuptools和pip

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

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

superset 能对接MongoDB吗_数据分析_12

2)安装Supetset

pip install apache-superset -i https://pypi.douban.com/simple/

superset 能对接MongoDB吗_big data_13

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

superset db upgrade

报错

cannot import name 'soft_unicode' from 'markupsafe' (/opt/module/miniconda3/envs/superset/lib/python3.7/site-packages/markupsafe/__init__.py)

解决

superset 能对接MongoDB吗_ico_14


superset 能对接MongoDB吗_数据分析_15


superset 能对接MongoDB吗_ico_16


superset 能对接MongoDB吗_ico_17

python -m pip install markupsafe==2.0.1

superset 能对接MongoDB吗_big data_18


superset 能对接MongoDB吗_数据分析_19

superset 能对接MongoDB吗_python_20


superset 能对接MongoDB吗_python_21

4)创建管理员用户

export FLASK_APP=superset
superset fab create-admin

说明:flask是一个python web框架,Superset使用的就是flask

superset 能对接MongoDB吗_python_22

5)Superset初始化

superset init

superset 能对接MongoDB吗_ico_23

superset 能对接MongoDB吗_python_24

2.2.3 启动Supterset

1)安装gunicorn

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

superset 能对接MongoDB吗_教育电商_25

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

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

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

(3)登录Superset

访问http://hadoop102:8787,并使用创建的管理员账号进行登录。

superset 能对接MongoDB吗_教育电商_26


superset 能对接MongoDB吗_big data_27

3)停止superset
停掉gunicorn进程

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

退出superset环境

conda deactivate
2.2.4 superset启停脚本

1)创建superset.sh文件

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

2)加执行权限

chmod +x superset.sh

3)测试
启动superset
停止superset

superset.sh start
superset.sh stop

superset 能对接MongoDB吗_ico_28

三、Superset使用

3.1 对接MySQL数据源

3.1.1 安装依赖
conda install mysqlclient

superset 能对接MongoDB吗_python_29

3.1.2 重启Superset
superset.sh restart
3.1.3 数据源配置

1)Database配置

Step1:点击Data/Databases

Step2:点击+DATABASE

Step3:点击填写Database及SQL Alchemy URI

注:SQL Alchemy URI编写规范:mysql://用户名:密码@主机名:端口号/数据库名称


此处填写:

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

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

Step5:点击ADD

superset 能对接MongoDB吗_ico_30

2)Table配置

Step1:点击Data/Datasets

Step2:点击Data/ Datasets

Step3:配置Table

superset 能对接MongoDB吗_ico_31