当前位置:网站首页>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
边栏推荐
- 一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!
- 运维人员常用到的 11 款服务器监控工具
- 值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
- Shell uses. Net objects to send mail
- 华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
- 维图PDMS切图软件
- 年轻一代 winner 的程序人生,改变世界的起点藏在身边
- laravel8更新之速率限制改进
- 浅谈单调栈
- Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
猜你喜欢
Tidb performance competition 11.02-11.06
用 Python 写出来的进度条,竟如此美妙~
PMP experience sharing
一文读懂机器学习“数据中毒”
笔试面试题目:判断单链表是否有环
笔试面试题目:求缺失的最小正整数
Written interview questions: find the smallest positive integer missing
How to write a resume and project
A scheme to improve the memory utilization of flutter
Get PMP certificate at 51CTO College
随机推荐
Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
How TCP protocol ensures reliable transmission
蘑菇街电商交易平台服务架构及改造优化历程(含PPT)
不多不少,大学里必做的五件事(从我的大一说起)
Iqkeyboardmanager source code to see
笔试面试题目:求丢失的猪
Flink's sink: a preliminary study
Python Gadgets: code conversion
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
Personal current technology stack
C语言I博客作业03
Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
Introduction to mongodb foundation of distributed document storage database
This time Kwai tiktok is faster than shaking.
C语言I博客作业03
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
Adobe Lightroom /Lr 2021软件安装包(附安装教程)
如何将 PyTorch Lightning 模型部署到生产中
C language I blog assignment 03