当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 笔试面试题目:盛水最多的容器
- IQKeyboardManager 源代码看看
- How to write a resume and project
- 入门级!教你小程序开发不求人(附网盘链接)
- 第二次作业
- [data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
- 笔试面试题目:求缺失的最小正整数
- 我们采访了阿里云云数据库SQL Server的产品经理,他说了解这四个问题就可以了...
- Flink的sink实战之一:初探
- Installing MacOS 11 Big Sur in virtual machine
猜你喜欢

Enabling education innovation and reconstruction with science and technology Huawei implements education informatization

阿里教你深入浅出玩转物联网平台!(附网盘链接)

Rust: performance test criteria Library
![211 postgraduate entrance examination failed, stay up for two months, get the byte offer! [face to face sharing]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
211 postgraduate entrance examination failed, stay up for two months, get the byte offer! [face to face sharing]

Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)

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

Xamarin 从零开始部署 iOS 上的 Walterlv.CloudKeyboard 应用

擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?

Can you do it with only six characters?

Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
随机推荐
Research on WLAN direct connection (peer-to-peer connection or P2P) and cross platform research of IOS
Powershell 使用.Net对象发送邮件
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Adobe media encoder /Me 2021软件安装包(附安装教程)
The container with the most water
浅谈单调栈
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
C语言I博客作业03
It's 20% faster than python. Are you excited?
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
Installing MacOS 11 Big Sur in virtual machine
python基础教程python opencv pytesseract 验证码识别的实现
适合c/c++新手学习的一些项目,别给我错过了!
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
Implementation of verification code recognition in Python opencv pytesseract
Flink的sink实战之一:初探
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
Bccoin tells you: what is the most reliable investment project at the end of the year!
TiDB 性能竞赛 11.02-11.06
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom