本文簡要介紹 python 語言中pyflink.table.StreamTableEnvironment.from_data_stream
的用法。
用法:
from_data_stream(data_stream: pyflink.datastream.data_stream.DataStream, *fields_or_schema: Union[pyflink.table.expression.Expression, pyflink.table.schema.Schema]) → pyflink.table.table.Table
當fields_or_schema 是一個表達式序列時:
將給定的DataStream 轉換為具有指定字段名稱的表。
將原始字段映射到Table的字段有兩種模式:
按名稱引用輸入字段:
架構定義中的所有字段都由名稱引用(並且可能使用和別名 (as) 重命名。此外,我們可以使用任意名稱在任意位置定義 proctime 和 rowtime 屬性(結果架構中存在的除外)。在這種模式下, 字段可以重新排序和投影。此模式可用於任何輸入類型。
按位置引用輸入字段:
在這種模式下,字段被簡單地重命名。 Event-time 屬性可以替換其在輸入數據中的位置上的字段(如果它是正確的類型)或附加在末尾。 Proctime 屬性必須附加在末尾。僅當輸入類型具有已定義的字段順序(元組、案例類、行)並且沒有任何字段引用輸入類型的字段時,才能使用此模式。
當fields_or_schema 是模式時:
將給定的DataStream 轉換為表格。
Table 的列名和類型自動派生自 DataStream 的TypeInformation。如果最外層記錄的TypeInformation 是 CompositeType,它將在第一層被展平。複合嵌套字段將不可訪問。
由於DataStream API 本身不支持變更日誌處理,因此此方法在stream-to-table 轉換期間假定append-only/insert-only 語義。類 Row 的記錄必須說明 RowKind.INSERT 更改。
默認情況下,除非明確聲明,否則不會傳播流記錄的時間戳和水印。
此方法允許為結果表聲明一個模式。該聲明類似於 SQL 中的 {@code CREATE TABLE} DDL,並允許:
使用自定義 DataType 豐富或覆蓋自動派生列
重新排序列
在物理列旁邊添加計算列或元數據列
訪問流記錄的時間戳
聲明水印策略或傳播DataStream水印
可以聲明沒有物理/常規列的模式。在這種情況下,這些列將自動派生並隱式放置在模式聲明的開頭。
以下示例說明了常見的模式聲明及其語義:
例子:
=== EXAMPLE 1 === no physical columns defined, they will be derived automatically, e.g. BigDecimal becomes DECIMAL(38, 18) >>> Schema.new_builder() ... .column_by_expression("c1", "f1 + 42") ... .column_by_expression("c2", "f1 - 1") ... .build() equal to: CREATE TABLE (f0 STRING, f1 DECIMAL(38, 18), c1 AS f1 + 42, c2 AS f1 - 1) === EXAMPLE 2 === physical columns defined, input fields and columns will be mapped by name, columns are reordered and their data type overwritten, all columns must be defined to show up in the final table's schema >>> Schema.new_builder() ... .column("f1", "DECIMAL(10, 2)") ... .column_by_expression("c", "f1 - 1") ... .column("f0", "STRING") ... .build() equal to: CREATE TABLE (f1 DECIMAL(10, 2), c AS f1 - 1, f0 STRING) === EXAMPLE 3 === timestamp and watermarks can be added from the DataStream API, physical columns will be derived automatically >>> Schema.new_builder() ... .column_by_metadata("rowtime", "TIMESTAMP_LTZ(3)") ... .watermark("rowtime", "SOURCE_WATERMARK()") ... .build() equal to: CREATE TABLE ( f0 STRING, f1 DECIMAL(38, 18), rowtime TIMESTAMP(3) METADATA, WATERMARK FOR rowtime AS SOURCE_WATERMARK() )
注意
create_temporary_view 通過提供 Schema(案例 2.)是從 flink 1.14.0 添加的。
參數:
data_stream- 要轉換的數據流。
fields_or_schema- 將DataStream 的原始字段映射到表格的字段或最終表格的自定義模式的字段表達式。
返回:
轉換後的表。
版本 1.12.0 中的新函數。
相關用法
- Python pyflink StreamTableEnvironment.create用法及代碼示例
- Python pyflink StreamTableEnvironment.to_changelog_stream用法及代碼示例
- Python pyflink StreamExecutionEnvironment.set_restart_strategy用法及代碼示例
- Python pyflink StreamExecutionEnvironment.set_python_executable用法及代碼示例
- Python pyflink StreamExecutionEnvironment.enable_checkpointing用法及代碼示例
- Python pyflink StreamExecutionEnvironment.set_state_backend用法及代碼示例
- Python pyflink StreamExecutionEnvironment.add_python_archive用法及代碼示例
- Python pyflink StreamExecutionEnvironment.set_stream_time_characteristic用法及代碼示例
- Python pyflink StreamExecutionEnvironment.register_type用法及代碼示例
- Python pyflink StreamExecutionEnvironment.set_default_savepoint_directory用法及代碼示例
- Python pyflink StreamExecutionEnvironment.add_default_kryo_serializer用法及代碼示例
- Python pyflink StreamExecutionEnvironment.set_python_requirements用法及代碼示例
- Python pyflink StreamExecutionEnvironment.register_type_with_kryo_serializer用法及代碼示例
- Python pyflink StatementSet.add_insert用法及代碼示例
- Python pyflink Session用法及代碼示例
- Python pyflink Slide用法及代碼示例
- Python pyflink Schema.Builder.watermark用法及代碼示例
- Python pyflink Schema.Builder.column_by_expression用法及代碼示例
- Python pyflink SlidingProcessingTimeWindows用法及代碼示例
- Python pyflink SlidingEventTimeWindows用法及代碼示例
- Python pyflink Table.intersect_all用法及代碼示例
- Python pyflink GroupedTable.select用法及代碼示例
- Python pyflink Expression.to_date用法及代碼示例
- Python pyflink Table.fetch用法及代碼示例
- Python pyflink PulsarSourceBuilder用法及代碼示例
注:本文由純淨天空篩選整理自apache.org大神的英文原創作品 pyflink.table.StreamTableEnvironment.from_data_stream。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。