-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathcreate_super_cube.py
134 lines (113 loc) · 4.9 KB
/
create_super_cube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""This is the demo script to show how to export data from Strategy One with
SuperCube. It is possible to create and publish single or multi-table SuperCube.
You can update super cube with different policies when adding table:
- add -> insert entirely new data
- update -> update existing data
- upsert -> simultaneously updates existing data and inserts new data
- replace -> truncates and replaces the data
You can set in how big increments data is transmitted to the server with the
`chunksize` parameter.
This script will not work without replacing parameters with real values.
Its basic goal is to present what can be done with this module and to ease
its usage.
"""
import pandas as pd
import datetime as dt
from mstrio.project_objects import (
SuperCubeAttribute,
SuperCubeAttributeForm,
SuperCubeFormExpression
)
from mstrio.project_objects.datasets import SuperCube
from mstrio.connection import get_connection
# Define a variable which can be later used in a script
PROJECT_NAME = $project_name # Insert project name here
conn = get_connection(workstationData, project_name=PROJECT_NAME)
# prepare Pandas DataFrames to add it into tables of super cube
stores = {"store_id": [1, 2, 3], "location": ["New York", "Seattle", "Los Angeles"]}
stores_df = pd.DataFrame(stores, columns=["store_id", "location"])
sales = {
"store_id": [1, 2, 3],
"category": ["TV", "Books", "Accessories"],
"sales": [400, 200, 100],
"sales_fmt": ["$400", "$200", "$100"]
}
sales_df = pd.DataFrame(sales, columns=["store_id", "category", "sales", "sales_fmt"])
dates = {
"dates": ['2021-11-15','2020-05-04','2018-01-26','2019-02-18','2021-12-10']
}
dates_df = pd.DataFrame(dates, columns=["dates"])
# If any of your columns should have type 'Date' please set it manually as below
dates_df["dates"] = pd.to_datetime(dates_df["dates"]).dt.date
# Define a variable which can be later used in a script
SUPER_CUBE_NAME = $super_cube_name # Insert name of created suber cube here
# Add tables to the super cube and create it. By default 'create()' will
# additionally upload data to the I-Server and publish it. You can manipulate it
# by setting parameters `auto_upload` and `auto_publish`
ds = SuperCube(connection=conn, name=SUPER_CUBE_NAME)
ds.add_table(name="Stores", data_frame=stores_df, update_policy="add")
ds.add_table(name="Sales", data_frame=sales_df, update_policy="add")
ds.add_table(name="Dates", data_frame=dates_df, update_policy="add")
ds.create()
# Add tables to super cube and map columns to attribute forms
ds = SuperCube(connection=conn, name=SUPER_CUBE_NAME)
ds.add_table(name="Stores", data_frame=stores_df, update_policy="add")
ds.add_table(name="Sales", data_frame=sales_df, update_policy="add")
attribute_form_mapping = SuperCubeAttribute(
name='store',
forms=[
SuperCubeAttributeForm(
category='ID',
expressions=[
SuperCubeFormExpression(
table='Stores',
column='store_id'
),
SuperCubeFormExpression(
table='Sales',
column='store_id'
)
]
),
SuperCubeAttributeForm(
category='DESC',
expressions=[
SuperCubeFormExpression(
table='Stores',
column='name'
)
]
)
]
)
ds.create(attribute_forms=[attribute_form_mapping])
# When using `SuperCube.add_table()`, Pandas data types are mapped to
# Strategy One data types. By default, numeric data is modeled as MSTR metrics
# and non-numeric as attributes. You can set manually which columns treat as
# attributes and which as metrics.
ds.add_table(name="Stores", data_frame=stores_df, update_policy="add", to_attribute=["store_id"])
ds.add_table(
name="Sales",
data_frame=sales_df,
update_policy="add",
to_attribute=["store_id"],
to_metric=["sales_fmt"]
)
# Define a variable which can be later used in a script
SUPER_CUBE_ID = $super_cube_id # insert ID of edited super cube here
# It is possible to update previously created super cubes what looks really
# similar to creation. You can use different update policies which are explained
# in the description of this script at the top. By default, `update()`
# is publishing data automatically, if you don't want to publish data,
# you have to set argument 'auto_publish` to False. It is also possible to set
# chunksize for the update.
ds = SuperCube(connection=conn, id=SUPER_CUBE_ID)
ds.add_table(name="Stores", data_frame=stores_df, update_policy="update")
ds.add_table(name="Sales", data_frame=sales_df, update_policy="upsert")
ds.update()
# Finally, it is possible to certify an existing super cube
ds.certify()
# Limitations
# Updating SuperCubes that were not created using the Strategy One REST API is
# not possible. This applies for example to Cubes created via Strategy One Web
# client.