superset 官网简介链接
- 简介: https://superset.apache.ac.cn/docs/intro/
- 安装: https://superset.apache.ac.cn/docs/installation/kubernetes/
- 使用: https://superset.apache.ac.cn/docs/using-superset/creating-your-first-dashboard/
- API: https://superset.apache.org/docs/api/
为何选择 Superset?
Superset 是一个现代的数据探索和数据可视化平台。Superset 可以为许多团队取代或增强专有的商业智能工具。Superset 可以很好地与各种数据源集成。
Superset 提供
- 一个无代码界面,用于快速构建图表
- 一个功能强大的基于 Web 的SQL 编辑器,用于高级查询
- 一个轻量级语义层,用于快速定义自定义维度和指标
- 开箱即用,支持几乎任何 SQL数据库或数据引擎
- 各种美观的可视化效果,展示您的数据,从简单的条形图到地理空间可视化效果
- 轻量级、可配置的缓存层,帮助减轻数据库负载
- 高度可扩展的安全角色和身份验证选项
- 用于编程自定义的API
- 云原生架构,从头开始设计,以实现扩展
- 最重要的是开源免费够用
安装
bash
sudo apt-get install build-essential libssl-dev libffi-dev python-dev python-pip libsasl2-dev libldap2-dev default-libmysqlclient-dev
python3 -m venv superset_venv
source superset_venv/bin/activate
pip install apache-superset
superset db upgrade # 执行此命令后,会生成superset.db。位置:~/.superset/superset.db
mkdir superset_app
cd superset_app
vim superset_config.py # 可参考:https://github.com/apache/superset/blob/master/superset/config.py
---------------------- superset_config.py ---------------------------
ROW_LIMIT = 5000
SECRET_KEY = 'YOUR_OWN_RANDOM_GENERATED_SECRET_KEY'
SQLALCHEMY_DATABASE_URI = 'sqlite:root/.superset/superset.db?check_same_thread=false'
WTF_CSRF_ENABLED = True
WTF_CSRF_EXEMPT_LIST = []
WTF_CSRF_TIME_LIMIT = 60 * 60 * 24 * 365
MAPBOX_API_KEY = ''
FILTER_STATE_CACHE_CONFIG = {
'CACHE_TYPE': 'RedisCache',
'CACHE_DEFAULT_TIMEOUT': 86400,
'CACHE_KEY_PREFIX': 'superset_filter_cache',
'CACHE_REDIS_URL': 'redis://localhost:6379/0'
}
---------------------- end --------------------------------------------
# Create an admin user in your metadata database (use `admin` as username to be able to load the examples)
vim ~/.bashrc
添加:export FLASK_APP=superset
source ~/.bashrc
superset fab create-admin
# Load some data to play with
superset load_examples
# Create default roles and permissions
superset init
# To start a development web server on port 8088, use -p to bind to another port
superset run -host 0.0.0.0 -p 8088 --with-threads --reload --debugger
gunicorn -w 4 --timeout 120 --bind 0.0.0.0:8088 "superset.app:create_app()"
使用supervisor管理进程
bash
cd /etc/supervisor/conf.d
vim superset.conf
-------------------- start ----------------
[program:superset]
directory=/root/superset_app
command=/root/superset_venv/bin/gunicorn -w 4 --timeout 120 --bind 0.0.0.0:8088 "superset.app:create_app()"
user=root
autostart=true
autorestart=true
startsecs=5
redirect_stderr=true
stdout_logfile_maxbytes=50MB
stdout_logfile=/root/superset_app/superset.log
loglevel=info
[supervisord]
[supervisorctl]
-------------------- end ------------------
supervisorctl reread # 加载配置,不启动进程
supervisorctl update # 更新配置,启动新进程
访问 : http://10.10.25.113:8088
安装数据库连接依赖
bash
pip install psycopg2 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install clickhouse-connect -i https://pypi.tuna.tsinghua.edu.cn/simple
API使用
bash
https://superset.apache.org/docs/api/
http://10.10.25.113:8088/api/v1/chart # 返回所有图表信息
rows = data.get('result', [])
for row in rows:
slice_name = row.get('slice_name') # "slice_name": "各个省份锥形图"
id = row.get('id') # "id": 2,
viz_type = row.get('viz_type') # 图表类型 "viz_type": "pie"
将图表数据通过API,将数据返回给前段
如何获取csrf_token
http://10.10.25.113:8088/api/v1/security/csrf_token/
{
"result": "IjJiNjI0ODZkYmI3NTYxY2Q4MDdmMTg2NDkxODI0NTQ4OGJiOTBiNDki.ZrHMjw.FMTc8dovtd9qPOleDo9AKFNf6qA"
}
图标ID=1
http://10.10.25.113:8088/api/v1/chart/{图表ID}/data
{
"result": [
{
"cache_key": "81846ce79d7a5c4c9cb532f776cab21b",
"cached_dttm": null,
"cache_timeout": 86400,
"applied_template_filters": [],
"annotation_data": {},
"error": null,
"is_cached": null,
"query": "SELECT sheng AS sheng,\n source AS source,\n count(id) AS \"COUNT(id)\"\nFROM public.bicompanyinfo\nWHERE ((DATE(created_time) = '2024-08-04'))\nGROUP BY sheng,\n source\nORDER BY \"COUNT(id)\" DESC\nLIMIT 100;\n\n",
"status": "success",
"stacktrace": null,
"rowcount": 19,
"sql_rowcount": 19,
"from_dttm": null,
"to_dttm": null,
"label_map": {
"sheng": [
"sheng"
],
"source": [
"source"
],
"COUNT(id)": [
"COUNT(id)"
]
},
"colnames": [
"sheng",
"source",
"COUNT(id)"
],
"indexnames": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18
],
"coltypes": [
1,
1,
0
],
"data": [
{
"sheng": "江苏",
"source": "处置信息",
"COUNT(id)": 7
},
{
"sheng": "山东",
"source": "处置信息",
"COUNT(id)": 7
},
{
"sheng": "河北",
"source": "处置信息",
"COUNT(id)": 5
},
{
"sheng": "黑龙江",
"source": "处置信息",
"COUNT(id)": 4
},
{
"sheng": "陕西",
"source": "处置信息",
"COUNT(id)": 3
},
{
"sheng": "广西",
"source": "处置信息",
"COUNT(id)": 3
},
{
"sheng": "广东",
"source": "处置信息",
"COUNT(id)": 2
},
{
"sheng": "湖北",
"source": "处置信息",
"COUNT(id)": 2
},
{
"sheng": "辽宁",
"source": "处置信息",
"COUNT(id)": 2
},
{
"sheng": "安徽",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "直辖市",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "甘肃",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "海南",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "河南",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "江西",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "山西",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "四川",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "云南",
"source": "处置信息",
"COUNT(id)": 1
},
{
"sheng": "浙江",
"source": "处置信息",
"COUNT(id)": 1
}
],
"result_format": "json",
"applied_filters": [],
"rejected_filters": []
}
]
}