layout: posttitle: 通过Python来操作kylin数据
subtitle: kylinpy工具库使用
date: 2020-04-01
author: NSX
header-img: img/post-bg-ios9-web.jpg
catalog: true
tags:
- 技术
- 教程
- Python

通过Python来操作kylin数据

  1. 安装依赖的包(py2/py3都支持)
  1. pip install kylinpy
  2. pip install sqlalchemy
  3. pip install --upgrade kylinpy

kylinpy工具库包含两个可使用原件. 想要了解更多关于此工具库信息请点击Github仓库.

  • Apache Kylin 命令行工具
  • Apache Kylin SQLAchemy方言
  1. 示例代码
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. import sqlalchemy as sa
  4. import kylinpy
  5. import pymysql
  6. # SQLAlchemy 实例
  7. def kylin_query1(conn_str, query_sql):
  8. kylin_engine = sa.create_engine(conn_str)
  9. print(kylin_engine.table_names())
  10. results = kylin_engine.execute(query_sql)
  11. print([e for e in results])
  12. # kylinpy 实例
  13. def kylin_query1(query_sql):
  14. kylin = kylinpy.KylinCluster(host=ip, username="ADMIN", password="", project="")
  15. # print(kylin.projects())
  16. results = kylin.query(query_sql)
  17. print(results["results"])
  18. if __name__ == "__main__":
  19. conn_str = "kylin://<username>:<password>@<ip>:<port>/<project>?version=<v1|v2>&prefix=</kylin/api>"
  20. query_sql = "select userid, datetime, count(*) c from soda_report group by userid, datetime"
  21. kylin_query1(conn_str, query_sql)
  22. kylin_query2(query_sql)

参考

通过Python来操作kylin

Kylin Python 客户端工具库 - kylinpy