当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 如何将 PyTorch Lightning 模型部署到生产中
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
- 运维人员常用到的 11 款服务器监控工具
- How to deploy pytorch lightning model to production
- Python Gadgets: code conversion
- 原创 | 数据资产确权浅议
- 11 server monitoring tools commonly used by operation and maintenance personnel
- Adobe Lightroom /Lr 2021软件安装包(附安装教程)
- How to write a resume and project
猜你喜欢

Written interview questions: find the smallest positive integer missing

Adobe media encoder / me 2021 software installation package (with installation tutorial)

Adobe Lightroom /Lr 2021软件安装包(附安装教程)

python基本语法 变量

How to deploy pytorch lightning model to production

最全!阿里巴巴经济体云原生实践!(附网盘链接)

Harbor项目高手问答及赠书活动

笔试面试题目:求丢失的猪

TCP协议如何确保可靠传输

Get PMP certificate at 51CTO College
随机推荐
Python基础语法
一个方案提升Flutter内存利用率
2 days, using 4 hours after work to develop a test tool
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
This time Kwai tiktok is faster than shaking.
Istio流量管理--Ingress Gateway
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Python basic syntax variables
Powershell 使用.Net对象发送邮件
C language I blog assignment 03
Written interview questions: find the smallest positive integer missing
为 Docsify 自动生成 RSS 订阅
Q & A and book giving activities of harbor project experts
The container with the most water
Tight supply! Apple's iPhone 12 power chip capacity exposed
蘑菇街电商交易平台服务架构及改造优化历程(含PPT)
Analysis of ArrayList source code
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
Istio traffic management -- progress gateway
OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎