当前位置:网站首页>Flink: from introduction to Zhenxiang (3. Reading data from collection and file)
Flink: from introduction to Zhenxiang (3. Reading data from collection and file)
2020-11-08 12:06:00 【open_neocf7df】
You can refer to : https://blog.51cto.com/mapengfei/2546985
Reading data from a collection
New package ,com.mafei.apitest, Create a new one scala Object class ,
package com.mafei.apitest
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
// Get sensor data
case class SensorReading(id: String,timestamp: Long, temperature: Double)
object SourceTest {
def main(args: Array[String]): Unit = {
// Create an execution environment
val env = StreamExecutionEnvironment.getExecutionEnvironment
// 1、 Reading data from a collection
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()
// perform
env.execute(" source test")
}
}
Code catalog diagram :
Running effect
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)
Read data from file
Just like the first step , New package ,com.mafei.apitest, Create a new one scala Object class ,
package com.mafei.apitest
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
// Get sensor data
case class SensorReading(id: String,timestamp: Long, temperature: Double)
object SourceTest {
def main(args: Array[String]): Unit = {
// Create an execution environment
val env = StreamExecutionEnvironment.getExecutionEnvironment
// Reading data from a file
val stream2= env.readTextFile("/opt/java2020_study/maven/flink1/src/main/resources/sensor.txt")
stream2.print()
// perform
env.execute(" source test")
}
}
stay resources New under the directory sensor.txt, Write the following
sensor1,1603766281,41
sensor2,1603766282,42
sensor3,1603766283,43
sensor4,1603766284,44
Code structure diagram :
Code running effect :
1> sensor1,1603766281,41
1> sensor2,1603766282,42
2> sensor3,1603766283,43
3> sensor4,1603766284,44
版权声明
本文为[open_neocf7df]所创,转载请带上原文链接,感谢
边栏推荐
- Enabling education innovation and reconstruction with science and technology Huawei implements education informatization
- How TCP protocol ensures reliable transmission
- Tight supply! Apple's iPhone 12 power chip capacity exposed
- 阿里教你深入浅出玩转物联网平台!(附网盘链接)
- Research on WLAN direct connection (peer-to-peer connection or P2P) and cross platform research of IOS
- Flink从入门到真香(10、Sink数据输出-Elasticsearch)
- 入门级!教你小程序开发不求人(附网盘链接)
- PMP心得分享
- 笔试面试题目:求丢失的猪
- 2天,利用下班后的4小时开发一个测试工具
猜你喜欢

优化if-else代码的八种方案

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?

Introduction to mongodb foundation of distributed document storage database

笔试面试题目:盛水最多的容器

It's 20% faster than python. Are you excited?
![211 postgraduate entrance examination failed, stay up for two months, get the byte offer! [face to face sharing]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
211 postgraduate entrance examination failed, stay up for two months, get the byte offer! [face to face sharing]

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

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

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

Flink的sink实战之一:初探
随机推荐
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
解析Istio访问控制
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
Get PMP certificate at 51CTO College
Can you do it with only six characters?
The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
Written interview topic: looking for the lost pig
一文剖析2020年最火十大物联网应用|IoT Analytics 年度重磅报告出炉!
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
入门级!教你小程序开发不求人(附网盘链接)
Istio traffic management -- progress gateway
学习小结(关于深度学习、视觉和学习体会)
Written interview questions: find the smallest positive integer missing
The container with the most water
Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
A scheme to improve the memory utilization of flutter
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
蘑菇街电商交易平台服务架构及改造优化历程(含PPT)
Adobe media encoder /Me 2021软件安装包(附安装教程)