Project Template for Java

    • Maven
    • These templates help you to set up the project structure and to create the initial build files.

    The only requirements are working Maven 3.0.4 (or higher) and Java 8.x installations.

    Create Project

    Use one of the following commands to create a project:

    This allows you to name your newly created project. It will interactively ask you for the groupId, artifactId, and package name.

    There will be a new directory in your working directory. If you’ve usedthe curl approach, the directory is called quickstart. Otherwise,it has the name of your artifactId:

    The sample project is a Maven project, which contains two classes: StreamingJob and BatchJob are the basic skeleton programs for a DataStream and DataSet program.The main method is the entry point of the program, both for in-IDE testing/execution and for proper deployments.

    We recommend you import this project into your IDE to develop andtest it. IntelliJ IDEA supports Maven projects out of the box.If you use Eclipse, the allows to import Maven projects.Some Eclipse bundles include that plugin by default, others require youto install it manually.

    Please note: The default JVM heapsize for Java may be toosmall for Flink. You have to manually increase it.In Eclipse, choose Run Configurations -> Arguments and write into the VM Arguments box: -Xmx800m.In IntelliJ IDEA recommended way to change JVM options is from the Help | Edit Custom VM Options menu. See for details.

    Build Project

    Note: If you use a different class than StreamingJob as the application’s main class / entry point,we recommend you change the mainClass setting in the pom.xml file accordingly. That way, Flinkcan run the application from the JAR file without additionally specifying the main class.

    The only requirements are working Gradle 3.x (or higher) and Java 8.x installations.

    Create Project

    Use one of the following commands to create a project:

    1. buildscript {
    2. repositories {
    3. jcenter() // this applies only to the Gradle 'Shadow' plugin
    4. }
    5. dependencies {
    6. classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
    7. }
    8. }
    9. plugins {
    10. id 'java'
    11. id 'application'
    12. // shadow plugin to produce fat JARs
    13. id 'com.github.johnrengelman.shadow' version '2.0.4'
    14. }
    15. // artifact properties
    16. group = 'org.myorg.quickstart'
    17. version = '0.1-SNAPSHOT'
    18. mainClassName = 'org.myorg.quickstart.StreamingJob'
    19. ext {
    20. javaVersion = '1.8'
    21. flinkVersion = '1.9.0'
    22. scalaBinaryVersion = '2.11'
    23. slf4jVersion = '1.7.7'
    24. log4jVersion = '1.2.17'
    25. }
    26. targetCompatibility = javaVersion
    27. tasks.withType(JavaCompile) {
    28. options.encoding = 'UTF-8'
    29. }
    30. applicationDefaultJvmArgs = ["-Dlog4j.configuration=log4j.properties"]
    31. task wrapper(type: Wrapper) {
    32. gradleVersion = '3.1'
    33. }
    34. // declare where to find the dependencies of your project
    35. repositories {
    36. mavenCentral()
    37. maven { url "https://repository.apache.org/content/repositories/snapshots/" }
    38. }
    39. // NOTE: We cannot use "compileOnly" or "shadow" configurations since then we could not run code
    40. // in the IDE or with "gradle run". We also cannot exclude transitive dependencies from the
    41. // shadowJar yet (see https://github.com/johnrengelman/shadow/issues/159).
    42. // -> Explicitly define the // libraries we want to be included in the "flinkShadowJar" configuration!
    43. configurations {
    44. flinkShadowJar // dependencies which go into the shadowJar
    45. // always exclude these (also from transitive dependencies) since they are provided by Flink
    46. flinkShadowJar.exclude group: 'org.apache.flink', module: 'force-shading'
    47. flinkShadowJar.exclude group: 'com.google.code.findbugs', module: 'jsr305'
    48. flinkShadowJar.exclude group: 'org.slf4j'
    49. flinkShadowJar.exclude group: 'log4j'
    50. }
    51. // declare the dependencies for your production and test code
    52. dependencies {
    53. // Compile-time dependencies that should NOT be part of the
    54. // shadow jar and are provided in the lib folder of Flink
    55. // --------------------------------------------------------------
    56. compile "org.apache.flink:flink-java:${flinkVersion}"
    57. compile "org.apache.flink:flink-streaming-java_${scalaBinaryVersion}:${flinkVersion}"
    58. // --------------------------------------------------------------
    59. // Dependencies that should be part of the shadow jar, e.g.
    60. // connectors. These must be in the flinkShadowJar configuration!
    61. // --------------------------------------------------------------
    62. compile "log4j:log4j:${log4jVersion}"
    63. compile "org.slf4j:slf4j-log4j12:${slf4jVersion}"
    64. // Add test dependencies here.
    65. // testCompile "junit:junit:4.12"
    66. }
    67. // make compileOnly dependencies available for tests:
    68. sourceSets {
    69. main.compileClasspath += configurations.flinkShadowJar
    70. main.runtimeClasspath += configurations.flinkShadowJar
    71. test.compileClasspath += configurations.flinkShadowJar
    72. test.runtimeClasspath += configurations.flinkShadowJar
    73. javadoc.classpath += configurations.flinkShadowJar
    74. }
    75. run.classpath = sourceSets.main.runtimeClasspath
    76. jar {
    77. manifest {
    78. attributes 'Built-By': System.getProperty('user.name'),
    79. 'Build-Jdk': System.getProperty('java.version')
    80. }
    81. }
    82. shadowJar {
    83. configurations = [project.configurations.flinkShadowJar]
    84. }
    1. bash -c "$(curl https://flink.apache.org/q/gradle-quickstart.sh)" -- 1.9.0 2.11

    This allows you to name your newly created project. It will interactively ask you for the project name, organization (also used for the package name), project version, Scala and Flink version.

    There will be a new directory in your working directory based on theproject name you provided, e.g. for quickstart:

    The sample project is a Gradle project, which contains two classes: StreamingJob and BatchJob are the basic skeleton programs for a DataStream and DataSet program.The main method is the entry point of the program, both for in-IDE testing/execution and for proper deployments.

    We recommend you import this project into your IDE to develop andtest it. IntelliJ IDEA supports Gradle projects after installing the Gradle plugin.Eclipse does so via the Eclipse Buildship plugin(make sure to specify a Gradle version >= 3.0 in the last step of the import wizard; the shadow plugin requires it).You may also use to create project files from Gradle.

    Build Project

    If you want to build/package your project, go to your project directory andrun the ‘gradle clean shadowJar’ command.You will find a JAR file that contains your application, plus connectors and librariesthat you may have added as dependencies to the application: build/libs/<project-name>-<version>-all.jar.

    Note: If you use a different class than StreamingJob as the application’s main class / entry point,we recommend you change the mainClassName setting in the file accordingly. That way, Flinkcan run the application from the JAR file without additionally specifying the main class.

    Write your application!

    If you are writing a streaming application and you are looking for inspiration what to write,take a look at the .

    If you are writing a batch processing application and you are looking for inspiration what to write,take a look at the Batch Application Examples.

    For a complete overview over the APIs, have a look at the andDataSet API sections.

    you can find out how to run an application outside the IDE on a local cluster.