当前位置:网站首页>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
边栏推荐
- Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
- Web novice problem of attacking and defending the world
- 【计算机网络】学习笔记,第三篇:数据链路层(谢希仁版)
- 笔试面试题目:判断单链表是否有环
- 应届生年薪35w+ !倒挂老员工,互联网大厂薪资为何越来越高?
- Istio traffic management -- progress gateway
- ArrayList源码分析
- Second assignment
- 年轻一代 winner 的程序人生,改变世界的起点藏在身边
- C language I blog assignment 03
猜你喜欢
VC + + specified directory file output by time
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?
Is software testing training class easy to find a job
PMP考试通过心得分享
How to deploy pytorch lightning model to production
BCCOIN告诉您:年底最靠谱的投资项目是什么!
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
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
Xamarin 从零开始部署 iOS 上的 Walterlv.CloudKeyboard 应用
随机推荐
Adobe media encoder /Me 2021软件安装包(附安装教程)
Ali! Visual computing developer's series of manuals (with internet disk link)
The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
分布式文档存储数据库之MongoDB基础入门
Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
PMP考试通过心得分享
Learning summary (about deep learning, vision and learning experience)
浅谈单调栈
OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎
The container with the most water
PMP experience sharing
入门级!教你小程序开发不求人(附网盘链接)
运维人员常用到的 11 款服务器监控工具
How to deploy pytorch lightning model to production
Research on WLAN direct connection (peer-to-peer connection or P2P) and cross platform research of IOS
笔试面试题目:求缺失的最小正整数
YGC问题排查,又让我涨姿势了!
Automatically generate RSS feeds for docsify
Win10 Terminal + WSL 2 安装配置指南,精致开发体验