24
24
import org .apache .flink .api .common .eventtime .WatermarkGeneratorSupplier ;
25
25
import org .apache .flink .api .common .eventtime .WatermarkStrategy ;
26
26
import org .apache .flink .api .common .functions .JoinFunction ;
27
+ import org .apache .flink .api .common .serialization .SimpleStringEncoder ;
27
28
import org .apache .flink .api .java .functions .KeySelector ;
28
29
import org .apache .flink .api .java .tuple .Tuple2 ;
29
30
import org .apache .flink .api .java .tuple .Tuple3 ;
30
31
import org .apache .flink .api .java .utils .ParameterTool ;
32
+ import org .apache .flink .configuration .MemorySize ;
33
+ import org .apache .flink .connector .file .sink .FileSink ;
34
+ import org .apache .flink .core .fs .Path ;
31
35
import org .apache .flink .streaming .api .datastream .DataStream ;
32
36
import org .apache .flink .streaming .api .environment .StreamExecutionEnvironment ;
37
+ import org .apache .flink .streaming .api .functions .sink .filesystem .rollingpolicies .DefaultRollingPolicy ;
33
38
import org .apache .flink .streaming .api .windowing .assigners .TumblingEventTimeWindows ;
34
39
import org .apache .flink .streaming .api .windowing .time .Time ;
35
40
import org .apache .flink .streaming .examples .join .WindowJoinSampleData .GradeSource ;
36
41
import org .apache .flink .streaming .examples .join .WindowJoinSampleData .SalarySource ;
37
42
43
+ import java .time .Duration ;
44
+
38
45
/**
39
46
* Example illustrating a windowed stream join between two data streams.
40
47
*
@@ -55,6 +62,7 @@ public static void main(String[] args) throws Exception {
55
62
final ParameterTool params = ParameterTool .fromArgs (args );
56
63
final long windowSize = params .getLong ("windowSize" , 2000 );
57
64
final long rate = params .getLong ("rate" , 3L );
65
+ final boolean fileOutput = params .has ("output" );
58
66
59
67
System .out .println ("Using windowSize=" + windowSize + ", data rate=" + rate );
60
68
System .out .println (
@@ -80,8 +88,23 @@ public static void main(String[] args) throws Exception {
80
88
DataStream <Tuple3 <String , Integer , Integer >> joinedStream =
81
89
runWindowJoin (grades , salaries , windowSize );
82
90
83
- // print the results with a single thread, rather than in parallel
84
- joinedStream .print ().setParallelism (1 );
91
+ if (fileOutput ) {
92
+ joinedStream
93
+ .sinkTo (
94
+ FileSink .<Tuple3 <String , Integer , Integer >>forRowFormat (
95
+ new Path (params .get ("output" )),
96
+ new SimpleStringEncoder <>())
97
+ .withRollingPolicy (
98
+ DefaultRollingPolicy .builder ()
99
+ .withMaxPartSize (MemorySize .ofMebiBytes (1 ))
100
+ .withRolloverInterval (Duration .ofSeconds (10 ))
101
+ .build ())
102
+ .build ())
103
+ .name ("output" );
104
+ } else {
105
+ // print the results with a single thread, rather than in parallel
106
+ joinedStream .print ().setParallelism (1 );
107
+ }
85
108
86
109
// execute program
87
110
env .execute ("Windowed Join Example" );
0 commit comments