Views
Fenl expressions can be shared and re-used by creating a view. A view is a named expression. In subsequent Fenl expressions, the view’s name is synonymous with the view’s expression. Views are persisted in the Kaskada manager and are accessible to any query.
Once you’ve created a view, you can use the view’s name anywhere you could use the view’s expression - the only restriction placed on views is that they must be valid expressions. Views may reference other views.
Views are useful any time you need to share or re-use expressions:
-
Cleaning operations
-
Common business logic
-
ML feature definitions
Managing views
Creating a View
To create a view, we’ll start by describing the expression we’d like to name. In this case, we’re interested in some purchase statistics for each user. This definition depends on business logic and might require some iteration to get just right.
-
Python
-
CLI
from kaskada import view
from kaskada.api.session import LocalBuilder
session = LocalBuilder().build()
view.create_view(
view_name = "PurchaseStats",
expression = "{
time: Purchase.purchase_time,
entity: Purchase.customer_id,
max_amount: Purchase.amount | max(),
min_amount: Purchase.amount | min(),
}"
)
kaskada-cli view create PurchaseStats \
"{time: Purchase.purchase_time,entity: Purchase.customer_id,max_amount: Purchase.amount | max(),min_amount: Purchase.amount | min()}"
This creates a view names PurchaseStats
.
Idiomatic Kaskada
We like to use CamelCase for view names. This is the same naming convention as we use for tables, and helps to communicate that we’re referring to a persistent resource in the Kaskada system, as opposed to a temporary local value created as part of a query. |
If you’re using IPython/Jupyter, you can create a view from the contents of a %%fenl
block using the --var
argument
%%fenl --var purchase_stats
{
time: Purchase.purchase_time,
entity: Purchase.customer_id,
max_amount: Purchase.amount | max(),
min_amount: Purchase.amount | min(),
}
Adding --var purchase_stats
to the beginning of the
magic block causes the extension to assign the query result to the
variable purchase_stats
when the block is run.
We can use this variable to create a view using the Python client without re-typing the expression:
from kaskada import view
from kaskada.api.session import LocalBuilder
session = LocalBuilder().build()
view.create_view(
view_name = "PurchaseStats",
expression = purchase_stats.expression,
)
List Views
The list views method returns all views defined for your user. An optional search string can filter the response set.
Here is an example of listing views:
-
Python
-
CLI
from kaskada import view
from kaskada.api.session import LocalBuilder
session = LocalBuilder().build()
view.list_views()
kaskada-cli view list
Get View
You can get a view using its name:
-
Python
-
CLI
from kaskada import view
from kaskada.api.session import LocalBuilder
session = LocalBuilder().build()
view.get_view("PurchaseStats")
kaskada-cli view get PurchaseStats
Updating a View
Views are currently immutable. Updating a view requires deleting that view and then re-creating it.
Deleting a view
You can delete a view using its name:
-
Python
-
CLI
from kaskada import view
from kaskada.api.session import LocalBuilder
session = LocalBuilder().build()
view.delete_view("PurchaseStats")
kaskada-cli view delete PurchaseStats
A failed precondition error is returned when another view and/or
materialization references the view. To continue with the deletion of
the view we can either, delete the dependent resources manually with another delete operation, or, supply the force
flag to
delete the view forcefully. Forcefully deleting a view without
deleting its dependent resources may result in its dependent resources
functioning incorrectly.
-
Python
-
CLI
from kaskada import view
from kaskada.api.session import LocalBuilder
session = LocalBuilder().build()
view.delete_view("PurchaseStats", force = True)
kaskada-cli view delete PurchaseStats --force