当前位置:网站首页>Flink从入门到真香(7、Sink数据输出-文件)
Flink从入门到真香(7、Sink数据输出-文件)
2020-11-08 12:06:00 【osc_u9mt0sus】
Source 是 Flink 程序的输入,Sink 就是 Flink 程序处理完Source后数据的输出,比如将输出写到文件、sockets、外部系统、或者仅仅是显示(在大数据生态中,很多类似的,比如Flume里也是对应的Source/Channel/Sink),Flink 提供了多种数据输出方式
跟在代码中直接写不同(比如可以在RickMap中open、close、map中直接写)他可以保存一些状态,容错重试机制等等
package com.mafei.sinktest
import org.apache.flink.api.common.serialization.SimpleStringEncoder
import org.apache.flink.core.fs.Path
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink
import org.apache.flink.streaming.api.scala.{StreamExecutionEnvironment, createTypeInformation}
case class SensorReadingTest3(id: String,timestamp: Long, temperature: Double)
object FileSink {
def main(args: Array[String]): Unit = {
//创建执行环境
val env = StreamExecutionEnvironment.getExecutionEnvironment
val inputStream= env.readTextFile("/opt/java2020_study/maven/flink1/src/main/resources/sensor.txt")
env.setParallelism(1)
//先转换成样例类类型
val dataStream = inputStream
.map(data =>{
val arr = data.split(",") //按照,分割数据,获取结果
SensorReadingTest3(arr(0), arr(1).toLong,arr(2).toDouble) //生成一个传感器类的数据,参数中传toLong和toDouble是因为默认分割后是字符串类别
})
dataStream.print()
//简单的输出到txt中的方法,已被flink弃用
// dataStream.writeAsText("/opt/java2020_study/maven/flink1/src/main/resources/sink.txt")
//新的输出方式-推荐
dataStream.addSink(
StreamingFileSink.forRowFormat(
new Path("/opt/java2020_study/maven/flink1/src/main/resources/sink2.txt"),
new SimpleStringEncoder[SensorReadingTest3]() //可以在括号中传入编码,默认是udf-8
).build()
)
env.execute("udf test")
}
}
代码结构及最终输出效果:

版权声明
本文为[osc_u9mt0sus]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4365833/blog/4708100
边栏推荐
- 软件测试培训班出来好找工作么
- Python basic syntax variables
- Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
- IQKeyboardManager 源代码看看
- This time Kwai tiktok is faster than shaking.
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- 解析Istio访问控制
- Powershell 使用.Net对象发送邮件
- The container with the most water
- Written interview topic: looking for the lost pig
猜你喜欢

一文读懂机器学习“数据中毒”

仅用六种字符来完成Hello World,你能做到吗?

虚拟机中安装 macOS 11 big sur

解析Istio访问控制

Japan PSE certification

用科技赋能教育创新与重构 华为将教育信息化落到实处

Ali tear off the e-commerce label

软件测试培训班出来好找工作么

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom

Dogs can also operate drones! You're right, but it's actually an autonomous drone - you know
随机推荐
Web novice problem of attacking and defending the world
YGC问题排查,又让我涨姿势了!
“1024”征文活动结果新鲜出炉!快来看看是否榜上有名?~~
不多不少,大学里必做的五件事(从我的大一说起)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Analysis of istio access control
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
这次,快手终于比抖音'快'了!
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
This time Kwai tiktok is faster than shaking.
浅谈单调栈
虚拟机中安装 macOS 11 big sur
分布式文档存储数据库之MongoDB基础入门
2 days, using 4 hours after work to develop a test tool
阿里出品!视觉计算开发者系列手册(附网盘链接)
为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
笔试面试题目:盛水最多的容器
How to deploy pytorch lightning model to production
Personal current technology stack
next.js实现服务端缓存