Skip to content

Commit

Permalink
[FLINK-1777][Documentation] Improved documentation for Java 8 Lambdas…
Browse files Browse the repository at this point in the history
… in Eclipse

This closes apache#528
  • Loading branch information
twalthr authored and rmetzger committed Apr 2, 2015
1 parent 359b39c commit 57e9ae0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
43 changes: 39 additions & 4 deletions docs/java8_programming_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ env.fromElements(1, 2, 3)
.print();
~~~

The next two example show different implementations of a function that uses a `Collector` for output.
The next two examples show different implementations of a function that uses a `Collector` for output.
Functions, such as `flatMap()`, require a output type (in this case `String`) to be defined for the `Collector` in order to be type-safe.
If the `Collector` type can not be inferred from the surrounding context, it need to be declared in the Lambda Expression's parameter list manually.
Otherwise the output will be treated as type `Object` which can lead to undesired behaviour.
Expand Down Expand Up @@ -96,7 +96,7 @@ input.filter(line -> !line.contains("not"))
~~~

### Compiler Limitations
Currently, Flink only supports jobs containing Lambda Expressions completely if they are **compiled with the Eclipse JDT compiler**.
Currently, Flink only supports jobs containing Lambda Expressions completely if they are **compiled with the Eclipse JDT compiler contained in Eclipse Luna 4.4.2 (and above)**.

Only the Eclipse JDT compiler preserves the generic type information necessary to use the entire Lambda Expressions feature type-safely.
Other compilers such as the OpenJDK's and Oracle JDK's `javac` throw away all generic parameters related to Lambda Expressions. This means that types such as `Tuple2<String,Integer` or `Collector<String>` declared as a Lambda function input or output parameter will be pruned to `Tuple2` or `Collector` in the compiled `.class` files, which is too little information for the Flink Compiler.
Expand All @@ -105,9 +105,9 @@ How to compile a Flink job that contains Lambda Expressions with the JDT compile

However, it is possible to implement functions such as `map()` or `filter()` with Lambda Expressions in Java 8 compilers other than the Eclipse JDT compiler as long as the function has no `Collector`s or `Iterable`s *and* only if the function handles unparameterized types such as `Integer`, `Long`, `String`, `MyOwnClass` (types without Generics!).

#### Compile Flink jobs with the Eclipse JDT compiler
#### Compile Flink jobs with the Eclipse JDT compiler and Maven

If you are using the Eclipse IDE, you can run and debug your Flink code within the IDE without any problems. The Eclipse IDE by default compiles its Java sources with the Eclipse JDT compiler.
If you are using the Eclipse IDE, you can run and debug your Flink code within the IDE without any problems after some configuration steps. The Eclipse IDE by default compiles its Java sources with the Eclipse JDT compiler. The next section describes how to configure the Eclipse IDE.

If you are using a different IDE such as IntelliJ IDEA or you want to package your Jar-File with Maven to run your job on a cluster, you need to modify your project's `pom.xml` file and build your program with Maven. The [quickstart](setup_quickstart.html) contains preconfigured Maven projects which can be used for new projects or as a reference. Uncomment the mentioned lines in your generated quickstart `pom.xml` file if you want to use Java 8 with Lambda Expressions.

Expand Down Expand Up @@ -147,10 +147,45 @@ If you are using Eclipse for development, the m2e plugin might complain about th
<versionRange>[3.1,)</versionRange>
<goals>
<goal>testCompile</goal>
<goal>compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
~~~

#### Run and debug Flink jobs within the Eclipse IDE

First of all, make sure you are running a current version of Eclipse IDE (4.4.2 or later). Also make sure that you have a Java 8 Runtime Environment (JRE) installed in Eclipse IDE (`Window` -> `Preferences` -> `Java` -> `Installed JREs`).

Create/Import your Eclipse project.

If you are using Maven, you also need to change the Java version in your `pom.xml` for the `maven-compiler-plugin`. Otherwise right click the `JRE System Library` section of your project and open the `Properties` window in order to switch to a Java 8 JRE (or above) that supports Lambda Expressions.

The Eclipse JDT compiler needs a special compiler flag in order to store type information in `.class` files. Open the JDT configuration file at `{project directoy}/.settings/org.eclipse.jdt.core.prefs` with your favorite text editor and add the following line:

~~~
org.eclipse.jdt.core.compiler.codegen.lambda.genericSignature=generate
~~~

If not already done, also modify the Java versions of the following properties to `1.8` (or above):

~~~
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.source=1.8
~~~

After you have saved the file, perform a complete project refresh in Eclipse IDE.

If you are using Maven, right click your Eclipse project and select `Maven` -> `Update Project...`.

You have configured everything correctly, if the following Flink program runs without exceptions:

~~~java
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
env.fromElements(1, 2, 3).map((in) -> new Tuple1<String>(" " + in)).print();
env.execute();
~~~
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ under the License.
<versionRange>[3.1,)</versionRange>
<goals>
<goal>testCompile</goal>
<goal>compile</goal>
</goals>
</pluginExecutionFilter>
<action>
Expand Down

0 comments on commit 57e9ae0

Please sign in to comment.