当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- The young generation of winner's programming life, the starting point of changing the world is hidden around
- 笔试面试题目:盛水最多的容器
- Improvement of rate limit for laravel8 update
- 211考研失败后,熬夜了两个月拿下字节offer!【面经分享】
- Implementation of verification code recognition in Python opencv pytesseract
- 这次,快手终于比抖音'快'了!
- Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
- On monotonous stack
- 阿里教你深入浅出玩转物联网平台!(附网盘链接)
- Tidb performance competition 11.02-11.06
猜你喜欢
随机推荐
Analysis of ArrayList source code
Flink的sink实战之一:初探
Understanding design patterns
Xamarin 从零开始部署 iOS 上的 Walterlv.CloudKeyboard 应用
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
在51CTO学院Get到PMP证书
C language I blog assignment 03
用 Python 写出来的进度条,竟如此美妙~
分布式文档存储数据库之MongoDB基础入门
C language I blog assignment 03
A scheme to improve the memory utilization of flutter
Python Gadgets: code conversion
Flink从入门到真香(7、Sink数据输出-文件)
如何将 PyTorch Lightning 模型部署到生产中
It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
On monotonous stack
Get PMP certificate at 51CTO College
Ubuntu20.04 access FTP server garbled problem + upload files






