zoukankan      html  css  js  c++  java
  • FLINK基础(138):DS流与表转换(4) Handling of (Insert-Only) Streams(3)createTemporaryView(FLINK1.13以上)

    NO.1 code

    DataStream can be registered directly as a view (possibly enriched with a schema).

    Views created from a DataStream can only be registered as temporary views. Due to their inline/anonymous nature, it is not possible to register them in a permanent catalog.

    The following code shows how to use createTemporaryView for different scenarios.

    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.streaming.api.datastream.DataStream;
    
    // create some DataStream
    DataStream<Tuple2<Long, String>> dataStream = env.fromElements(
        Tuple2.of(12L, "Alice"),
        Tuple2.of(0L, "Bob"));
    
    
    // === EXAMPLE 1 ===
    
    // register the DataStream as view "MyView" in the current session
    // all columns are derived automatically
    
    tableEnv.createTemporaryView("MyView", dataStream);
    
    tableEnv.from("MyView").printSchema();
    
    // prints:
    // (
    //  `f0` BIGINT NOT NULL,
    //  `f1` STRING
    // )
    
    
    // === EXAMPLE 2 ===
    
    // register the DataStream as view "MyView" in the current session,
    // provide a schema to adjust the columns similar to `fromDataStream`
    
    // in this example, the derived NOT NULL information has been removed
    
    tableEnv.createTemporaryView(
        "MyView",
        dataStream,
        Schema.newBuilder()
            .column("f0", "BIGINT")
            .column("f1", "STRING")
            .build());
    
    tableEnv.from("MyView").printSchema();
    
    // prints:
    // (
    //  `f0` BIGINT,
    //  `f1` STRING
    // )
    
    
    // === EXAMPLE 3 ===
    
    // use the Table API before creating the view if it is only about renaming columns
    
    tableEnv.createTemporaryView(
        "MyView",
        tableEnv.fromDataStream(dataStream).as("id", "name"));
    
    tableEnv.from("MyView").printSchema();
    
    // prints:
    // (
    //  `id` BIGINT NOT NULL,
    //  `name` STRING
    // )

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/15204055.html

  • 相关阅读:
    MySQL基础(3):进阶用法
    MySQL基础(2):DDL语言
    MySQL基础(1):基本语法
    Linux中配置ftp传输
    Eclipse插件安装的三种方法
    C++中string,wstring,CString的基本概念和用法
    程序员的谈判技巧
    转:C++编程隐蔽错误:error C2533: 构造函数不能有返回类型
    CMake入门以及学习笔记
    程序员的学习方法
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/15204055.html
Copyright © 2011-2022 走看看