关键字:caravel、olap、kylin、数据可视化
经过我和好友Rocky一段时间的修改和调试,现已将初版的Caravel For Kylin上传至Github,供大家使用,如有发现任何的问题,可通过本博客或github联系我们。
GitHub地址:
https://github.com/rocky1001/pykylin/tree/caravel-kylin
https://github.com/rocky1001/caravel/tree/caravel-kylin
基于Caravel 0.8.9
关于Caravel:
http://airbnb.io/caravel/
http://lxw1234.com/archives/2016/06/681.htm
关于PyKylin:
https://github.com/wxiang7/pykylin (感谢作者@Wu Xiang)
修改说明
PyKylin:
修复where及having条件中的中文问题。
支持kylin中多表关联查询(实现方式很粗糙,但功能基本实现);
优化kylin多表查询时候时间序列相关的几个图表生成的SQL。
Caravel:
修复kylin中不支持的部分语法,比如使用timestamp关键字作为时间字段的别名。
安装与启动
Caravel:
按照官网的安装文档即可 http://airbnb.io/caravel/installation.html
Pykylin:
https://github.com/rocky1001/pykylin/tree/caravel-kylin
强烈推荐使用Python3,避免中文问题。
启动Caravel:
nohup gunicorn -w 16 –timeout 60 -b 0.0.0.0:8080 caravel:app >> /tmp/caravel.log 2>&1 &
注意:不建议使用官网给出的启动命令,caravel runserver -d
避免因查询页面关闭造成的Caravel Server挂掉。
使用说明
对于普通单表的使用,请参考:
http://lxw1234.com/archives/2016/06/681.htm
配置Kylin多表查询
为了支持Kylin中多表查询,一般是一个事实表关联多张维度表,并获取衍生维度,我们通过在Caravel Table中添加自定义字段,并对该字段定义一定规则的表达式(字段串常量),提交到PyKylin之后,PyKylin解析该字符串常量,转换成与维度表关联的SQL查询并返回结果。
这里做法有些粗糙,但功能基本实现,你可以参考该思路做进一步的优化和修改。
这里以事实表AD_REPORT2为例,其中有维度ID字段AD_ID,在Kylin中构建Cube时候,通过INNER JOIN维度表AD_DIM来获取维度名称AD_NAME。
在Caravel的AD_REPORT2中添加字段ad_name,
该字段表达式为字符串常量:
‘$|INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)|b.__ad_name|$’
字符串以$开头和结尾,以|分隔。
INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)
定义了维度表、JOIN类型、ON条件,该字符串会直接添加到原始SQL中,作为JOIN子句;
在维度表中的字段前面加上__为了和事实表中的字段区分而不用考虑表的别名。
b.__ad_name
定义了使用该字段作为最终的字段取值。
当选择ad_name作为维度查询时,Caravel提交给PyKylin的SQL语句为:
SELECT '$|INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)|b.__ad_name|$' AS ad_name, SUM(imp_pv) AS sum__imp_pv FROM liuxiaowen.AD_REPORT2 WHERE pt >= '2015-06-13' AND pt <= '2016-06-13' GROUP BY '$|INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)|b.__ad_name|$' ORDER BY SUM(imp_pv) DESC LIMIT 50
PyKylin经过转换后提交给Kylin的SQL语句为:
SELECT b.__ad_name as ad_name, SUM(imp_pv) AS sum__imp_pv FROM liuxiaowen.ad_report2 inner join (SELECT ad_id AS __ad_id,ad_name AS __ad_name FROM liuxiaowen.ad_dim) AS b ON (ad_id = __ad_id) WHERE pt >= '2015-06-13' AND pt <= '2016-06-13' GROUP BY b.__ad_name ORDER BY SUM(imp_pv) DESC
对于时间序列类的图表,Caravel提交给PyKylin的SQL语句为:
SELECT '$|INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)|b.__ad_name|$' AS ad_name, pt AS _timestamp, SUM(imp_pv) AS sum__imp_pv FROM liuxiaowen.AD_REPORT2 JOIN ( SELECT '$|INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)|b.__ad_name|$' AS __ad_name FROM liuxiaowen.AD_REPORT2 WHERE pt >= '2015-06-13' AND pt <= '2016-06-13' GROUP BY '$|INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)|b.__ad_name|$' ORDER BY SUM(imp_pv) DESC LIMIT 50 ) AS anon_1 ON '$|INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)|b.__ad_name|$' = __ad_name WHERE pt >= '2015-06-13' AND pt <= '2016-06-13' GROUP BY '$|INNER JOIN (select ad_id as __ad_id,ad_name as __ad_name from LIUXIAOWEN.AD_DIM) as b ON (ad_id = __ad_id)|b.__ad_name|$', pt ORDER BY SUM(imp_pv) DESC LIMIT 50000
PyKylin经过优化后的SQL语句为:
SELECT b.__ad_name AS ad_name, pt AS _timestamp, SUM(imp_pv) AS sum__imp_pv FROM liuxiaowen.ad_report2 inner join (SELECT ad_id AS __ad_id,ad_name AS __ad_name FROM liuxiaowen.ad_dim) AS b ON (ad_id = __ad_id) WHERE pt >= '2015-06-13' AND pt <= '2016-06-13' GROUP by b.__ad_name, pt ORDER by sum(imp_pv) desc limit 50
该配置方法对于关联一张维度表获取多个字段,以及关联多张维度表获取多个维度字段同样适用,只需要在Caravel Table中添加多个字段,表达式的写法一样即可。
另外,该修改只针对数据源类型为kylin,对于Caravel使用其他数据源不受影响。
Enjoy it!
20160812更新:
很多网友在按照上面的方法(基于源码离线安装)安装之后,都遇到了js无法正常加载,页面无法正常显示的问题,该问题是由于node/npm环境未正确安装而导致的。
解决方法请参考官方说明 https://github.com/airbnb/caravel/blob/master/CONTRIBUTING.md#setting-up-the-node–npm-javascript-environment
或者 http://www.cnblogs.com/kevin19900306/p/5701401.html
如果觉得本博客对您有帮助,请 赞助作者 。