通过工厂组织查询的工具。
query-factor的Python项目详细描述
查询工厂
这个工具应该有助于将SQL查询组织到python项目中。
使用
您应该在yaml文件中分隔查询模板,如下例所示:
# template.yamldescription:|This is a simple query for demonstration purpose.variables:start_date:description:UTC datetime string to gather data from (inclusive)required:trueend_date:description:UTC datetime string to gather data to (exclusive)required:truecategory_id:description:Category id to filter on. If null, filter won't apply.required:falsedefault:nullmarket:description:Market scope (either 'pro' or 'part').required:falsedefault:partquery_template:|SELECT *FROM db.tableWHERE event_date >= {{ start_date }}AND event_date < {{ end_date }}AND market = {{ market }}{% if category_id %}AND category_id = {{ category_id }}{% endif %}LIMIT = 100;
然后启动工厂并运行一些查询:
^{pr2}$工厂提供有关模板的一些信息:
>>>set(factory.required_variables){'end_date','start_date'}>>>set(factory.optional_variables){'category_id'}>>>factory.describe("start_date")'UTC datetime string to gather data from (inclusive)'
以下是如何使用如上定义的工厂对查询进行变量化:
importpandasaspdconnection=connect_to_sql_query_engine()data_2020_02_01=pd.read_sql(factory(start_date="2020-02-01",end_date="2020-02-02"),con=connection)data_2020_02_02_filtered_on_categ1=pd.read_sql(factory(start_date="2020-02-02",end_date="2020-02-03",category_id="categ_1"),con=connection)
- 项目
标签: