Created
September 15, 2020 05:02
-
-
Save zyxue/bc566180d2b01f1e2e77c1bbe3a7c5e5 to your computer and use it in GitHub Desktop.
flink-DataStream-toy-example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 com.css.myflinkapp; | |
import org.apache.flink.api.common.functions.FlatMapFunction; | |
import org.apache.flink.api.java.tuple.Tuple2; | |
import org.apache.flink.api.java.utils.ParameterTool; | |
import org.apache.flink.streaming.api.datastream.DataStream; | |
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | |
import org.apache.flink.util.Collector; | |
/** | |
* Skeleton for a Flink Streaming Job. | |
* | |
* <p>For a tutorial how to write a Flink streaming application, check the | |
* tutorials and examples on the <a href="https://flink.apache.org/docs/stable/">Flink Website</a>. | |
* | |
* <p>To package your application into a JAR file for execution, run | |
* 'mvn clean package' on the command line. | |
* | |
* <p>If you change the name of the main class (with the public static void main(String[] args)) | |
* method, change the respective entry in the POM.xml file (simply search for 'mainClass'). | |
*/ | |
public class StreamingJob { | |
public static void main(String[] args) throws Exception { | |
final ParameterTool params = ParameterTool.fromArgs(args); | |
// set up the execution environment | |
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); | |
// get input data | |
DataStream<String> text; | |
// read the text file from given input path | |
text = env.readTextFile(params.get("input")); | |
// make parameters available in the web interface | |
env.getConfig().setGlobalJobParameters(params); | |
final int windowSize = params.getInt("window", 3); | |
final int slideSize = params.getInt("slide", 1); | |
System.out.println(windowSize); | |
System.out.println(slideSize); | |
DataStream<Tuple2<String, Integer>> counts = | |
// split up the lines in pairs (2-tuples) containing: (word,1) | |
text.flatMap(new Tokenizer()) | |
// create windows of windowSize records slided every slideSize records | |
.keyBy(value -> value.f0) | |
// .countWindow(windowSize, slideSize) | |
.countWindow((windowSize)) | |
// group by the tuple field "0" and sum up tuple field "1" | |
.sum(1); | |
// emit result | |
if (params.has("output")) { | |
counts.writeAsText(params.get("output")); | |
} else { | |
System.out.println("Printing result to stdout. Use --output to specify output path."); | |
counts.print(); | |
} | |
// execute program | |
env.execute("WindowWordCount"); | |
} | |
public static final class Tokenizer implements FlatMapFunction<String, Tuple2<String, Integer>> { | |
@Override | |
public void flatMap(String value, Collector<Tuple2<String, Integer>> out) { | |
// normalize and split the line | |
String[] tokens = value.toLowerCase().split("\\W+"); | |
// emit the pairs | |
for (String token : tokens) { | |
if (token.length() > 0) { | |
out.collect(new Tuple2<>(token, 1)); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment