Flink SQL

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.typeutils.RowTypeInfo;
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.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

public class table {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment fbEnv = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment fbTableEnv = StreamTableEnvironment.create(fbEnv);
        DataStream<String> ds = fbEnv.fromElements("100,100","200,100");
        DataStream<Tuple2<String,String>> ds1 = ds.map(new MapFunction<String, Tuple2<String,String>>() {
            @Override
            public Tuple2<String, String> map(String s) throws Exception {
                String[] args = s.split(",");
                return new Tuple2<String,String>(args[0],args[1]);
            }
        });

        //流转表
        Table table = fbTableEnv.fromDataStream(ds1,"id,name");
        //注册表
        fbTableEnv.registerTable("mytable", table);
        //执行查询
        Table table2 = fbTableEnv.sqlQuery("select * from mytable");
        //表转流
        DataStream<Row> ds3 = fbTableEnv.toAppendStream(table2,new RowTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO));

        ds3.map(new MapFunction<Row, Tuple2<String,String>>() {
            @Override
            public Tuple2<String, String> map(Row row) throws Exception {
                String id = (String)row.getField(0);
                String name = (String)row.getField(1);
                return new Tuple2<String,String>(id,name);
            }
        }).print();

        fbEnv.execute();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

tom_fans

谢谢打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值