Managing Views
This notebook shows how you can manage views with the ThanoSQL library. We will cover how to create views, get the list of stored views, and delete views. First, initialize a ThanoSQL client with your API token and engine URL.
from thanosql import ThanoSQL
client = ThanoSQL(
api_token="THANOSQL_API_TOKEN",
engine_url="THANOSQL_ENGINE_URL"
)
Listing Views
You can get the list of views in your workspace using the list() function. By default, views from all schemas will be shown, but you can change this behavior by specifying schema. You can also specify offset and limit (the default values are 0 and 100, respectively).
views = client.view.list()
for view in views:
print(view.name, view.table_schema)
By default, the output will only include the name and schema of the view. In order to include more information, namely the list of columns the view has and its definition, you have to enable the verbose option.
views = client.view.list(verbose=True)
for view in views:
print(view.columns, view.definition)
You can also show the details (name, schema, columns, definition) of a specific view using the get() function. Aside from the view name, you can also specify the schema the view belongs to. While this is not required for views in the default (public) schema, this is necessary for views in other schemas.
view = client.view.get(name="my_view_in_schema_1", schema="schema_1")
view
Creating Views
Currently, there is no direct API for creating views. However, it is possible to do through raw query using the query API.
query = "CREATE VIEW my_view_1 AS SELECT column_2, column_3 FROM my_table_1"
client.query.execute(query)
Deleting Views
You can also delete views from your workspace using the client. Similar to get, you do not need to specify a schema if the view is stored in the public schema. For views in other schemas, you have to pass in the schema name as well.
client.view.delete("my_view_1")
client.view.delete(name="my_view_in_schema_1", schema="schema_1")