当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- python基础教程python opencv pytesseract 验证码识别的实现
- 分布式文档存储数据库之MongoDB基础入门
- 学习小结(关于深度学习、视觉和学习体会)
- 渤海银行百万级罚单不断:李伏安却称治理完善,增速呈下滑趋势
- 11 server monitoring tools commonly used by operation and maintenance personnel
- Personal current technology stack
- 阿里教你深入浅出玩转物联网平台!(附网盘链接)
- 分布式文档存储数据库之MongoDB基础入门
- 阿里出品!视觉计算开发者系列手册(附网盘链接)
- Rust : 性能测试criterion库
猜你喜欢
On monotonous stack
Rust : 性能测试criterion库
The progress bar written in Python is so wonderful~
Flink从入门到真香(7、Sink数据输出-文件)
Personal current technology stack
漫画|讲解一下如何写简历&项目
笔试面试题目:求缺失的最小正整数
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
This paper analyzes the top ten Internet of things applications in 2020!
Where is the new target market? What is the anchored product? |Ten questions 2021 Chinese enterprise service
随机推荐
C language I blog assignment 03
Get PMP certificate at 51CTO College
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
From a friend recently Ali, Tencent, meituan and other P7 Python development post interview questions
The young generation of winner's programming life, the starting point of changing the world is hidden around
Share the experience of passing the PMP examination
Flink的sink实战之一:初探
python小工具:编码转换
Web novice problem of attacking and defending the world
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
解析Istio访问控制
这次,快手终于比抖音'快'了!
Flink从入门到真香(7、Sink数据输出-文件)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Analysis of ArrayList source code
PMP心得分享
优化if-else代码的八种方案
Flink从入门到真香(6、Flink实现UDF函数-实现更细粒度的控制流)
PMP experience sharing
笔试面试题目:盛水最多的容器