当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- PMP心得分享
- python小工具:编码转换
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Tight supply! Apple's iPhone 12 power chip capacity exposed
- 不多不少,大学里必做的五件事(从我的大一说起)
- 适合c/c++新手学习的一些项目,别给我错过了!
- Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
- Flink从入门到真香(7、Sink数据输出-文件)
- Flink's sink: a preliminary study
- Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
猜你喜欢
On the confirmation of original data assets
Rust: performance test criteria Library
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
阿里教你深入浅出玩转物联网平台!(附网盘链接)
C language I blog assignment 03
Flink从入门到真香(3、从集合和文件中读取数据)
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
211考研失败后,熬夜了两个月拿下字节offer!【面经分享】
PMP experience sharing
随机推荐
Why is Schnorr Signature known as the biggest technology update after bitcoin segwit
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
应届生年薪35w+ !倒挂老员工,互联网大厂薪资为何越来越高?
Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
年轻一代 winner 的程序人生,改变世界的起点藏在身边
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
The young generation of winner's programming life, the starting point of changing the world is hidden around
Python基础语法
用科技赋能教育创新与重构 华为将教育信息化落到实处
Introduction to mongodb foundation of distributed document storage database
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
笔试面试题目:判断单链表是否有环
一文读懂机器学习“数据中毒”
How to deploy pytorch lightning model to production
软件测试培训班出来好找工作么
PDMS cutting software
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
不多不少,大学里必做的五件事(从我的大一说起)
入门级!教你小程序开发不求人(附网盘链接)