pyflink.table.table_environment.TableEnvironment.create_temporary_view#
- TableEnvironment.create_temporary_view(view_path: str, table_or_data_stream: Union[pyflink.table.table.Table, pyflink.datastream.data_stream.DataStream], *fields_or_schema: Union[str, pyflink.table.expression.Expression, pyflink.table.schema.Schema])[source]#
When table_or_data_stream is a
Table
:Registers a
Table
API object as a temporary view similar to SQL temporary views.Temporary objects can shadow permanent ones. If a permanent object in a given path exists, it will be inaccessible in the current session. To make the permanent object available again you can drop the corresponding temporary object.
When table_or_data_stream is a
DataStream
:2.1 When fields_or_schema is a str or a sequence of
Expression
:Creates a view from the given {@link DataStream} in a given path with specified field names. Registered views can be referenced in SQL queries.
1. Reference input fields by name: All fields in the schema definition are referenced by name (and possibly renamed using an alias (as). Moreover, we can define proctime and rowtime attributes at arbitrary positions using arbitrary names (except those that exist in the result schema). In this mode, fields can be reordered and projected out. This mode can be used for any input type, including POJOs.
Example:
>>> stream = ... # reorder the fields, rename the original 'f0' field to 'name' and add # event-time attribute named 'rowtime' # use str >>> table_env.create_temporary_view( ... "cat.db.myTable", ... stream, ... "f1, rowtime.rowtime, f0 as 'name'") # or use a sequence of expression >>> table_env.create_temporary_view( ... "cat.db.myTable", ... stream, ... col("f1"), ... col("rowtime").rowtime, ... col("f0").alias('name'))
2. Reference input fields by position: In this mode, fields are simply renamed. Event-time attributes can replace the field on their position in the input data (if it is of correct type) or be appended at the end. Proctime attributes must be appended at the end. This mode can only be used if the input type has a defined field order (tuple, case class, Row) and none of the {@code fields} references a field of the input type.
Example:
>>> stream = ... # rename the original fields to 'a' and 'b' and extract the internally attached # timestamp into an event-time attribute named 'rowtime' # use str >>> table_env.create_temporary_view( ... "cat.db.myTable", stream, "a, b, rowtime.rowtime") # or use a sequence of expressions >>> table_env.create_temporary_view( ... "cat.db.myTable", ... stream, ... col("a"), ... col("b"), ... col("rowtime").rowtime)
Temporary objects can shadow permanent ones. If a permanent object in a given path exists, it will be inaccessible in the current session. To make the permanent object available again you can drop the corresponding temporary object.
2.2 When fields_or_schema is a
Schema
:Creates a view from the given {@link DataStream} in a given path. Registered views can be referenced in SQL queries.
See
from_data_stream()
for more information on how aDataStream
is translated into a table.Temporary objects can shadow permanent ones. If a permanent object in a given path exists, it will be inaccessible in the current session. To make the permanent object available again you can drop the corresponding temporary object.
Note
create_temporary_view by providing a Schema (case 2.) was added from flink 1.14.0.
- Parameters
view_path – The path under which the view will be registered. See also the
TableEnvironment
class description for the format of the path.table_or_data_stream – The Table or DataStream out of which to create the view.
fields_or_schema – The fields expressions(str) to map original fields of the DataStream to the fields of the View or the customized schema for the final table.
New in version 1.10.0.