【问题标题】:Python Cubes OLAP Framework - how to work with joins?Python Cubes OLAP 框架 - 如何使用连接?
【发布时间】:2012-10-25 09:20:34
【问题描述】:

我正在尝试在一个非常简单的数据库上使用 python 的 olap 框架 cubes,但在连接表时遇到了一些问题。

我的架构如下所示:

Users table
ID | name

Products table
ID | name | price

Purchases table
ID | user_id | product_id | date

还有立方体模型:

{
    'dimensions': [
        {'name': 'user_id'},
        {'name': 'product_id'},
        {'name': 'date'},
    ],
    'cubes': [
        {
            'name': 'purchases',
            'dimensions': ['user_id', 'product_id', 'date'],
            'measures': ['price']
            'mappings': {
                'purchases.user_id': 'users.id',
                'purchases.product_id': 'products.id',
                'purchases.price': 'products.price'
            },
            'joins': [
                {
                    'master': 'purchases.user_id',
                    'detail': 'users.id'
                },
                {
                    'master': 'purchases.product_id',
                    'detail': 'products.id'
                }
            ]
        }
    ]
}

现在我想显示所有购买,显示产品名称、用户名称和购买日期。我似乎找不到办法做到这一点。文档有点稀缺。

谢谢

【问题讨论】:

  • OLAP 不是为连接而设计的。或许可以改用 SQL 数据库。
  • @AaronWatters Python 的 Cubes 框架提供了这个功能。它是一个 MySQL 后端。
  • 澄清一下:连接对最终用户是隐藏的,它们是逻辑到物理映射的一部分,并在逻辑模型中指定。此外,Cubes 有许多关系数据库后端,不仅是 MySQL - 所有后端SQLAlchemy 支持的。其中包括:PostgreSQL、Oracle、SQLite 等等。

标签: python database olap star-schema


【解决方案1】:

首先让我们稍微修正一下模型。在您的模式中,每个维度都有更多属性:id 和 name,您将来可能会获得更多详细信息。您可以通过将属性指定为列表来添加它们:"attriubtes": ["id", "name"]。另请注意,维度被命名为实体product 而不是键id_product。键id_product 只是product 维度的一个属性,name 或将来可能是category。维度反映分析师的观点。

暂时我们忽略了日期应该是一个特殊维度的事实,将日期视为单值键,例如一年,这里就不复杂了。

"dimensions": [
    {"name": "user", "attributes": ["id", "name"]},
    {"name": "product", "attributes": ["id", "name"]},
    {"name": "date"}
],

因为我们更改了维度的名称,所以我们必须在多维数据集的维度列表中更改它们:

"cubes": [
    {
        "name": "purchases",
        "dimensions": ["user", "product", "date"],
        ...

您的架构反映了经典的事务架构,而不是传统的数据仓库架构。在这种情况下,您必须像以前一样明确,并提及所有必要的映射。规则是:如果属性属于事实表(逻辑视图),那么key就是attribute,比如price,没有表说明。如果属性属于某个维度,例如product.id,则语法为dimension.attribute。映射字典的值是物理表和物理列。见more information about mappings。您的架构的映射如下所示:

"mappings": {
    "price": "products.price",
    "product.id": "products.id",
    "product.name": "products.name",
    "user.id": "users.id",
    "user.name": "users.name"
}

如果您的架构是,您将不必编写映射:

fact purchases
id | date | user_id | product_id | amount

dimension product
id | name | price

dimension user
id | name

在这种情况下,您只需要连接,因为所有维度属性都在它们各自的维度表中。请注意事实表中的amount,在您的情况下,由于您每次购买都没有购买产品的count,因此与product 中的price 相同。

这是您的模型的更新模型:

{
    "dimensions": [
        {"name": "user", "attributes": ["id", "name"]},
        {"name": "product", "attributes": ["id", "name"]},
        {"name": "date"}
    ],
    "cubes": [
        {
            "name": "purchases",
            "dimensions": ["user", "product", "date"],
            "measures": ["price"],
            "mappings": {
                "price": "products.price",
                "product.id": "products.id",
                "product.name": "products.name",
                "user.id": "users.id",
                "user.name": "users.name"
            },
            "joins": [
                {
                    "master": "purchases.user_id",
                    "detail": "users.id"
                },
                {
                    "master": "purchases.product_id",
                    "detail": "products.id"
                }
            ]
        }

    ]
}

您无需编写任何 Python 代码即可试用该模型,只需使用 slicer 命令即可。为此,您需要slicer.ini configuration file:

[server]
backend: sql
port: 5000
log_level: info
prettyprint: yes

[workspace]
url: sqlite:///data.sqlite

[model]
path: model.json

[workspace] 中的url 更改为指向您的数据库,并将[model] 中的path 更改为指向您的模型文件。现在你可以试试:

curl "http://localhost:5000/aggregate"

也尝试向下钻取:

curl "http://localhost:5000/aggregate?drilldown=product"

如果您需要任何进一步的帮助,请告诉我,我是 Cubes 的作者。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多