前言

其实大数据图表展示的这类平台有很多,Superset是其中之一,最近有个需求对各类图表展示的开发较多,索性将工作量交给这个平台。

介绍

Superset的中文翻译是快船,而Superset其实是一个自助式数据分析工具,它的主要目标是简化我们的数据探索分析操作,它的强大之处在于整个过程一气呵成,几乎不用片刻的等待。

部署

docker方式(推荐)

docker pull amancevice/caravel
docker run --name caravel -d -p 8088:8088 amancevice/caravel
docker exec -it caravel demo
#系统会提示设置管理员

具体效果如下

[root@localhost ~]# docker exec -it ac94ec4d9469 demo
Username [admin]: admin
User first name [admin]: admin
User last name [user]: a
Email [admin@fab.org]: a
Password: 
Repeat for confirmation:
******此处省略几百行

centos系统

yum makecache
sudo easy_install -i http://pypi.douban.com/simple/ pip
sudo easy_install pip
sudo yum install gcc libffi-devel python-devel python-pip python-wheel openssl-devel libsasl2-devel openldap-devel
#安装Superset 
pip install superset 

#创建管理员用户名和密码 
fabmanager create-admin --app superset 

#初始化Superset 
superset db upgrade 

#装载初始化样例数据 
superset load_examples 

#创建默认角色和权限 
superset init 

#启动Superset 
superset runserver 

数据库依赖

数据库命令链接方式
MySQLpip install mysqlclientmysql://
Postgrespip install psycopg2postgresql+psycopg2://
Prestopip install pyhivepresto://
Oraclepip install cx_Oracleoracle://
sqlitesqlite://
Redshiftpip install sqlalchemy-redshiftredshift+psycopg2://
MSSQLpip install pymssqlmssql://
Impalapip install impylaimpala://
SparkSQLpip install pyhivejdbc+hive://

改造(docker环境)

因为默认是有权限控制的,即使分享出去的图表也需要登录用户才能看到,那如何做到不登陆用户也能查看图表呢,于是改造之路就开始了。

方法一:改源码

刚开始没找到方法,不得不从从源码入手解开权限,
https://github.com/apache/incubator-superset/blob/master/superset/views/core.py这个文件
/superset/views/core.py文件922行删掉 @has_access_api注解

@log_this
    @has_access_api
    @expose("/explore_json/<datasource_type>/<datasource_id>/")
    def explore_json(self, datasource_type, datasource_id):
        try:
            viz_obj = self.get_viz(
                datasource_type=datasource_type,

1006行删掉 @has_access_api注解

 @log_this
    @has_access
    @expose("/explore/<datasource_type>/<datasource_id>/")
    def explore(self, datasource_type, datasource_id):
        form_data = self.get_form_data()

在1017行加入

viz_obj.datasource.("all_datasource_access")

增加库的访问权限,可能还有些地方要改,该方法改了源码,侵入性强,不推荐。

方法二:改配置(推荐)

/superset/config.py第127行

# ---------------------------------------------------
# Roles config
# ---------------------------------------------------
# Grant public role the same set of permissions as for the GAMMA role.
# This is useful if one wants to enable anonymous users to view
# dashboards. Explicit grant on specific datasets is still required.
PUBLIC_ROLE_LIKE_GAMMA = False

PUBLIC_ROLE_LIKE_GAMMA改为True,
注释意思
授予公共角色与GAMMA角色相同的权限集。
如果想让匿名用户查看,可以设置这里
在仪表盘对特定数据集的授权显示,也在这里设置。

加入数据库权限这里加入所有数据库权限

Public  [can explore on Superset, all database access on all_database_access, can explore json on Superset]

其中
can explore on Superset为导出图表
can explore json on Superset为导出图表json
all database access on all_database_access访问所有数据库权限,也可以设置单个

iframe嵌入


选择某个切片,点右上角

<iframe
  width="600"
  height="400"
  seamless
  frameBorder="0"
  scrolling="no"
  src="http://10.10.100.18:32771/superset/explore/table/2/?form_data=%7B%22datasource%22%3A%222__table%22%2C%22viz_type%22%3A%22bubble%22%2C%22slice_id%22%3A9%2C%22granularity_sqla%22%3A%22year%22%2C%22time_grain_sqla%22%3A%22Time+Column%22%2C%22since%22%3A%222011-01-01%22%2C%22until%22%3A%222011-01-02%22%2C%22series%22%3A%22region%22%2C%22entity%22%3A%22country_name%22%2C%22size%22%3A%22sum__SP_POP_TOTL%22%2C%22limit%22%3A0%2C%22show_legend%22%3Atrue%2C%22max_bubble_size%22%3A%2250%22%2C%22x%22%3A%22sum__SP_RUR_TOTL_ZS%22%2C%22x_axis_format%22%3A%22.3s%22%2C%22x_axis_label%22%3A%22%22%2C%22x_log_scale%22%3Afalse%2C%22y%22%3A%22sum__SP_DYN_LE00_IN%22%2C%22y_axis_format%22%3A%22.3s%22%2C%22y_axis_label%22%3A%22%22%2C%22y_log_scale%22%3Afalse%2C%22where%22%3A%22%22%2C%22having%22%3A%22%22%2C%22filters%22%3A%5B%7B%22col%22%3A%22country_code%22%2C%22op%22%3A%22not+in%22%2C%22val%22%3A%5B%22TCA%22%2C%22MNP%22%2C%22DMA%22%2C%22MHL%22%2C%22MCO%22%2C%22SXM%22%2C%22CYM%22%2C%22TUV%22%2C%22IMY%22%2C%22KNA%22%2C%22ASM%22%2C%22ADO%22%2C%22AMA%22%2C%22PLW%22%5D%7D%5D%7D&standalone=true&height=400"
>
</iframe>

将iframe放入任意html内即可打开。

更多样式


总结

借助superset可以帮助我们可视化进行数据库分析,支持导出导入sql语句,支持各种数据库,在图表展现方面是个不错的框架。

觉得好的话,记得关注我哦!
掘金:
https://juejin.im/user/57cd55218ac247006459c40c
Github:
https://github.com/qq273681448

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐