当前位置:网站首页>[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();
}
}边栏推荐
- FLASH read / write problem of stm32cubemx
- Riotboard development board series notes (4) -- using Vpu hardware decoding
- P100 MSSQL database penetration test of secondary vocational network security skills competition
- Use and introduction of vim file editor
- Define macros in makefile and pass them to source code
- Chrome process architecture
- Brief understanding of operational amplifier
- Flink1.15 source code reading - Flink annotations
- Function method encapsulation -- mutual conversion of image types qpixmap, qimage and mat
- Function of each layer of data warehouse
猜你喜欢

Use of stm32cubemonitor part I - data plotting and instrument display

Bgy development small example

Chrome process architecture

04 -- two ways of writing el and data

Li Kou 279 complete square - dynamic programming

Unified return data format

Solution: owner's smart site supervision cloud platform
![[template engine] microservice Learning Notes 6: freemaker](/img/6a/cfe9c5aea0f7fc83d0812237de2256.png)
[template engine] microservice Learning Notes 6: freemaker

Use reflection to convert RDD to dataframe
![[Kali's sshd service is enabled]](/img/1b/180534d51049177254e30c4b783eba.png)
[Kali's sshd service is enabled]
随机推荐
Leetcode programming practice -- Tencent selected 50 questions (I)
Matplotlib tutorial (I) [getting to know Matplotlib first]
Dc-1-practice
[kaggle] how to effectively avoid oom and the long process of alchemy
CVPR 2020 | social stgcnn: pedestrian trajectory prediction based on graph convolution
C language function operation
A. Subtle Substring Subtraction
Unified return data format
PHP record
Brief understanding of operational amplifier
The difference between abstract classes and interfaces
B. Difference of GCDs
Imeta | ggclusternet microbial network analysis and visualization nanny level tutorial
[brother hero July training] day 19: binary tree
MySQL configuration in CDH installation
Vscode copy synchronization plug-in expansion
144. Preorder traversal of binary tree
mysql_ User table_ Field meaning
Riotboard development board series notes (4) -- using Vpu hardware decoding
Banana pie bpi-m5 toss record (3) -- compile BSP