Search code examples
pythonolapcubes

How to create OLAP cube from multiple stores in databrewery cubes


I'm currently trying to wrap my head around OLAP cubes.

For learning, I am using the cubes framework.

I'm currently stuck at making a cube that depends on dimensions from two different stores.

I've tried to use the fact that all defined dimensions are public by default to achieve this.

Below is some anonymized code showing how I'm trying to solve it:

workspace = Workspace()
workspace.register_store("store1", "sql", schema="schema1", url="postgresql://<URL>")
workspace.register_store("store2", "sql", schema="schema2", url="postgresql://<URL>")
workspace.import_model("model1.json", store="store1")
workspace.import_model("model2.json", store="store2")
workspace.import_model("cube_with_dimensions_from_model1_and_model2.json")
browser = workspace.browser("cube_with_dimensions_from_model1_and_model2")

I get the error

cubes.errors.NoSuchDimensionError: Dimension 'dimesion_from_model2' not found

This obviously means that it can't find the dimensions imported in model1 and model2.

Can anyone point me in the right direction on how to solve this?

PS: If there are more sample projects than those found here I would be very grateful to know.


Solution

  • Cubes currently creates a separate namespace for each store, if not specified otherwise. In your example, every model ends up in the namespace of the store (that is store1 and store2). Default (global) namespace remains empty, therefore the Workspace can't find your dimension in another store.

    Try one of the following options:

    A. use default namespace

    workspace.import_model("model1.json", "store1", namespace="default")
    workspace.import_model("model2.json", "store2", namespace="default")
    

    B. Separate common dimensions

    Put shared dimensions into model_common.json (or rather common.cubesmodel directory) and import it separately without any store or namespace specified (the dimensions will end-up in the default/global namespace):

    workspace.import_model("common.cubesmodel")
    workspace.import_model("model1.json", "store1")
    workspace.import_model("model2.json", "store2")