当前位置:网站首页>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
边栏推荐
- 原创 | 数据资产确权浅议
- PCIe enumeration process
- 一个方案提升Flutter内存利用率
- Written interview topic: looking for the lost pig
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- A scheme to improve the memory utilization of flutter
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- How TCP protocol ensures reliable transmission
- What can your cloud server do? What is the purpose of cloud server?
- How to write a resume and project
猜你喜欢

Automatically generate RSS feeds for docsify

YGC问题排查,又让我涨姿势了!

Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search

This paper analyzes the top ten Internet of things applications in 2020!

OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎

Share the experience of passing the PMP examination

What can your cloud server do? What is the purpose of cloud server?

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

笔试面试题目:判断单链表是否有环

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
随机推荐
如何将 PyTorch Lightning 模型部署到生产中
Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
Rust: performance test criteria Library
Recommend an economic science video, very valuable!
笔试面试题目:判断单链表是否有环
If you don't understand the gap with others, you will never become an architect! What's the difference between a monthly salary of 15K and a monthly salary of 65K?
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
蘑菇街电商交易平台服务架构及改造优化历程(含PPT)
Iqkeyboardmanager source code to see
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
漫画|讲解一下如何写简历&项目
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
阿里教你深入浅出玩转物联网平台!(附网盘链接)
YGC troubleshooting, let me rise again!
Flink的sink实战之一:初探
Personal current technology stack
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
A scheme to improve the memory utilization of flutter
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom