Flink Table&SQL API demo

本文通过实例演示了Apache Flink中表API和SQL API的使用,展示了如何将数据流转换为表,执行选择操作,并将结果转换回数据流进行打印。代码示例涵盖了从集合创建数据流,注册表,以及使用两种不同的API进行数据处理的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package com.flink.test;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.typeutils.TupleTypeInfo;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.Types;
import org.apache.flink.table.api.java.StreamTableEnvironment;
import java.util.Arrays;


public class Table1 {
    public static void  main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tEnv = StreamTableEnvironment.getTableEnvironment(env);

        DataStream<Tuple2<Integer, String>> stream1 = env.fromCollection(Arrays.asList(
                new Tuple2<>(1, "hello"),
                new Tuple2<>(2, "hello")
        ));

        //将数据类型映射到表模式
        Table table1 = tEnv.fromDataStream(stream1,  "count1, word");
        // 注册表
        tEnv.registerTable("table1", table1);

        /**
         *  table api
         */
        Table tapiResult = tEnv.scan("table1").select("count1");
        // toRetractStream 始终可以使用此模式。它使用布尔标志对插入和删除更改进行编码。
        DataStream<Tuple2<Boolean, Integer>> dsRow = tEnv.toRetractStream(tapiResult, Integer.class);
        dsRow.print();

        /**
         *  sql api
         **/
        TupleTypeInfo<Tuple2<Integer, String>> tupleType = new TupleTypeInfo<>(
                Types.INT(),
                Types.STRING());

        Table sqlResult  = tEnv.sqlQuery("SELECT count1,word FROM table1");

        // toAppendStream只追加,以前发出的结果永远不会更新。
        DataStream<Tuple2<Integer, String>> dsRow1 = tEnv.toAppendStream(sqlResult, tupleType);
        dsRow1.print();

        env.execute();
    }
}

执行代码:

1> 2
2> 1
2> (1,hello)
1> (2,hello)

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>FlinkDataHandle</groupId>
    <artifactId>FlinkDataHandle</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>kafka-flink-hbase</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hbase.version>1.2.0</hbase.version>
        <flink.version>1.7.2</flink.version>
    </properties>

    <repositories>
        <!-- 		指定该项目可以从哪些地方下载依赖包  -->
        <repository>
            <id>aliyun</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
        <repository>
            <id>jboss</id>
            <url>http://repository.jboss.org/nexus/content/groups/public</url>
        </repository>
    </repositories>

    <dependencies>
            <!-- This dependency provides the implementation of compiler "jdt": -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-java_${scala.compat.version}</artifactId>
            <version>${flink.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-table_${scala.compat.version}</artifactId>
            <version>${flink.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-scala_${scala.compat.version}</artifactId>
            <version>${flink.version}</version>
        </dependency>
       
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0</version>
        </dependency>
    
    </dependencies>
</project>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千里风雪

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值