1+ package com .baeldung .flink ;
2+
3+ import org .apache .flink .api .common .functions .ReduceFunction ;
4+ import org .apache .flink .api .common .operators .Order ;
5+ import org .apache .flink .api .java .DataSet ;
6+ import org .apache .flink .api .java .ExecutionEnvironment ;
7+ import org .apache .flink .api .java .functions .KeySelector ;
8+ import org .apache .flink .api .java .tuple .Tuple2 ;
9+ import org .apache .flink .api .java .tuple .Tuple3 ;
10+ import org .apache .flink .streaming .api .datastream .DataStream ;
11+ import org .apache .flink .streaming .api .datastream .SingleOutputStreamOperator ;
12+ import org .apache .flink .streaming .api .environment .StreamExecutionEnvironment ;
13+ import org .apache .flink .streaming .api .functions .timestamps .BoundedOutOfOrdernessTimestampExtractor ;
14+ import org .apache .flink .streaming .api .windowing .assigners .TumblingEventTimeWindows ;
15+ import org .apache .flink .streaming .api .windowing .time .Time ;
16+ import org .junit .Test ;
17+
18+ import java .time .ZonedDateTime ;
19+ import java .util .Arrays ;
20+ import java .util .List ;
21+
22+ import static org .assertj .core .api .Assertions .assertThat ;
23+
24+
25+ public class WordCountIntegrationTest {
26+ final ExecutionEnvironment env = ExecutionEnvironment .getExecutionEnvironment ();
27+
28+ @ Test
29+ public void givenDataSet_whenExecuteWordCount_thenReturnWordCount () throws Exception {
30+ //given
31+ List <String > lines = Arrays .asList ("This is a first sentence" , "This is a second sentence with a one word" );
32+
33+ //when
34+ DataSet <Tuple2 <String , Integer >> result = WordCount .startWordCount (env , lines );
35+
36+ //then
37+ List <Tuple2 <String , Integer >> collect = result .collect ();
38+ assertThat (collect .size ()).isEqualTo (9 );
39+ assertThat (collect .contains (new Tuple2 <>("a" , 3 ))).isTrue ();
40+ assertThat (collect .contains (new Tuple2 <>("sentence" , 2 ))).isTrue ();
41+ assertThat (collect .contains (new Tuple2 <>("word" , 1 ))).isTrue ();
42+ assertThat (collect .contains (new Tuple2 <>("is" , 2 ))).isTrue ();
43+ assertThat (collect .contains (new Tuple2 <>("this" , 2 ))).isTrue ();
44+ assertThat (collect .contains (new Tuple2 <>("second" , 1 ))).isTrue ();
45+ assertThat (collect .contains (new Tuple2 <>("first" , 1 ))).isTrue ();
46+ }
47+
48+ @ Test
49+ public void givenListOfAmounts_whenUseMapReduce_thenSumAmountsThatAreOnlyAboveThreshold () throws Exception {
50+ //given
51+ DataSet <Integer > amounts = env .fromElements (1 , 29 , 40 , 50 );
52+ int threshold = 30 ;
53+
54+ //when
55+ List <Integer > collect = amounts
56+ .filter (a -> a > threshold )
57+ .reduce ((ReduceFunction <Integer >) (integer , t1 ) -> integer + t1 )
58+ .collect ();
59+
60+ //then
61+ assertThat (collect .get (0 )).isEqualTo (90 );
62+ }
63+
64+ @ Test
65+ public void givenDataSetOfComplexObjects_whenMapToGetOneField_thenReturnedListHaveProperElements () throws Exception {
66+ //given
67+ DataSet <Person > personDataSource = env .fromCollection (Arrays .asList (new Person (23 , "Tom" ), new Person (75 , "Michael" )));
68+
69+ //when
70+ List <Integer > ages = personDataSource .map (p -> p .age ).collect ();
71+
72+ //then
73+ assertThat (ages .size ()).isEqualTo (2 );
74+ assertThat (ages .containsAll (Arrays .asList (23 , 75 ))).isTrue ();
75+
76+ }
77+
78+ @ Test
79+ public void givenDataSet_whenSortItByOneField_thenShouldReturnSortedDataSet () throws Exception {
80+ //given
81+ Tuple2 <Integer , String > secondPerson = new Tuple2 <>(4 , "Tom" );
82+ Tuple2 <Integer , String > thirdPerson = new Tuple2 <>(5 , "Scott" );
83+ Tuple2 <Integer , String > fourthPerson = new Tuple2 <>(200 , "Michael" );
84+ Tuple2 <Integer , String > firstPerson = new Tuple2 <>(1 , "Jack" );
85+ DataSet <Tuple2 <Integer , String >> transactions = env .fromElements (fourthPerson , secondPerson ,
86+ thirdPerson , firstPerson );
87+
88+
89+ //when
90+ List <Tuple2 <Integer , String >> sorted = transactions
91+ .sortPartition (new IdKeySelectorTransaction (), Order .ASCENDING )
92+ .collect ();
93+
94+ //then
95+ assertThat (sorted .size ()).isEqualTo (4 );
96+ assertThat (sorted .get (0 )).isEqualTo (firstPerson );
97+ assertThat (sorted .get (1 )).isEqualTo (secondPerson );
98+ assertThat (sorted .get (2 )).isEqualTo (thirdPerson );
99+ assertThat (sorted .get (3 )).isEqualTo (fourthPerson );
100+
101+ }
102+
103+
104+ @ Test
105+ public void giveTwoDataSets_whenJoinUsingId_thenProduceJoinedData () throws Exception {
106+ //given
107+ Tuple3 <Integer , String , String > address = new Tuple3 <>(1 , "5th Avenue" , "London" );
108+ DataSet <Tuple3 <Integer , String , String >> addresses = env .fromElements (address );
109+
110+ Tuple2 <Integer , String > firstTransaction = new Tuple2 <>(1 , "Transaction_1" );
111+ DataSet <Tuple2 <Integer , String >> transactions =
112+ env .fromElements (firstTransaction , new Tuple2 <>(12 , "Transaction_2" ));
113+
114+
115+ //when
116+ List <Tuple2 <Tuple2 <Integer , String >, Tuple3 <Integer , String , String >>> joined =
117+ transactions .join (addresses )
118+ .where (new IdKeySelectorTransaction ())
119+ .equalTo (new IdKeySelectorAddress ())
120+ .collect ();
121+
122+ //then
123+ assertThat (joined .size ()).isEqualTo (1 );
124+ assertThat (joined .contains (new Tuple2 <>(firstTransaction , address )));
125+
126+ }
127+
128+ @ Test
129+ public void givenStreamOfEvents_whenProcessEvents_thenShouldPrintResultsOnSinkOperation () throws Exception {
130+ //given
131+ final StreamExecutionEnvironment env = StreamExecutionEnvironment .getExecutionEnvironment ();
132+
133+ DataStream <String > text
134+ = env .fromElements ("This is a first sentence" , "This is a second sentence with a one word" );
135+
136+
137+ SingleOutputStreamOperator <String > upperCase = text .map (String ::toUpperCase );
138+
139+ upperCase .print ();
140+
141+ //when
142+ env .execute ();
143+ }
144+
145+
146+ @ Test
147+ public void givenStreamOfEvents_whenProcessEvents_thenShouldApplyWindowingOnTransformation () throws Exception {
148+ //given
149+ final StreamExecutionEnvironment env = StreamExecutionEnvironment .getExecutionEnvironment ();
150+
151+ SingleOutputStreamOperator <Tuple2 <Integer , Long >> windowed = env .fromElements (
152+ new Tuple2 <>(16 , ZonedDateTime .now ().plusMinutes (25 ).toInstant ().getEpochSecond ()),
153+ new Tuple2 <>(15 , ZonedDateTime .now ().plusMinutes (2 ).toInstant ().getEpochSecond ())
154+ ).assignTimestampsAndWatermarks (new BoundedOutOfOrdernessTimestampExtractor <Tuple2 <Integer , Long >>(Time .seconds (20 )) {
155+ @ Override
156+ public long extractTimestamp (Tuple2 <Integer , Long > element ) {
157+ return element .f1 * 1000 ;
158+ }
159+ });
160+
161+ SingleOutputStreamOperator <Tuple2 <Integer , Long >> reduced = windowed
162+ .windowAll (TumblingEventTimeWindows .of (Time .seconds (5 )))
163+ .maxBy (0 , true );
164+
165+ reduced .print ();
166+
167+ //when
168+ env .execute ();
169+ }
170+
171+
172+ private static class IdKeySelectorTransaction implements KeySelector <Tuple2 <Integer , String >, Integer > {
173+ @ Override
174+ public Integer getKey (Tuple2 <Integer , String > value ) {
175+ return value .f0 ;
176+ }
177+ }
178+
179+ private static class IdKeySelectorAddress implements KeySelector <Tuple3 <Integer , String , String >, Integer > {
180+ @ Override
181+ public Integer getKey (Tuple3 <Integer , String , String > value ) {
182+ return value .f0 ;
183+ }
184+ }
185+
186+ private static class Person {
187+ private final int age ;
188+ private final String name ;
189+
190+ private Person (int age , String name ) {
191+ this .age = age ;
192+ this .name = name ;
193+ }
194+ }
195+
196+ }
0 commit comments