首先让我们修一下模型.在您的架构中,每个维度都有更多属性:id和name,您最终可能会在将来获得更多详细信息.您可以通过将属性指定为列表来添加它们:“attriubtes”:[“id”,“name”].另请注意,维度被命名为实体产品而不是关键id_product.密钥id_product只是产品维度的一个属性,名称或将来可能是类别. Dimension反映了分析师的观点.
目前,我们忽略了这样一个事实:日期应该是一个特殊的维度,并将日期视为单值键,例如一年,而不是在这里使事情变得复杂.
"dimensions": [
{"name": "user", "attributes": ["id", "name"]},
{"name": "product", "attributes": ["id", "name"]},
{"name": "date"}
],
因为我们更改了维度的名称,所以我们必须在多维数据集的维度列表中更改它们:
"cubes": [
{
"name": "purchases",
"dimensions": ["user", "product", "date"],
...
您的架构反映了传统的事务架构,而不是传统的数据仓库架构.在这种情况下,您必须像过去一样明确,并提及所有必要的映射.规则是:如果属性属于事实表(逻辑视图),那么键只是属性,例如价格,没有表格规范.如果属性属于某个维度,例如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
在这种情况下,您只需要连接,因为所有维度属性都在各自的维度表中.请注意事实表中的金额,在您的情况下,由于您没有每次购买的购买产品数量,因此与产品价格相同.
以下是您模型的更新模型:
{
"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]中的路径以指向您的模型文件.现在你可以尝试:
curl "http://localhost:5000/aggregate"
同时尝试向下钻取:
curl "http://localhost:5000/aggregate?drilldown=product"
如果您需要任何进一步的帮助,请告诉我,我是Cubes的作者.