APPLICATION
Syntax
Description
By using an APPLICATION, you can create a set of DSQL statements (including DDL and Query statements) and run them as a unit of work with all-or-nothing effect:
In case of DDL statements, either all of them succeed or none depending on whether there is a failure in processing statements. This means the metastore will be updated, only if all statements in the Application succeed.
In case of Query statements, all queries will be run in a single job. This helps achieving better efficiency and resource utilization at runtime.
The order of statements in an application matters.
Supported Statements
Currently, below statement types are supported in applications:
USE (
USE database
,USE schema
,USE store
)CREATE STREAM AS SELECT (virtual and non-virtual)
CREATE CHANGELOG AS SELECT (virtual and non-virtual)
Benefits of Application
Applications help users achieve better efficiency and overall cost reduction in two ways:
What Happens during an Application Failure
If a failure happens when processing one of the DDL or Query statements (if any) in an application (for example, due to a parse error), then the whole application fails. This means none of the entities will be created and no query will start.
If a runtime failure happens when the application job is running (for example, due to an authentication error to access a store), then all queries in the application will fail; However all the relation(s) which were created at the beginning of application via DDL statements (if any) will remain.
Virtual Relation
Here is the syntax to define a virtual relation:
Virtual relations are for defining intermediate computation results which are used in one or more subsequent queries in the same Application. They help simplify the computation logic and do some common computation among queries only once and use it multiple times, at no extra cost.
Examples
For the following examples, assume a Stream named pageviews
and has been created using the below CREATE STREAM statement:
Application with multiple statements
Application with multiple related statements
The example Application below has 5 statements. New streams are defined and some are used in other queries in the same Application. For example, the INSERT INTO
statement is reading data from pv_copy,
which is defined using a CREATE STREAM AS SELECT statement and writes into pageviews2
which is defined with another CSAS in the same Application. Note that the order of Statements matter in Application. Therefore, statements defining pv_copy
and pageviews2
should appear before the INSERT INTO
statement, that is using them, in the Application body. Moreover, the users
Stream is defined via a CREATE STREAM statement and is used in the JOIN query and last CSAS statement. Again, data records are read once from pageviews
and users
Entities and are used in the queries that are referring to them in their FROM clause. All the queries in the Application run within a single job.
Application with virtual relations
In the example Application below, we assume the pageviews
Stream is already defined in DeltaStream and we have a topic, named users
in our Store. In this Application, we first create a Changelog on the users
topic to track changes in users' information. Given that we are interested in pageviews done by 3 specific users, we can create a virtual Stream: virtual.public.v1
to filter records for those users from pageviews
and use them in subsequent queries. We first find pageviews done by these 3 users every 30 seconds using a Tumble window function on virtual.public.v1
and write the results into a new topic visit_freq
. Then we create a new virtual Stream: virtual.public.v2
using a temporal join between virtual.public.v1
and users_log
Changelog to extend each record with its user's information. We then use virtual.public.v2
records in two subsequent queries: One to find the popular_pages
visited by female users among our users of interest and a second query to find pages visited by different users in a given duration and enrich the results by latest information about the visiting users.
Note that although we have 5 CAS statements in this Application, only 3 new topics will be created in the configured Store: visit_freq
, popular_pages
and cross_region_pages
. Virtual relations' records are not persisted in any topic and are only fed to subsequent queries which are referring them.
Last updated