当前位置:网站首页>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
边栏推荐
- Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
- Harbor项目高手问答及赠书活动
- 值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)
- Can you do it with only six characters?
- Written interview questions: find the smallest positive integer missing
- Dogs can also operate drones! You're right, but it's actually an autonomous drone - you know
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- Xamarin 从零开始部署 iOS 上的 Walterlv.CloudKeyboard 应用
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- BCCOIN告诉您:年底最靠谱的投资项目是什么!
猜你喜欢
一个方案提升Flutter内存利用率
The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
Ali! Visual computing developer's series of manuals (with internet disk link)
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
Adobe media encoder /Me 2021软件安装包(附安装教程)
Installing MacOS 11 Big Sur in virtual machine
Tight supply! Apple's iPhone 12 power chip capacity exposed
Win10 Terminal + WSL 2 安装配置指南,精致开发体验
随机推荐
The container with the most water
Analysis of ArrayList source code
一文读懂机器学习“数据中毒”
学习小结(关于深度学习、视觉和学习体会)
python基础教程python opencv pytesseract 验证码识别的实现
阿里出品!视觉计算开发者系列手册(附网盘链接)
Adobe media encoder / me 2021 software installation package (with installation tutorial)
How to write a resume and project
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
Flink的sink实战之一:初探
运维人员常用到的 11 款服务器监控工具
Harbor项目高手问答及赠书活动
Share the experience of passing the PMP examination
当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
来自朋友最近阿里、腾讯、美团等P7级Python开发岗位面试题
IQKeyboardManager 源代码看看
This time Kwai tiktok is faster than shaking.
用科技赋能教育创新与重构 华为将教育信息化落到实处
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
笔试面试题目:求丢失的猪