当前位置:网站首页>Flink from introduction to Zhenxiang (7. Sink data output file)
Flink from introduction to Zhenxiang (7. Sink data output file)
2020-11-08 12:06:00 【osc_u9mt0sus】
Source yes Flink Program input ,Sink Namely Flink The program is finished Source After the data output , For example, writing output to a file 、sockets、 An external system 、 Or just show ( In the big data Ecology , A lot of similar , such as Flume It's also corresponding to Source/Channel/Sink),Flink Provides a variety of data output methods
It's not like writing directly in code ( For example, you can RickMap in open、close、map Direct writing ) He can save some state , Fault tolerant retrial mechanism and so on
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 = {
// Create an execution environment
val env = StreamExecutionEnvironment.getExecutionEnvironment
val inputStream= env.readTextFile("/opt/java2020_study/maven/flink1/src/main/resources/sensor.txt")
env.setParallelism(1)
// First convert to sample class type
val dataStream = inputStream
.map(data =>{
val arr = data.split(",") // according to , Split data , To get the results
SensorReadingTest3(arr(0), arr(1).toLong,arr(2).toDouble) // Generate data for a sensor class , Parameters are passed in the middle toLong and toDouble Because the default split is string category
})
dataStream.print()
// Simple output to txt The method in , Has been flink Abandoning
// dataStream.writeAsText("/opt/java2020_study/maven/flink1/src/main/resources/sink.txt")
// New output mode - recommend
dataStream.addSink(
StreamingFileSink.forRowFormat(
new Path("/opt/java2020_study/maven/flink1/src/main/resources/sink2.txt"),
new SimpleStringEncoder[SensorReadingTest3]() // You can pass in the encoding in parentheses , The default is udf-8
).build()
)
env.execute("udf test")
}
}
Code structure and final output effect :
版权声明
本文为[osc_u9mt0sus]所创,转载请带上原文链接,感谢
边栏推荐
- 入门级!教你小程序开发不求人(附网盘链接)
- 211 postgraduate entrance examination failed, stay up for two months, get the byte offer! [face to face sharing]
- Istio traffic management -- progress gateway
- Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
- Bccoin tells you: what is the most reliable investment project at the end of the year!
- The container with the most water
- 阿里撕下电商标签
- Analysis of ArrayList source code
- Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
- 来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题
猜你喜欢
The progress bar written in Python is so wonderful~
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Q & A and book giving activities of harbor project experts
It's 20% faster than python. Are you excited?
Why is Schnorr Signature known as the biggest technology update after bitcoin segwit
On the confirmation of original data assets
Written interview topic: looking for the lost pig
YGC troubleshooting, let me rise again!
优化if-else代码的八种方案
随机推荐
如何将 PyTorch Lightning 模型部署到生产中
Introduction to mongodb foundation of distributed document storage database
来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题
laravel8更新之速率限制改进
Flink从入门到真香(6、Flink实现UDF函数-实现更细粒度的控制流)
C语言I博客作业03
软件测试培训班出来好找工作么
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
WLAN 直连(对等连接或 P2P)调研及iOS跨平台调研
Installing MacOS 11 Big Sur in virtual machine
TCP协议如何确保可靠传输
学习小结(关于深度学习、视觉和学习体会)
This paper analyzes the top ten Internet of things applications in 2020!
解析Istio访问控制
墨者学院SQL注入解题
How to deploy pytorch lightning model to production
Personal current technology stack
Automatically generate RSS feeds for docsify
Adobe Lightroom /Lr 2021软件安装包(附安装教程)
入门级!教你小程序开发不求人(附网盘链接)