-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Feature][Flink] Define FlinkStreamingJob Java lifecycle base class #4436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
och5351
wants to merge
1
commit into
apache:dev
Choose a base branch
from
och5351:feature/convert-java-flinkstreamingjob
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| ~ Licensed to the Apache Software Foundation (ASF) under one or more | ||
| ~ contributor license agreements. See the NOTICE file distributed with | ||
| ~ this work for additional information regarding copyright ownership. | ||
| ~ The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| ~ (the "License"); you may not use this file except in compliance with | ||
| ~ the License. You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.apache.streampark</groupId> | ||
| <artifactId>streampark-flink</artifactId> | ||
| <version>${revision}</version> | ||
| </parent> | ||
|
|
||
| <artifactId>streampark-flink-core</artifactId> | ||
| <name>StreamPark : Flink Core</name> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.flink</groupId> | ||
| <artifactId>flink-streaming-java</artifactId> | ||
| <version>${flink.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.apache.flink</groupId> | ||
| <artifactId>flink-clients</artifactId> | ||
| <version>${flink.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.assertj</groupId> | ||
| <artifactId>assertj-core</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <profiles> | ||
| <profile> | ||
| <id>apache-release</id> | ||
| <properties> | ||
| <maven.deploy.skip>true</maven.deploy.skip> | ||
| </properties> | ||
| </profile> | ||
| </profiles> | ||
|
|
||
| </project> |
26 changes: 26 additions & 0 deletions
26
...reampark-flink-core/src/main/java/org/apache/streampark/flink/core/FlinkJobException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.streampark.flink.core; | ||
|
|
||
| /** Thrown when a {@link FlinkStreamingJob} fails during lifecycle execution. */ | ||
| public class FlinkJobException extends Exception { | ||
|
|
||
| public FlinkJobException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
...reampark-flink-core/src/main/java/org/apache/streampark/flink/core/FlinkStreamingJob.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.streampark.flink.core; | ||
|
|
||
| import org.apache.flink.api.common.JobExecutionResult; | ||
| import org.apache.flink.api.java.utils.ParameterTool; | ||
| import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
|
|
||
| public abstract class FlinkStreamingJob { | ||
|
|
||
| protected StreamExecutionEnvironment env; | ||
| protected ParameterTool parameter; | ||
| protected JobExecutionResult jobExecutionResult; | ||
|
|
||
| public final void run(String[] args) throws FlinkJobException { | ||
| try { | ||
| init(args); | ||
| ready(); | ||
| handle(); | ||
| jobExecutionResult = | ||
| env.execute(parameter.get("app.name", getClass().getSimpleName())); | ||
| destroy(); | ||
| } catch (Exception e) { | ||
| throw new FlinkJobException("Failed to run Flink job: " + getClass().getSimpleName(), e); | ||
| } | ||
| } | ||
|
|
||
| private void init(String[] args) { | ||
| this.parameter = ParameterTool.fromArgs(args); | ||
| this.env = StreamExecutionEnvironment.getExecutionEnvironment(); | ||
| configure(env, parameter); | ||
| env.getConfig().setGlobalJobParameters(parameter); | ||
| } | ||
|
|
||
| protected void configure(StreamExecutionEnvironment env, ParameterTool parameter) { | ||
| } | ||
|
|
||
| protected void ready() { | ||
| } | ||
|
|
||
| protected abstract void handle() throws FlinkJobException; | ||
|
|
||
| protected void destroy() { | ||
| } | ||
| } | ||
98 changes: 98 additions & 0 deletions
98
...park-flink-core/src/test/java/org/apache/streampark/flink/core/FlinkStreamingJobTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.streampark.flink.core; | ||
|
|
||
| import org.apache.flink.api.java.utils.ParameterTool; | ||
| import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class FlinkStreamingJobTest { | ||
|
|
||
| @Test | ||
| void lifecycleExecutesInOrder() throws Exception { | ||
| List<String> trace = new ArrayList<>(); | ||
|
|
||
| FlinkStreamingJob job = | ||
| new FlinkStreamingJob() { | ||
|
|
||
| @Override | ||
| protected void configure(StreamExecutionEnvironment env, ParameterTool parameter) { | ||
| trace.add("configure"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void ready() { | ||
| trace.add("ready"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void handle() { | ||
| trace.add("handle"); | ||
| env.fromElements(1, 2, 3).map(i -> i * 2).print(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void destroy() { | ||
| trace.add("destroy"); | ||
| } | ||
| }; | ||
|
|
||
| job.run(new String[0]); | ||
|
|
||
| assertThat(trace).containsExactly("configure", "ready", "handle", "destroy"); | ||
| assertThat(job.jobExecutionResult).isNotNull(); | ||
| } | ||
|
|
||
| static class NoOpJob extends FlinkStreamingJob { | ||
|
|
||
| @Override | ||
| protected void handle() throws FlinkJobException { | ||
| env.fromElements(1).print(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void appNameDefaultsToClassName() throws Exception { | ||
| NoOpJob job = new NoOpJob(); | ||
| job.run(new String[0]); | ||
| assertThat(job.getClass().getSimpleName()).isEqualTo("NoOpJob"); | ||
| assertThat(job.jobExecutionResult).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void parameterToolPropagatedToEnv() throws Exception { | ||
| FlinkStreamingJob job = | ||
| new FlinkStreamingJob() { | ||
|
|
||
| @Override | ||
| protected void handle() { | ||
| env.fromElements(1).print(); | ||
| } | ||
| }; | ||
|
|
||
| job.run(new String[]{"--app.name", "test-job"}); | ||
|
|
||
| assertThat(job.parameter.get("app.name")).isEqualTo("test-job"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We greatly appreciate your contribution. I have observed that the current Java implementation differs considerably from the Scala version, and that several core classes (FlinkStreamTable, FlinkTable) are not yet updated. The recommendation is to keep the original Scala module's logic unchanged and only reimplement it in Java, which would reduce the risk to a minimum. Subsequent iterations and modifications can be made thereafter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review @wolfboys !
Regarding FlinkStreamTable and FlinkTable — this PR intentionally covers only Phase 0.1 of the [Proposal] Remove Scala from StreamPark — design and migration plan #4408 roadmap, limiting the scope to FlinkStreamingJob. The remaining classes are planned for subsequent phases. I apologize if that context wasn't clear enough in the PR description. If my approach is off track, I would really appreciate your feedback.
Regarding the difference from the Scala version — the lifecycle structure (init → ready → handle → execute → destroy) follows the original. The differences are intentional: since FlinkStreamingInitializer and StreamingContext are themselves Scala components targeted for removal in later phases, the Java version uses Flink's standard APIs directly instead of depending on them. This was a deliberate decision to avoid StreamPark's own wrapper classes, but I'd love your thoughts on whether this direction is the right one.