1、IDEA创建java项目,并导入flink相关jar包。
说明:flink的jar包来自flink安装包。
2、Flink安装包下载
(1)flink的官网:https://flink.apache.org
(2)flink安装包下载:https://flink.apache.org/downloads.html
3、Flink的相关代码参考
flink的相关代码参考在gitHub上面
进入gitHub官网,搜索flink即出现相关参考代码:
4、flink批处理wordCount gitHub源码验证
(1)WordCount.java
package com.flink.batch;
import com.flink.util.WordCountData;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.util.Collector;
public class WordCount {
// *************************************************************************
// PROGRAM
// *************************************************************************
public static void main(String[] args) throws Exception {
final ParameterTool params = ParameterTool.fromArgs(args);
// set up the execution environment
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
// make parameters available in the web interface
env.getConfig().setGlobalJobParameters(params);
// get input data
DataSet<String> text;
if (params.has("input")) {
// read the text file from given input path
text = env.readTextFile(params.get("input"));
} else {
// get default test text data
System.out.println("Executing WordCount example with default input data set.");
System.out.println("Use --input to specify file input.");
text = WordCountData.getDefaultTextLineDataSet(env);
}
DataSet<Tuple2<String, Integer>> counts =
// split up the lines in pairs (2-tuples) containing: (word,1)
text.flatMap(new Tokenizer())
// group by the tuple field "0" and sum up tuple field "1"
.groupBy(0)
.sum(1);
// emit result
if (params.has("output")) {
counts.writeAsCsv(params.get("output"), "\n", " ");
// execute program
env.execute("WordCount Example");
} else {
System.out.println("Printing result to stdout. Use --output to specify output path.");
counts.print();
}
}
// *************************************************************************
// USER FUNCTIONS
// *************************************************************************
/**
* Implements the string tokenizer that splits sentences into words as a user-defined
* FlatMapFunction. The function takes a line (String) and splits it into
* multiple pairs in the form of "(word,1)" ({@code Tuple2<String, Integer>}).
*/
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));
}
}
}
}
}
(2)WordCountData.java
package com.flink.util;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
public class WordCountData {
public static final String[] WORDS = new String[] {
"To be, or not to be,--that is the question:--",
"Whether 'tis nobler in the mind to suffer",
"The slings and arrows of outrageous fortune",
"Or to take arms against a sea of troubles,",
"And by opposing end them?--To die,--to sleep,--",
"No more; and by a sleep to say we end",
"The heartache, and the thousand natural shocks",
"That flesh is heir to,--'tis a consummation",
"Devoutly to be wish'd. To die,--to sleep;--",
"To sleep! perchance to dream:--ay, there's the rub;",
"For in that sleep of death what dreams may come,",
"When we have shuffled off this mortal coil,",
"Must give us pause: there's the respect",
"That makes calamity of so long life;",
"For who would bear the whips and scorns of time,",
"The oppressor's wrong, the proud man's contumely,",
"The pangs of despis'd love, the law's delay,",
"The insolence of office, and the spurns",
"That patient merit of the unworthy takes,",
"When he himself might his quietus make",
"With a bare bodkin? who would these fardels bear,",
"To grunt and sweat under a weary life,",
"But that the dread of something after death,--",
"The undiscover'd country, from whose bourn",
"No traveller returns,--puzzles the will,",
"And makes us rather bear those ills we have",
"Than fly to others that we know not of?",
"Thus conscience does make cowards of us all;",
"And thus the native hue of resolution",
"Is sicklied o'er with the pale cast of thought;",
"And enterprises of great pith and moment,",
"With this regard, their currents turn awry,",
"And lose the name of action.--Soft you now!",
"The fair Ophelia!--Nymph, in thy orisons",
"Be all my sins remember'd."
};
public static DataSet<String> getDefaultTextLineDataSet(ExecutionEnvironment env) {
return env.fromElements(WORDS);
}
}
(3)运行结果
"C:\Program Files\Java\jdk1.8.0_91\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.1\lib\idea_rt.jar=55160:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jpcap.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jxl.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jpcap.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;E:\bigdata\flink\offlineFlink\out\production\offlineFlink;E:\bigdata\flink\offlineFlink\lib\flink-dist_2.11-1.7.2.jar;E:\bigdata\flink\offlineFlink\lib\flink-python_2.11-1.7.2.jar;E:\bigdata\flink\offlineFlink\lib\flink-shaded-hadoop2-uber-1.7.2.jar;E:\bigdata\flink\offlineFlink\lib\log4j-1.2.17.jar;E:\bigdata\flink\offlineFlink\lib\slf4j-log4j12-1.7.15.jar" com.flink.batch.WordCount
Executing WordCount example with default input data set.
Use --input to specify file input.
Printing result to stdout. Use --output to specify output path.
log4j:WARN No appenders could be found for logger (org.apache.flink.api.java.ExecutionEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
(bourn,1)
(coil,1)
(come,1)
(d,4)
(dread,1)
(is,3)
(long,1)
(make,2)
(more,1)
(must,1)
(no,2)
(oppressor,1)
(pangs,1)
(perchance,1)
(sicklied,1)
(something,1)
(takes,1)
(these,1)
(us,3)
(what,1)
(with,3)
(a,5)
(contumely,1)
(die,2)
(flesh,1)
(himself,1)
(his,1)
(hue,1)
(in,3)
(mind,1)
(now,1)
(or,2)
(orisons,1)
(rather,1)
(regard,1)
(resolution,1)
(rub,1)
(so,1)
(sweat,1)
(thought,1)
(undiscover,1)
(wish,1)
(you,1)
(against,1)
(arms,1)
(awry,1)
(bodkin,1)
(end,2)
(heartache,1)
(insolence,1)
(love,1)
(might,1)
(moment,1)
(mortal,1)
(name,1)
(not,2)
(office,1)
(pale,1)
(puzzles,1)
(s,5)
(sea,1)
(the,22)
(those,1)
(thousand,1)
(thus,2)
(troubles,1)
(unworthy,1)
(but,1)
(devoutly,1)
(dream,1)
(enterprises,1)
(fair,1)
(fly,1)
(for,2)
(fortune,1)
(grunt,1)
(life,2)
(may,1)
(others,1)
(remember,1)
(respect,1)
(spurns,1)
(take,1)
(to,15)
(we,4)
(weary,1)
(when,2)
(whips,1)
(wrong,1)
(after,1)
(and,12)
(arrows,1)
(ay,1)
(be,4)
(cast,1)
(country,1)
(er,1)
(great,1)
(know,1)
(merit,1)
(my,1)
(nobler,1)
(of,15)
(off,1)
(pith,1)
(say,1)
(scorns,1)
(shocks,1)
(this,2)
(thy,1)
(traveller,1)
(turn,1)
(death,2)
(delay,1)
(does,1)
(dreams,1)
(ills,1)
(quietus,1)
(sleep,5)
(slings,1)
(suffer,1)
(time,1)
(under,1)
(who,2)
(whose,1)
(action,1)
(bare,1)
(calamity,1)
(despis,1)
(he,1)
(law,1)
(makes,2)
(nymph,1)
(o,1)
(ophelia,1)
(opposing,1)
(outrageous,1)
(proud,1)
(returns,1)
(shuffled,1)
(than,1)
(them,1)
(tis,2)
(whether,1)
(all,2)
(bear,3)
(by,2)
(conscience,1)
(consummation,1)
(cowards,1)
(currents,1)
(fardels,1)
(from,1)
(give,1)
(have,2)
(heir,1)
(lose,1)
(man,1)
(native,1)
(natural,1)
(patient,1)
(pause,1)
(question,1)
(sins,1)
(soft,1)
(that,7)
(their,1)
(there,2)
(will,1)
(would,2)