本文目的:详细介绍 MacOS 上的 Intellij IDEA 中配置 Flink-1.8.0 的过程。
MacOS Mojave
(1)未安装 jdk 的情况
(2)已安装 jdk ,但该版本高于 jdk 1.8 的情况,必须再安装 jdk 1.8
$ java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
若未下载,可前往官网链接下载,community 版本即可。
$ mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /Users/yantong/Downloads/apache-maven-3.6.1
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.5", arch: "x86_64", family: "mac"
于官网链接上下载 Apache flink-1.8.0 Scala 2.11
解压
$ cd ~/Downloads # Go to download directory
$ tar xzf flink-*.tgz # Unpack the downloaded archive
我们将使用 Maven archetypes 来构建项目。
例如,我想创建在 IdeaProjects 下,进入该目录
$ cd IdeaProjects
$ mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Define value for property 'groupId'
时,键入如下,并回车Define value for property 'groupId': org.apache.flink
Define value for property 'artifactId':
和 Define value for property 'version' 1.0-SNAPSHOT:
信息时,键入如下,并回车Define value for property 'groupId': org.apache.flink
Define value for property 'artifactId': flink-quickstart-java
Define value for property 'version' 1.0-SNAPSHOT: : 1.8.0
Define value for property 'package' org.apache.flink: : com.panda
Confirm properties configuration:
groupId: org.apache.flink
artifactId: flink-quickstart-java
version: 1.8.0
package: com.panda
$ tree flink-quickstart-java
flink-quickstart-java
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── panda
│ └── App.java
└── test
└── java
└── com
└── panda
└── AppTest.java
因为我们需导入是 Flink-1.8.0,参考其官方链接,导入 Java 编程环境必须的依赖。官网提供了 Java 和 Scala的,按自己需要添加。下面是 Java 环境的必需依赖。
org.apache.flink
flink-java
1.8.0
provided
org.apache.flink
flink-streaming-java_2.11
1.8.0
provided
将上面这部分代码复制到 pom.xml 的里面,如图所示,复制完后,点击右下角的"Enable Auto-import"
本文的目的是搭建环境,并进行测试,所以使用来自官方文档的 WordCount 代码进行测试。在目录下新建一个 Java Class文件,并将以下代码复制进去。
package com.panda;
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.util.Collector;
public class WordCount {
public static void main(String[] args) throws Exception {
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<String> text = env.fromElements(
"Who's there?",
"I think I hear them. Stand, ho! Who's there?");
DataSet<Tuple2<String, Integer>> wordCounts = text
.flatMap(new LineSplitter())
.groupBy(0)
.sum(1);
wordCounts.print();
}
public static class LineSplitter implements FlatMapFunction<String, Tuple2<String, Integer>> {
public void flatMap(String line, Collector<Tuple2<String, Integer>> out) {
for (String word : line.split(" ")) {
out.collect(new Tuple2<String, Integer>(word, 1));
}
}
}
}
右键运行 WordCount.java 文件,明明代码没有问题,pom.xml 文件也按照官方文档的 Basic Dependencies 进行配置,为何还是报以下错误:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/flink/api/java/DataSet
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.flink.api.java.DataSet
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Process finished with exit code 1
由 Caused by: java.lang.ClassNotFoundException: org.apache.flink.api.java.DataSet 得知,缺少 Flink-1.8.0 中的 lib 包。
运行结果:
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.
(hear,1)
(ho!,1)
(them.,1)
(Stand,,1)
(I,2)
(Who's,2)
(there?,2)
(think,1)
Process finished with exit code 0
选择 JAR -> From modules with dependencies
我的 flink-1.8.0 就在 Downloads 里面,读者可根据自己的下载位置进入路径
cd ~/Downloads/flink-1.8.0
在 Terminal 中输入以下命令,启动 Flink
./bin/start-cluster.sh
打开浏览器,在地址栏输入 localhost:8081,回车,即可显示 flink 的可视化界面
首先,为了方便,把5中生成的 flink-quickstart-java.jar 复制到 flink-1.8.0 的examples中
在 Terminal 中键入
$ ./bin/flink run -c com.panda.WordCount examples/flink-quickstart-java.jar
其中,-c 表示选择的 class,因为我是将整个项目打包成 jar 包的,需要选择MainClass
在 Terminal 中显示结果如下:
Starting execution of program
(I,2)
(Stand,,1)
(Who's,2)
(hear,1)
(ho!,1)
(them.,1)
(there?,2)
(think,1)
Program execution finished
Job with JobID 79202112687cdbaacca520c1db6da68c has finished.
Job Runtime: 2230 ms
Accumulator Results:
- ca35e52bc9f5a07b095f6da5746cb9bf (java.util.ArrayList) [8 elements]
到这里,在 MacOS 上搭建 Flink-1.8.0 的环境基本就结束了,本篇博客已上传至我的 Github 。
本文细致地介绍了搭建、代码测试的全过程,之后我将会继续学习 Flink 的程序编写,批处理、流处理等,欢迎关注点赞,有什么问题欢迎在 Github 上与我讨论,给我留言,谢谢。