当前位置:网站首页>Flink: from introduction to Zhenxiang (3. Reading data from collection and file)
Flink: from introduction to Zhenxiang (3. Reading data from collection and file)
2020-11-08 12:06:00 【open_neocf7df】
You can refer to : https://blog.51cto.com/mapengfei/2546985
Reading data from a collection
New package ,com.mafei.apitest, Create a new one scala Object class ,
package com.mafei.apitest
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
// Get sensor data
case class SensorReading(id: String,timestamp: Long, temperature: Double)
object SourceTest {
def main(args: Array[String]): Unit = {
// Create an execution environment
val env = StreamExecutionEnvironment.getExecutionEnvironment
// 1、 Reading data from a collection
val dataList = List(
SensorReading("sensor1",1603766281,41),
SensorReading("sensor2",1603766282,42),
SensorReading("sensor3",1603766283,43),
SensorReading("sensor4",1603766284,44)
)
val stream1 = env.fromCollection(dataList)
stream1.print()
// perform
env.execute(" source test")
}
}
Code catalog diagram :
Running effect
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2> SensorReading(sensor3,1603766283,43.0)
4> SensorReading(sensor1,1603766281,41.0)
3> SensorReading(sensor4,1603766284,44.0)
1> SensorReading(sensor2,1603766282,42.0)
Read data from file
Just like the first step , New package ,com.mafei.apitest, Create a new one scala Object class ,
package com.mafei.apitest
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
// Get sensor data
case class SensorReading(id: String,timestamp: Long, temperature: Double)
object SourceTest {
def main(args: Array[String]): Unit = {
// Create an execution environment
val env = StreamExecutionEnvironment.getExecutionEnvironment
// Reading data from a file
val stream2= env.readTextFile("/opt/java2020_study/maven/flink1/src/main/resources/sensor.txt")
stream2.print()
// perform
env.execute(" source test")
}
}
stay resources New under the directory sensor.txt, Write the following
sensor1,1603766281,41
sensor2,1603766282,42
sensor3,1603766283,43
sensor4,1603766284,44
Code structure diagram :
Code running effect :
1> sensor1,1603766281,41
1> sensor2,1603766282,42
2> sensor3,1603766283,43
3> sensor4,1603766284,44
版权声明
本文为[open_neocf7df]所创,转载请带上原文链接,感谢
边栏推荐
- 如何将 PyTorch Lightning 模型部署到生产中
- 运维人员常用到的 11 款服务器监控工具
- 擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 浅谈单调栈
- AQS analysis
- PMP考试通过心得分享
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- It's 20% faster than python. Are you excited?
- Written interview questions: find the smallest positive integer missing
猜你喜欢

【Python 1-6】Python教程之——数字

供货紧张!苹果被曝 iPhone 12 电源芯片产能不足

211考研失败后,熬夜了两个月拿下字节offer!【面经分享】

如何将 PyTorch Lightning 模型部署到生产中

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

Bccoin tells you: what is the most reliable investment project at the end of the year!

运维人员常用到的 11 款服务器监控工具

优化if-else代码的八种方案

Ali tear off the e-commerce label

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
随机推荐
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
C语言I博客作业03
阿里教你深入浅出玩转物联网平台!(附网盘链接)
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Second assignment
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
Ubuntu20.04 access FTP server garbled problem + upload files
值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
PMP心得分享
漫画|讲解一下如何写简历&项目
Shell uses. Net objects to send mail
Can you do it with only six characters?
Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
Adobe Lightroom /Lr 2021软件安装包(附安装教程)
Understanding design patterns
next.js实现服务端缓存
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom