当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 解析Istio访问控制
- 年轻一代 winner 的程序人生,改变世界的起点藏在身边
- Istio traffic management -- progress gateway
- When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
- 吐血整理!阿里巴巴 Android 开发手册!(附网盘链接)
- C语言I博客作业03
- The container with the most water
- 我们采访了阿里云云数据库SQL Server的产品经理,他说了解这四个问题就可以了...
- AQS解析
- This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?
猜你喜欢

PMP experience sharing

Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)

BCCOIN告诉您:年底最靠谱的投资项目是什么!

Mozi college SQL injection solution

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

擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?

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

Introduction to mongodb foundation of distributed document storage database

原创 | 数据资产确权浅议

Q & A and book giving activities of harbor project experts
随机推荐
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
Xamarin 从零开始部署 iOS 上的 Walterlv.CloudKeyboard 应用
How to deploy pytorch lightning model to production
11 server monitoring tools commonly used by operation and maintenance personnel
YGC troubleshooting, let me rise again!
分布式文档存储数据库之MongoDB基础入门
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
年轻一代 winner 的程序人生,改变世界的起点藏在身边
你的云服务器可以用来做什么?云服务器有什么用途?
Python basic syntax variables
解析Istio访问控制
Flink从入门到真香(6、Flink实现UDF函数-实现更细粒度的控制流)
Is software testing training class easy to find a job
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
Installing MacOS 11 Big Sur in virtual machine
211 postgraduate entrance examination failed, stay up for two months, get the byte offer! [face to face sharing]
Iqkeyboardmanager source code to see
This time Kwai tiktok is faster than shaking.
Personal current technology stack