当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 虚拟机中安装 macOS 11 big sur
- 漫画|讲解一下如何写简历&项目
- Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
- Win10 Terminal + WSL 2 安装配置指南,精致开发体验
- 这次,快手终于比抖音'快'了!
- Where is the new target market? What is the anchored product? |Ten questions 2021 Chinese enterprise service
- 蘑菇街电商交易平台服务架构及改造优化历程(含PPT)
- How to write a resume and project
- Tight supply! Apple's iPhone 12 power chip capacity exposed
- 11 server monitoring tools commonly used by operation and maintenance personnel
猜你喜欢
OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎
Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
This paper analyzes the top ten Internet of things applications in 2020!
Flink从入门到真香(7、Sink数据输出-文件)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
新的目标市场在哪里?锚定的产品是什么?| 十问2021中国企业服务
Where is the new target market? What is the anchored product? |Ten questions 2021 Chinese enterprise service
用 Python 写出来的进度条,竟如此美妙~
C language I blog assignment 03
随机推荐
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
Get PMP certificate at 51CTO College
一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!
Second assignment
Flink从入门到真香(7、Sink数据输出-文件)
第二次作业
next.js实现服务端缓存
你的云服务器可以用来做什么?云服务器有什么用途?
优化if-else代码的八种方案
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
Python Gadgets: code conversion
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
Iqkeyboardmanager source code to see
PMP考试通过心得分享
Q & A and book giving activities of harbor project experts
Written interview topic: looking for the lost pig
The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
还不快看!对于阿里云云原生数据湖体系全解读!(附网盘链接)
If you don't understand the gap with others, you will never become an architect! What's the difference between a monthly salary of 15K and a monthly salary of 65K?