您好,登錄后才能下訂單哦!
此次實踐過程全屬個人學習,我選擇了在window下安裝Superset,并進行嵌入后臺系統實踐。對此進行實踐過程總結,實踐成果分享給大家,供大家參考,如果你有更好的想法,歡迎留言交流。
建議安裝Python 3.4 以上版本。Python 2.7 版本在windows 上存在各種編碼問題。https://www.python.org/downloads/release/python-350/
下載Windows x86-64 executable installer 。直接使用exe的安裝包即可,安裝過程中選中增加到環境變量。
檢查:CMD下 分別運行python -V 和 pip-V。如果找不到命令,則需要添加python的安裝目錄到path環境變量下。
此步驟可選,直接安裝的話跳到第四步。因為Superset需要安裝的組件較多,最好是使用virtualenv獨立一套python環境。
在開發Python應用程序的時候,系統安裝的Python3只有一個版本。所有第三方的包都會被pip安裝到Python3的site-packages目錄下。
如果我們要同時開發多個應用程序,那這些應用程序都會共用一個Python,就是安裝在系統的Python 3。如果應用A需要jinja 2.7,而應用B需要jinja 2.6怎么辦?
這種情況下,每個應用可能需要各自擁有一套“獨立”的Python運行環境。virtualenv就是用來為一個應用創建一套“隔離”的Python運行環境。
安裝命令:
pip install virtualenv
2.3 使用virtualenv
先在D盤建立d:\pythonVir 目錄。
然后激活:
cd d:\pythonVir virtualenv env//等待初始化完成... //激活: env\Scripts\activate
激活之后的界面如下圖,注意在命令行輸入的左側有(env)標記,這樣我們的后續操作都會在env中生效,不會影響整體的pyhton環境。
Superset中依賴的一些庫需要使用microsoft visual c++ 2010編譯。
根據說明應該是也可以安裝 Visual C++ 2015 Build Tools:
http://landinghub.visualstudio.com/visual-cpp-build-tools
這里是個大坑, 我之前直接安裝superset一直安裝不成功,報錯(sasl.h 找不到)。
解決辦法是:通過 http://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl 下載對應的版本
比如咱們安裝的python 是3.6版本,系統是64位,就下載sasl-0.2.1-cp36-cp36m-win_amd64.whl。
另外,安裝過程中出現“ Failed building wheel for xxx”的解決辦法如下:
出現原因:缺失相應的whl文件。
解決辦法:下載并安裝對應的whl文件。
例如,出現“ Failed building wheel for python_geohash”則下載相應python版本的python_geohash文件。
我用的是Python3.6版本,則找到python_geohash-0.8.5-cp36-cp36m-win_amd64.whl文件進行下載即可。
安裝方法:
pip install F:\python_geohash-0.8.5-cp36-cp36m-win32.whl
1)前置環境準備完畢后,開始安裝superset.
pip install superset
執行成功界面:
2)創建管理員賬號:
fabmanager create-admin --app superset
執行過程界面如下:
3)初始化數據庫 (windows下,先進入到 Python安裝目錄(或者virtualEnv的虛擬目錄)下,lib\site-packages\superset\bin下)
執行命令:
python superset db upgrade
4)加載例子(后續操作都需要在lib\site-packages\superset\bin下)
python superset load_examples
5)初始化角色和權限
python superset init
6)啟動服務,端口 8088, 使用 -p 更改端口號。
python superset runserver -d
Superset默認使用sqllite。支持以下數據庫:
pip install mysqlclient | mysql:// | |
Postgres | pip install psycopg2 | postgresql+psycopg2:// |
Presto | pip install pyhive | presto:// |
Oracle | pip install cx_Oracle | oracle:// |
sqlite | 默認有了 | sqlite:// |
Redshift | pip install sqlalchemy-redshift | postgresql+psycopg2:// |
MSSQL | pip install pymssql | mssql:// |
Impala | pip install impyla | impala:// |
SparkSQL | pip install pyhive | jdbc+hive:// |
Greenplum | pip install psycopg2 | postgresql+psycopg2:// |
Athena | pip install "PyAthenaJDBC>1.0.9" | awsathena+jdbc:// |
Vertica | pip install sqlalchemy-vertica-python | vertica+vertica_python:// |
ClickHouse | pip install sqlalchemy-clickhouse | clickhouse:// |
使用pip安裝好數據庫后,就可以在Web界面中,配置相關數據源了。
數據庫的連接字符串格式參見:
http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#database-urls
登錄superset后,我們就可以配置自己本地數據源了,進行數據查詢以及展示。
修改superset中的config.py配置文件,將PUBLIC_ROLE_LIKE_GAMMA改為True。
注釋意思:
授予公共角色與GAMMA角色相同的權限集。
如果想讓匿名用戶查看,可以設置這里,在儀表盤對特定數據集的授權顯示,也在這里設置。
避免iframe跨站訪問問題。
其中:
? can explore on Superset為導出圖表
? can explore json on Superset為導出圖表json
? all database access on all_database_access訪問所有數據庫權限,也可以設置單個
<iframe width="600" height="400" seamless frameBorder="0" scrolling="no" src="http://127.0.0.1:8088/superset/explore/?form_data=%7B%22datasource%22%3A%223__table%22%2C%22viz_type%22%3A%22line%22%2C%22slice_id%22%3A63%2C%22granularity_sqla%22%3A%22ds%22%2C%22time_grain_sqla%22%3Anull%2C%22since%22%3A%22100+years+ago%22%2C%22until%22%3A%22now%22%2C%22metrics%22%3A%5B%7B%22aggregate%22%3A%22SUM%22%2C%22column%22%3A%7B%22column_name%22%3A%22num_california%22%2C%22expression%22%3A%22CASE+WHEN+state+%3D+%27CA%27+THEN+num+ELSE+0+END%22%7D%2C%22expressionType%22%3A%22SIMPLE%22%2C%22label%22%3A%22SUM%28num_california%29%22%7D%5D%2C%22adhoc_filters%22%3Anull%2C%22groupby%22%3A%5B%22name%22%5D%2C%22limit%22%3A%2210%22%2C%22timeseries_limit_metric%22%3A%7B%22aggregate%22%3A%22SUM%22%2C%22column%22%3A%7B%22column_name%22%3A%22num_california%22%2C%22expression%22%3A%22CASE+WHEN+state+%3D+%27CA%27+THEN+num+ELSE+0+END%22%7D%2C%22expressionType%22%3A%22SIMPLE%22%2C%22label%22%3A%22SUM%28num_california%29%22%7D%2C%22order_desc%22%3Atrue%2C%22contribution%22%3Afalse%2C%22row_limit%22%3A50000%2C%22color_scheme%22%3A%22bnbColors%22%2C%22show_brush%22%3A%22auto%22%2C%22show_legend%22%3Atrue%2C%22rich_tooltip%22%3Atrue%2C%22show_markers%22%3Afalse%2C%22line_interpolation%22%3A%22linear%22%2C%22x_axis_label%22%3A%22%22%2C%22bottom_margin%22%3A%22auto%22%2C%22x_ticks_layout%22%3A%22auto%22%2C%22x_axis_format%22%3A%22smart_date%22%2C%22x_axis_showminmax%22%3Afalse%2C%22y_axis_label%22%3A%22%22%2C%22left_margin%22%3A%22auto%22%2C%22y_axis_showminmax%22%3Afalse%2C%22y_log_scale%22%3Afalse%2C%22y_axis_format%22%3A%22.3s%22%2C%22y_axis_bounds%22%3A%5Bnull%2Cnull%5D%2C%22rolling_type%22%3A%22None%22%2C%22time_compare%22%3A%5B%5D%2C%22num_period_compare%22%3A%22%22%2C%22period_ratio_type%22%3A%22growth%22%2C%22resample_how%22%3Anull%2C%22resample_rule%22%3Anull%2C%22resample_fillmethod%22%3Anull%2C%22annotation_layers%22%3A%5B%5D%2C%22compare_lag%22%3A%2210%22%2C%22compare_suffix%22%3A%22o10Y%22%2C%22markup_type%22%3A%22markdown%22%2C%22metric%22%3A%22sum__num%22%2C%22where%22%3A%22%22%2C%22url_params%22%3A%7B%7D%7D&standalone=true&height=400" > </iframe>
效果如下:
為什么需要重定向呢?這里主要是為了后臺應用隱藏superset的圖表鏈接,防止被掃描到后,惡意使用;只要在后臺應用重新寫一個具有權限控制的請求鏈接,重新定向到superset的圖表鏈接,這樣就能防止數據泄露出去。
后臺代碼:
那么,對于鏈接地址:/chart/getDemoDashboardUrl,在后臺就可以進行權限管理。
以上已經完全可以把superset中的圖表嵌入到后臺應用系統中了,但是怎么能夠實現參數傳遞呢?現在,我在這里把實現過程整理出來,跟著試驗樣例看它怎么實現的。
研究一下superset圖表提供出去的鏈接地址,就可以發現,已json作為參數傳遞的。如下:
form_data={"datasource":"3__table","viz_type":"line","slice_id":63,"granularity_sqla":"ds","time_grain_sqla":null,"since":"100 years ago","until":"now","metrics":[{"aggregate":"SUM","column":{"column_name":"num_california","expression":"CASE WHEN state = 'CA' THEN num ELSE 0 END"},"expressionType":"SIMPLE","label":"SUM(num_california)"}],"adhoc_filters":[{"expressionType":"SIMPLE","subject":"gender","operator":"==","comparator":"boy","clause":"WHERE","sqlExpression":null,"fromFormData":true,"filterOptionName":"filter_gtzm93u9ocq_9sy5vd5ocfg"},{"expressionType":"SIMPLE","subject":"name","operator":"LIKE","comparator":"Aaron","clause":"WHERE","sqlExpression":null,"fromFormData":true,"filterOptionName":"filter_6cgdixdoh4_5wrgyuorwoa"}],"groupby":["name"],"limit":"10","timeseries_limit_metric":{"aggregate":"SUM","column":{"column_name":"num_california","expression":"CASE WHEN state = 'CA' THEN num ELSE 0 END"},"expressionType":"SIMPLE","label":"SUM(num_california)"},"order_desc":true,"contribution":false,"row_limit":50000,"color_scheme":"bnbColors","show_brush":"auto","show_legend":true,"rich_tooltip":true,"show_markers":false,"line_interpolation":"linear","x_axis_label":"","bottom_margin":"auto","x_ticks_layout":"auto","x_axis_format":"smart_date","x_axis_showminmax":false,"y_axis_label":"","left_margin":"auto","y_axis_showminmax":false,"y_log_scale":false,"y_axis_format":".3s","y_axis_bounds":[null,null],"rolling_type":"None","time_compare":[],"num_period_compare":"","period_ratio_type":"growth","resample_how":null,"resample_rule":null,"resample_fillmethod":null,"annotation_layers":[],"compare_lag":"10","compare_suffix":"o10Y","markup_type":"markdown","metric":"sum__num","where":"","url_params":{}}
其中,標注×××的就是過濾條件的參數配置,于是提取出來在代碼中做相應的修改:
修改后界面如下:
輸入條件,姓名:Amy,性別選擇:girl,點擊查詢如下:
9. 結束語
以上實踐為預研superset可視化工具的過程整理,編寫的樣例都很簡陋。如果實際應用到項目中去,按照上訴原理,重新設計一套可拓展,易使用的架構,將其打磨成可配置化的產品工具。這里先留下伏筆,暫不闡述,如果你有好的想法,歡迎留言交流。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。