FlinkSql(二)API使用-sink

0.前言

表的输出,是通过将数据写入 TableSink 来实现的。TableSink 是一个通用接口,可以 支持不同的文件格式、存储数据库和消息队列。

具体实现,输出表最直接的方法,就是通过 Table.insertInto() 方法将一个 Table 写入 注册过的 TableSink 中。

 1.FileSystem

// 注册输出表
tableEnv.connect(
 new FileSystem().path("…\\resources\\out.txt")
) // 定义到文件系统的连接
 .withFormat(new Csv()) // 定义格式化方法,Csv 格式
 .withSchema(new Schema()
 .field("id", DataTypes.STRING())
 .field("temp", DataTypes.DOUBLE())
) // 定义表结构
 .createTemporaryTable("outputTable") // 创建临时表
resultSqlTable.insertInto("outputTable")

其实对于sink来说结构都是tableEnv.connect(new sink).withFormant().withSchema().createTemporaryTable()

2.Kafka

// 输出到 kafka
tableEnv.connect(
 new Kafka()
 .version("0.11")
 .topic("sinkTest")
 .property("zookeeper.connect", "hdp-1:2181")
 .property("bootstrap.servers", "hdp-1:9092") 
 )
 .withFormat( new Csv() )
 .withSchema( new Schema()
 .field("id", DataTypes.STRING())
 .field("temp", DataTypes.DOUBLE())
 )
 .createTemporaryTable("kafkaOutputTable")
resultTable.insertInto("kafkaOutputTable")

 3.ElasticSearch

<dependency>
     <groupId>org.apache.flink</groupId>
     <artifactId>flink-json</artifactId>
     <version>1.10.0</version>
</dependency
// 输出到 es
tableEnv.connect(
 new Elasticsearch()
 .version("6")
 .host("hdp-1", 9200, "http")
 .index("sensor")
 .documentType("temp") )
 .inUpsertMode() // 指定是 Upsert 模式
 .withFormat(new Json())
 .withSchema( new Schema()
 .field("id", DataTypes.STRING())
 .field("count", DataTypes.BIGINT())
 )
 .createTemporaryTable("esOutputTable")
aggResultTable.insertInto("esOutputTable")

4.MySQL

Flink 专门为 Table API jdbc 连接提供了 flink-jdbc 连接器,我们需要先引入依赖

<dependency>
     <groupId>org.apache.flink</groupId>
     <artifactId>flink-jdbc_2.11</artifactId>
     <version>1.10.0</version>
</dependency>

jdbc 连接的代码实现比较特殊,因为没有对应的 java/scala 类实现 ConnectorDescriptor,所以不能直接调connect()。不过Flink SQL留下了执行DDL的接口:tableEnv.sqlUpdate()。对于 jdbc 的创建表操作,天生就适合直接写 DDL 来实现

// 输出到 Mysql
val sinkDDL: String =
 """
 |create table jdbcOutputTable (
 | id varchar(20) not null,
 | cnt bigint not null
 |) with (
 | 'connector.type' = 'jdbc',
 | 'connector.url' = 'jdbc:mysql://localhost:3306/test',
 | 'connector.table' = 'sensor_count',
 | 'connector.driver' = 'com.mysql.jdbc.Driver',
 | 'connector.username' = 'root',
 | 'connector.password' = 'root'
 |)
 """.stripMargin
tableEnv.sqlUpdate(sinkDDL)
aggResultSqlTable.insertInto("jdbcOutputTable")

猜你喜欢

转载自blog.csdn.net/weixin_43233971/article/details/107890586