Skip to content

Instantly share code, notes, and snippets.

@mxm
Last active November 22, 2016 10:49
Show Gist options
  • Save mxm/6cb1e6e9a572a26df76917176849f405 to your computer and use it in GitHub Desktop.
Save mxm/6cb1e6e9a572a26df76917176849f405 to your computer and use it in GitHub Desktop.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.myorg.quickstart;
import org.apache.flink.api.common.functions.CoGroupFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.AssignerWithPunctuatedWatermarks;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.triggers.ContinuousEventTimeTrigger;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import javax.annotation.Nullable;
import static org.apache.flink.streaming.api.windowing.time.Time.milliseconds;
public class William {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
DataStream<Integer> integerDataStreamSource = env
.fromElements(1, 2, 3, 4)
.assignTimestampsAndWatermarks(new AssignerWithPunctuatedWatermarks<Integer>() {
@Override
public long extractTimestamp(Integer element, long previousElementTimestamp) {
return (long) element;
}
@Nullable
@Override
public Watermark checkAndGetNextWatermark(Integer lastElement, long extractedTimestamp) {
return new Watermark(lastElement);
}
});
integerDataStreamSource
.map(new MapFunction<Integer, Tuple2<Integer, Integer>>() {
@Override
public Tuple2<Integer, Integer> map(Integer value) throws Exception {
return new Tuple2<>(1, value);
}
})
.keyBy(0)
.window(TumblingEventTimeWindows.of(milliseconds(10)))
.trigger(ContinuousEventTimeTrigger.of(milliseconds(1)))
.apply(new WindowFunction<Tuple2<Integer, Integer>, Integer, Tuple, TimeWindow>() {
@Override
public void apply(Tuple tuple, TimeWindow window, Iterable<Tuple2<Integer, Integer>> input, Collector<Integer> out) throws Exception {
System.out.println("tiggering");
for (Tuple2<Integer, Integer> val : input) {
System.out.println(val);
out.collect(val.f1);
}
}
})
.coGroup(integerDataStreamSource).where(new KeySelector<Integer, Integer>() {
@Override
public Integer getKey(Integer value) throws Exception {
return value;
}
})
.equalTo(new KeySelector<Integer, Integer>() {
@Override
public Integer getKey(Integer value) throws Exception {
return value;
}
})
.window(TumblingEventTimeWindows.of(milliseconds(10)))
.trigger(ContinuousEventTimeTrigger.of(milliseconds(1)))
.apply(new CoGroupFunction<Integer, Integer, Object>() {
@Override
public void coGroup(Iterable<Integer> first, Iterable<Integer> second, Collector<Object> out) throws Exception {
System.out.println("trigger cogroup");
System.out.println("first");
for (int val : first){
System.out.println(val);
}
System.out.println("second");
for (int val : second){
System.out.println(val);
}
}
});
env.execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment