当前位置:网站首页>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
边栏推荐
- Can you do it with only six characters?
- Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
- 个人目前技术栈
- Improvement of rate limit for laravel8 update
- Rust: performance test criteria Library
- The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
- 华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
- Automatically generate RSS feeds for docsify
- It's 20% faster than python. Are you excited?
- Analysis of istio access control
猜你喜欢

Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)

PDMS cutting software

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

211考研失败后,熬夜了两个月拿下字节offer!【面经分享】

C language I blog assignment 03

Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire

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

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

为 Docsify 自动生成 RSS 订阅

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?
随机推荐
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
用科技赋能教育创新与重构 华为将教育信息化落到实处
漫画|讲解一下如何写简历&项目
Xamarin 从零开始部署 iOS 上的 Walterlv.CloudKeyboard 应用
浅谈单调栈
Rust : 性能测试criterion库
应届生年薪35w+ !倒挂老员工,互联网大厂薪资为何越来越高?
Win10 Terminal + WSL 2 安装配置指南,精致开发体验
TCP协议如何确保可靠传输
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
仅用六种字符来完成Hello World,你能做到吗?
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
YGC troubleshooting, let me rise again!
PMP心得分享
Get PMP certificate at 51CTO College
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
还不快看!对于阿里云云原生数据湖体系全解读!(附网盘链接)
2天,利用下班后的4小时开发一个测试工具
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Python Gadgets: code conversion