Skip to content

Latest commit

 

History

History
124 lines (95 loc) · 6.33 KB

java_api_quickstart.md

File metadata and controls

124 lines (95 loc) · 6.33 KB
title nav-title nav-parent_id nav-pos
Java 项目模板
Java 项目模板
start
0
  • This will be replaced by the TOC {:toc}

通过简单地几步来开始编写你的 Flink Java 程序。

要求

唯一的要求是安装 Maven 3.0.4(或更高版本)和 Java 8.x

创建项目

使用下面的其中一个命令来 创建项目

{% highlight bash %} $ mvn archetype:generate \ -DarchetypeGroupId=org.apache.flink \ -DarchetypeArtifactId=flink-quickstart-java \{% unless site.is_stable %} -DarchetypeCatalog=https://repository.apache.org/content/repositories/snapshots/ \{% endunless %} -DarchetypeVersion={{site.version}} {% endhighlight %} 这种方式允许你为新建的项目命名。将以交互式地方式询问你为 groupId,artifactId 以及 package 命名。
{% highlight bash %} {% if site.is_stable %} $ curl https://flink.apache.org/q/quickstart.sh | bash -s {{site.version}} {% else %} $ curl https://flink.apache.org/q/quickstart-SNAPSHOT.sh | bash -s {{site.version}} {% endif %} {% endhighlight %}
</div>
{% unless site.is_stable %}
<p style="border-radius: 5px; padding: 5px" class="bg-danger">
    <b>Note</b>: For Maven 3.0 or higher, it is no longer possible to specify the repository (-DarchetypeCatalog) via the command line. If you wish to use the snapshot repository, you need to add a repository entry to your settings.xml. For details about this change, please refer to <a href="http://maven.apache.org/archetype/maven-archetype-plugin/archetype-repository.html">Maven official document</a>
    <b>注意</b>: 对于 Maven 3.0 及更高版本,不再可以通过命令行指定仓库 (-DarchetypeCatalog)。如果你想使用快照仓库,则需要在setting.xml里添加一个仓库配置。对于这个改变的详情,请参阅 <a href="http://maven.apache.org/archetype/maven-archetype-plugin/archetype-repository.html">Maven 官方文档</a>
</p>
{% endunless %}

检查项目

在你的工作目录下将会有一个新的目录。如果你试用 curl 来创建,这个目录的名字为 quickstart。否则名字为你命名的 artifactId

{% highlight bash %} $ tree quickstart/ quickstart/ ├── pom.xml └── src └── main ├── java │   └── org │   └── myorg │   └── quickstart │   ├── BatchJob.java │   └── StreamingJob.java └── resources └── log4j.properties {% endhighlight %}

示例项目是一个 Maven 项目,包含两个类: StreamingJobBatchJobDataStreamDataSet 的基本框架程序。main 方法是程序的入口,既可以用 IDE 测试/执行,也可用于部署。

我们建议你 把这个项目导入到你的 IDE 来开发和测试一下。IntelliJ IDEA 支持开箱即用的 Maven 项目。如果您使用 Eclipse,使用 m2e 插件可以导入 Maven 项目。 一些 Eclipse 默认捆绑该插件,其他的需要手动安装。

给 Mac OS X 用户的说明:给 Flink 分配的默认 JVM 堆内存太小。你需要手动增加堆内存。在 Eclipse中,选择 Run Configurations -> Arguments 并且写入 VM Arguments 框中:-Xmx800m 。 在 IntelliJ IDEA 中建议通过菜单 Help | Edit Custom VM Options 改变 JVM 的选项。 详情见 这篇文章

构建项目

如果你想 构建/打包你的项目,进入到你的项目目录并且执行 'mvn clean package' 命令。 你将 找到一个 JAR 文件,包含了您的应用、扩展的 connectors 以及程序依赖的Libraries到您的应用程序:target/<artifact-id>-<version>.jar

注意: 如果您使用和 StreamingJob 不同的类作为应用程序的主类/入口,我们建议您相应的修改 pom.xml 中的 mainClass 设置。那样,Flink 在运行应用程序的 JAR 文件的时候不需要再指定主类。

下一步

编写您的应用!

如果您正在编写流处理应用程序,并且您在寻找编写的灵感,可以看看 [流处理应用程序教程]({{ site.baseurl }}/quickstart/run_example_quickstart.html#writing-a-flink-program)。

如果您正在编写批处理的应用程序,并且您在寻找编写的灵感,可以看看 [批处理应用示例]({{ site.baseurl }}/dev/batch/examples.html)。

有关 API 的完整概述,请查看 [DataStream API]({{ site.baseurl }}/dev/datastream_api.html) 和 [DataSet API]({{ site.baseurl }}/dev/batch/index.html) 章节。

在[这里]({{ site.baseurl }}/quickstart/setup_quickstart.html)你可以找到怎么在 IDE 之外的集群上运行应用。

如果您有任何问题,可发邮件到邮箱列表。 我们很乐意提供帮助。

{% top %}