用于OLAP处理的轻量级分析引擎
项目描述
Babbage 分析引擎
babbage
是 PostgreSQL 中一个轻量级的 OLAP 风格数据库查询工具的实现。给定数据库模式和数据的逻辑模型,它可以用来对数据进行分析查询 - 通过编程或通过 Web API 完成。
它主要受到 Cubes 的启发,但目标不那么宏伟,即没有预计算聚合,也没有多个存储后端。
babbage
并非仅限于政府财政,可以很容易地用于 ReGENESIS 项目,该项目通过 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
$ export BABBAGE_TEST_DB=postgresql://postgres@localhost:5432/postgres
$ make test
用法
babbage
用于查询一组现有的数据库表,使用抽象的逻辑模型来查询它们。逻辑模型的一个示例可以在 tests/fixtures/models/cra.json
中找到,而指定模型的 JSON 架构可以在 babbage/schema/model.json
中找到。
babbage
的核心单位是 Cube
,即使用提供的模型元数据来构建查询的数据库表的 OLAP 立方体。此外,应用程序支持同时管理多个立方体,通过 CubeManager
实现,它可以被继承以允许定义特定于应用程序的立方体以及它们的元数据存储方式。
此外,babbage
包含一个 Flask 蓝图,可以用来通过 HTTP 暴露标准 API。此 API 由 JavaScript babbage.ui
包消费,并且它与 Cubes 和 OpenSpending HTTP API 非常相似。
编程用法
假设您有一个现有的采购数据数据库表,并且想使用 babbage
在 Python shell 中查询它。会话可能如下所示
import json
from sqlalchemy import create_engine
from babbage.cube import Cube
from babbage.model import Measure
engine = 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:
assert facts['total_fact_count'] == 17201
# There's a field called 'total_value':
assert 'total_value' in facts['fields']
# We can get metadata about it:
concept = cube.model['total_value']
assert isinstance(concept, Measure)
assert concept.label == 'Total Value'
# And there's some actual data:
assert len(facts['data']) == 5
fact_0 = facts['data'][0]
assert 'total_value' in fact_0
# For dimensions, we can get all the distinct values:
members = cube.members('supplier', cut='year:2015', page_size=500)
assert len(members['data']) <= 500
assert members['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.
assert aggregate['total_cell_count']
assert len(aggregate['cells']) <= 500
aggregate_0 = aggregate['cells'][0]
assert 'total_value.sum' in aggregate_0
# Note that these attribute names are made up for this example, they
# should be reflected from the model:
assert 'supplier.code' in aggregate_0
assert 'supplier.label' in aggregate_0
assert 'authority.code' in aggregate_0
assert 'authority.label' in aggregate_0
使用 HTTP API
babbage
的 HTTP API 是一个简单的 Flask 蓝图,用于暴露与上述列出的立方体函数相对应的一小套调用。要将它包含到现有的 Flask 应用程序中,您需要创建一个 CubeManager
,然后像这样配置 API
from flask import Flask
from sqlalchemy import create_engine
from babbage.manager import JSONCubeManager
from babbage.api import configure_api
app = 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 将暴露相对于给定 url_prefix
的一系列 JSON(P) 端点
/
返回系统状态和版本。/cubes
返回可用立方体的列表(仅名称)。/cubes/<name>/model
返回给定立方体的完整元数据(即指标、维度、聚合等)。/cubes/<name>/facts
用于以非聚合形式返回立方体的单个条目。支持过滤器(cut
)、要返回的fields
集合和sort
(field_name:direction
),以及page
和page_size
。/cubes/<name>/members
用于返回给定维度的不同值集,例如在采购数据集中提到的所有供应商。支持过滤器(cut
)、和sort
(field_name:direction
),以及page
和page_size
。/cubes/<name>/aggregate
是生成数据聚合视图的主要端点。支持指定要包含的aggregates
、要按其聚合的drilldowns
、一组过滤器(cut
)、和sort
(field_name:direction
),以及page
和page_size
。
项目详情
下载文件
下载适用于您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。