当前位置:网站首页>Flink从入门到真香(3、从集合和文件中读取数据)
Flink从入门到真香(3、从集合和文件中读取数据)
2020-11-08 12:06:00 【osc_neocf7df】
关于环境准备可以参考: https://blog.51cto.com/mapengfei/2546985
从集合中读取数据
新建包,com.mafei.apitest,新建一个scala Object类,
package com.mafei.apitest
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
//获取传感器数据
case class SensorReading(id: String,timestamp: Long, temperature: Double)
object SourceTest {
def main(args: Array[String]): Unit = {
//创建执行环境
val env = StreamExecutionEnvironment.getExecutionEnvironment
// 1、从集合中读取数据
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()
//执行
env.execute(" source test")
}
}
代码目录图:
运行效果
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)
从文件读取数据
和第一步一样,新建包,com.mafei.apitest,新建一个scala Object类,
package com.mafei.apitest
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
//获取传感器数据
case class SensorReading(id: String,timestamp: Long, temperature: Double)
object SourceTest {
def main(args: Array[String]): Unit = {
//创建执行环境
val env = StreamExecutionEnvironment.getExecutionEnvironment
//从文件中读取数据
val stream2= env.readTextFile("/opt/java2020_study/maven/flink1/src/main/resources/sensor.txt")
stream2.print()
//执行
env.execute(" source test")
}
}
在resources目录下新建sensor.txt,写入以下内容
sensor1,1603766281,41
sensor2,1603766282,42
sensor3,1603766283,43
sensor4,1603766284,44
代码结构图:
代码运行效果:
1> sensor1,1603766281,41
1> sensor2,1603766282,42
2> sensor3,1603766283,43
3> sensor4,1603766284,44
版权声明
本文为[osc_neocf7df]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4416758/blog/4708099
边栏推荐
- Bccoin tells you: what is the most reliable investment project at the end of the year!
- The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
- 仅用六种字符来完成Hello World,你能做到吗?
- Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
- WLAN 直连(对等连接或 P2P)调研及iOS跨平台调研
- 新的目标市场在哪里?锚定的产品是什么?| 十问2021中国企业服务
- Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
- PDMS cutting software
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Dogs can also operate drones! You're right, but it's actually an autonomous drone - you know
猜你喜欢
Istio traffic management -- progress gateway
Improvement of rate limit for laravel8 update
2天,利用下班后的4小时开发一个测试工具
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
C语言I博客作业03
软件测试培训班出来好找工作么
PCIe enumeration process
年轻一代 winner 的程序人生,改变世界的起点藏在身边
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
The container with the most water
随机推荐
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
攻防世界之web新手题
PDMS cutting software
On monotonous stack
解析Istio访问控制
入门级!教你小程序开发不求人(附网盘链接)
Improvement of rate limit for laravel8 update
Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
Python basic syntax variables
阿里撕下电商标签
python基础教程python opencv pytesseract 验证码识别的实现
C language I blog assignment 03
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
你的云服务器可以用来做什么?云服务器有什么用途?
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
比Python快20%,就问你兴不兴奋?
临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire