用于olap处理的轻量级分析引擎
babbage的Python项目详细描述
巴贝奇分析引擎
babbage
是olap风格数据库的轻量级实现
PostgreSQL查询工具。给定数据库模式和逻辑模型
在数据中,它可以用来对
数据-以编程方式或通过Web API。
它深受Cubes的启发,但是 没有雄心勃勃的目标,即没有预先计算总量,或 多个存储后端。
babbage
并不特定于政府财政,而且可以很容易地用于再生,这是一个通过api提供德国国家统计数据的项目。api通过解释用户生成的建模元数据(度量和维度)来工作。
安装和测试
babbage
通常作为pypi依赖项包含,或通过
pip
:
$ pip install babbage
有兴趣参与该软件包的人应该查看
然后使用提供的Makefile
安装
库(需要安装virtualenv
):
$ git clone https://github.com/openspending/babbage.git $ cd babbage $ make install $ pip install tox $ exportBABBAGE_TEST_DB=postgresql://postgres@localhost:5432/postgres $ make test
用法
< P> ^ {CD1>}用于查询一组现有数据库表,使用 抽象的逻辑模型来查询它们。逻辑模型的示例可以是 在tests/fixtures/models/cra.json
和指定
模型在babbage/schema/model.json
中可用。babbage
的中心单元是一个Cube
,即使用提供的模型元数据构造查询的OLAP cube。
针对数据库表。此外,应用程序支持管理
通过一个CubeManager
同时有多个多维数据集,可以是
子类以启用应用程序定义多维数据集的特定方式以及
它们的元数据被存储。
此外,babbage
还包括一个烧瓶蓝图,可用于暴露
通过http的标准api。这个api由javascriptbabbage.ui
使用
包,它非常紧密地模拟了多维数据集和openspending http
原料药。
编程用法
让我们假设您有一个现有的采购数据数据库表,
想在python shell中使用babbage
查询它。会话可能看起来
像这样:
importjsonfromsqlalchemyimportcreate_enginefrombabbage.cubeimportCubefrombabbage.modelimportMeasureengine=create_engine('postgresql://localhost/procurement')model=json.load(open('procurement_model.json','r'))cube=Cube(engine,'procurement',model)facts=cube.facts(page_size=5)# There are 17201 rows in the table:assertfacts['total_fact_count']==17201# There's a field called 'total_value':assert'total_value'infacts['fields']# We can get metadata about it:concept=cube.model['total_value']assertisinstance(concept,Measure)assertconcept.label=='Total Value'# And there's some actual data:assertlen(facts['data'])==5fact_0=facts['data'][0]assert'total_value'infact_0# For dimensions, we can get all the distinct values:members=cube.members('supplier',cut='year:2015',page_size=500)assertlen(members['data'])<=500assertmembers['total_member_count']# And, finally, we can aggregate by specific dimensions:aggregate=cube.aggregate(aggregates='total_value.sum',drilldowns='supplier|authority'cut='year:2015|authority.country:GB',page_size=500)# This translates to: # Aggregate the procurement data by summing up the 'total_value'# for each unique pair of values in the 'supplier' and 'authority'# dimensions, and filter for only those entries where the 'year'# dimensions key attribute is '2015' and the 'authority' dimensions# 'country' attribute is 'GB'. Return the first 500 results.assertaggregate['total_cell_count']assertlen(aggregate['cells'])<=500aggregate_0=aggregate['cells'][0]assert'total_value.sum'inaggregate_0# Note that these attribute names are made up for this example, they# should be reflected from the model:assert'supplier.code'inaggregate_0assert'supplier.label'inaggregate_0assert'authority.code'inaggregate_0assert'authority.label'inaggregate_0
使用http api
用于babbage
的http api是一个简单的烧瓶Blueprint,用于公开与
上面列出的多维数据集函数。将其放入现有的烧瓶中
应用程序,您需要创建一个CubeManager
,然后
如下配置api:
fromflaskimportFlaskfromsqlalchemyimportcreate_enginefrombabbage.managerimportJSONCubeManagerfrombabbage.apiimportconfigure_apiapp=Flask('demo')engine=models_directory='models/'manager=JSONCubeManager(engine,models_directory)blueprint=configure_api(app,manager)app.register_blueprint(blueprint,url_prefix='/api/babbage')app.run()
当然,您可以定义自己的CubeManager
,例如
您希望从数据库中检索模型元数据。
启用后,api将公开许多json(p)端点
相对于给定的url_prefix
:
/
,返回系统状态和版本。/cubes
,返回可用多维数据集的列表(仅名称)。/cubes/<name>/model
,返回给定的 立方体(即测量、尺寸、聚集等)/cubes/<name>/facts
用于返回 非聚合形式的多维数据集。支持筛选器(cut
),a 返回的fields
和sort
(field_name:direction
)的集合, 以及page
和page_size
。/cubes/<name>/members
用于返回 给定尺寸的值,例如 采购数据集。支持筛选器(cut
)、a和asort
(field_name:direction
),以及page
和page_size
。/cubes/<name>/aggregate
是生成 数据的聚合视图。支持指定aggregates
要包括一组过滤器,要聚合的drilldowns
(cut
),a和asort
(field_name:direction
),以及 作为page
和page_size
。