当前位置:网站首页>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
边栏推荐
- PMP experience sharing
- Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
- It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
- Understanding design patterns
- Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
- 阿里出品!视觉计算开发者系列手册(附网盘链接)
- C language I blog assignment 03
- 解析Istio访问控制
- 当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
- YGC troubleshooting, let me rise again!
猜你喜欢

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom

一个方案提升Flutter内存利用率

Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining

PDMS cutting software
![[computer network] learning notes, Part 3: data link layer (Xie Xiren version)](/img/b0/b236a52e38f1cd3eff25a398dac7aa.jpg)
[computer network] learning notes, Part 3: data link layer (Xie Xiren version)

Web novice problem of attacking and defending the world

学习小结(关于深度学习、视觉和学习体会)

When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)

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

Analysis of ArrayList source code
随机推荐
Iqkeyboardmanager source code to see
渤海银行百万级罚单不断:李伏安却称治理完善,增速呈下滑趋势
Xamarin deploys IOS from scratch Walterlv.CloudKeyboard application
最全!阿里巴巴经济体云原生实践!(附网盘链接)
笔试面试题目:求丢失的猪
分布式文档存储数据库之MongoDB基础入门
AQS解析
PMP心得分享
Istio traffic management -- progress gateway
一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!
Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
PCIe enumeration process
We interviewed the product manager of SQL server of Alibaba cloud database, and he said that it is enough to understand these four problems
Adobe Lightroom /Lr 2021软件安装包(附安装教程)
Get PMP certificate at 51CTO College
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Bccoin tells you: what is the most reliable investment project at the end of the year!
第二次作业
Python基础语法