当前位置:网站首页>[Flink] transform operator flatmap
[Flink] transform operator flatmap
2022-07-25 03:27:00 【No bug is the biggest bug】
One 、 Entity class
@Data
public class Event {
public String user;
public String url;
public Long timestamp;
public Event(String user, String url, Long timestamp) {
this.user = user;
this.url = url;
this.timestamp = timestamp;
}
}Two 、 Custom data sources simulate streaming data
public class ClickSource implements SourceFunction<Event> {
// Declare a flag class
private Boolean running = true;
@Override
public void run(SourceContext<Event> ctx) throws Exception {
// Randomly generated data
Random random = new Random();
// Define the data set selected by the field
String[] users = {"Mary","Alice","Bob","Cary"};
String[] urls = {"./home","./cart","./fav","./prod?id=100","./prod:id=10"};
// Loop data
while (running){
String user = users[random.nextInt(users.length)];
String url = urls[random.nextInt(user.length())];
Long time = System.currentTimeMillis();
ctx.collect(new Event(user,url,time));
Thread.sleep(2000);
}
}
@Override
public void cancel() {
running = false;
}
}3、 ... and 、 Task code
public class TransFormFlatMapTest {
public static void main(String[] args) throws Exception {
// Create an execution environment
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// Set parallelism
env.setParallelism(1);
// Read data source
DataStreamSource<Event> dataStream = env.addSource(new ClickSource());
// Perform a conversion operation Flat mapping
SingleOutputStreamOperator<String> flatMap = dataStream.flatMap(new FlatMapFunction<Event, String>() {
@Override
public void flatMap(Event event, Collector<String> collector) throws Exception {
if (event.user.equals("Bob")) {
collector.collect(event.url);
}
}
});
// Print
flatMap.print();
// Start
env.execute();
}
}边栏推荐
- JS password combination rule - 8-16 digit combination of numbers and characters, not pure numbers and pure English
- Banana pie bpi-m5 toss record (3) -- compile BSP
- Secondary vocational network security skills competition P100 web penetration test
- Matplotlib tutorial (I) [getting to know Matplotlib first]
- What is technical support| Daily anecdotes
- FLASH read / write problem of stm32cubemx
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- 55k is stable, and the recommendation system will always drop God!
- Solution: owner's smart site supervision cloud platform
- Leetcode.745. prefix and suffix search____ Double dictionary tree + double pointer
猜你喜欢

Flowlayout in compose

Secondary vocational network security skills competition P100 dcore (light CMS system) SQL injection

Force deduction problem 238. product of arrays other than itself

Use of stm32cubemonitor Part II - historical data storage and network access

Experiment 4 CTF practice

Hw2021 attack and defense drill experience - Insights

Lombok detailed introduction

Solution: owner's smart site supervision cloud platform
![[Kali's sshd service is enabled]](/img/1b/180534d51049177254e30c4b783eba.png)
[Kali's sshd service is enabled]

Dc-2-range practice
随机推荐
Time formatting
292. Nim game
B. Difference of GCDs
Flink1.15 source code reading - Flink annotations
Use and introduction of vim file editor
Unity word document click button to download
Implementation principle of virtual DOM
Experience sharing of system architecture designers in preparing for the exam: how to prepare for the exam effectively
ECMAScript new features
How is the virtual DOM different from the actual DOM?
Leetcode programming practice -- Tencent selected 50 questions (I)
"Introduction to interface testing" punch in to learn day04: how to abstract the procedural test script into a test framework?
Analysis of cascading relation operation examples of cascade
Question B: shunzi date
C language writes a circular advertising lantern or changes it to a confession system
Secondary vocational network security skills competition P100 vulnerability detection
C. Mark and His Unfinished Essay
Lombok detailed introduction
P100 MSSQL database penetration test of secondary vocational network security skills competition
A. Subtle Substring Subtraction