Data Processing Using Flink JAR
Scenarios
Flink JAR jobs are suitable for data analysis scenarios that require custom stream processing logic, complex state management, or integration with specific libraries. You must write and build a JAR job package. Before submitting a Flink JAR job, upload the JAR package to OBS, and then submit it along with the required data and job parameters to run the job.
This section guides you through the process of running a Flink JAR job package.
Procedure
Before using the following sample, complete the necessary preparation steps.
- Planning and Creating a Parallel File System: Create an OBS parallel file system to store job scripts.
- Creating a Workspace: Create a workspace.
- Creating a FlinkJob Endpoint for Running Jobs: Create a Flink endpoint to run jobs.
- Submitting a Job via an API and Viewing Execution Results: Send an API request to the Flink endpoint to submit and execute an AI DataLake job.
Preparations
- You have registered an account and completed real-name authentication. The account balance is sufficient, and the account is not frozen.
- You have enabled AI DataLake and obtained the required permissions for the cloud resources.
- You have obtained OBS permissions and configured the required agency.
- You have obtained an IAM token. For details, see Obtaining an IAM User Token.
Step 1: Plan and Create a Parallel File System
- Create a parallel file system.
- On the Parallel File Systems page, click the name of the created file system.
- On the Files tab, click Upload File to upload the job script to the created OBS parallel file system.
For example, the Flink Java code file is WindowJoin.java. A complete example is as follows:
package org.apache.flink.streaming.examples.join; import org.apache.flink.api.common.eventtime.WatermarkStrategy; import org.apache.flink.api.common.functions.JoinFunction; import org.apache.flink.api.common.serialization.SimpleStringEncoder; import org.apache.flink.api.java.tuple.Tuple3; import org.apache.flink.connector.file.sink.FileSink; import org.apache.flink.core.fs.Path; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy; import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows; import org.apache.flink.streaming.api.windowing.time.Time; import java.time.Duration; public class WindowJoin { public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); String outputPath = null; for (int i = 0; i < args.length - 1; i++) { if ("--output".equals(args[i])) { outputPath = args[i + 1]; break; } } DataStream<Tuple3<String, Integer, Long>> orderStream = env .fromElements( Tuple3.of("order1", 100, 1000L), Tuple3.of("order2", 200, 2000L), Tuple3.of("order3", 150, 3000L), Tuple3.of("order4", 300, 4000L) ) .assignTimestampsAndWatermarks( WatermarkStrategy .<Tuple3<String, Integer, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(1)) .withTimestampAssigner((event, ts) -> event.f2) ); DataStream<Tuple3<String, String, Long>> paymentStream = env .fromElements( Tuple3.of("order1", "credit", 1500L), Tuple3.of("order2", "debit", 2500L), Tuple3.of("order3", "credit", 3500L), Tuple3.of("order4", "debit", 4500L) ) .assignTimestampsAndWatermarks( WatermarkStrategy .<Tuple3<String, String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(1)) .withTimestampAssigner((event, ts) -> event.f2) ); DataStream<String> result = orderStream .join(paymentStream) .where(order -> order.f0) .equalTo(payment -> payment.f0) .window(TumblingEventTimeWindows.of(Time.seconds(5))) .apply( (JoinFunction<Tuple3<String, Integer, Long>, Tuple3<String, String, Long>, String>) (order, payment) -> "orderId=" + order.f0 + ", amount=" + order.f1 + ", payMethod=" + payment.f1 ); if (outputPath != null) { FileSink<String> sink = FileSink .forRowFormat(new Path(outputPath), new SimpleStringEncoder<String>("UTF-8")) .withRollingPolicy(OnCheckpointRollingPolicy.build()) .build(); result.sinkTo(sink); } else { result.print(); } env.execute("WindowJoin QuickStart"); } }A complete example of the Flink Java pom.xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>org.example</groupId> <artifactId>window-join</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <flink.version>1.20.0</flink.version> </properties> <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.apache.flink</groupId> <artifactId>flink-connector-files</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>org.apache.flink.streaming.examples.join.WindowJoin</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Step 2: Create a Workspace
- Log in to the AI DataLake console.
- In the left navigation pane, click the workspace area.
- Select Create Workspace from the drop-down list.
- On the Create Workspace page, set the following workspace parameters.
Table 1 Basic workspace information Category
Parameter
Mandatory
Description
Basic Information
Workspace Name
Yes
The name of the workspace to create. Workspace names must be unique within the same account.
- The name can contain only letters, digits, hyphens (-), and underscores (_).
- The name must be 4 to 32 characters long.
The workspace name is case-insensitive. Uppercase letters are automatically converted to lowercase.
Description
No
A brief description of the workspace. It cannot exceed 255 characters.
Enterprise Project
No
If the workspace you are creating belongs to an enterprise project, select that enterprise project.
EPS provides enterprise projects that group cloud resources by project to manage resources and users within specific projects.
For details about how to configure enterprise projects, see Enterprise Management User Guide.
NOTE:This parameter appears only for users with the EPS service enabled.
Multimodal Data Management
LakeFormation Instance
Yes
Select the LakeFormation instance associated with the current workspace. Each workspace must be associated with one LakeFormation instance. You cannot change this instance after the workspace is created.
- Select a LakeFormation instance to centrally manage metadata and control data access.
- If no instance is available, first create a LakeFormation instance.
- Verify that all configurations are correct.
Click Create Now to create the workspace.
After the workspace is created, you can view it in the workspace management list.
Step 3: Create a FlinkJob Endpoint for Running Jobs
- Configure the basic information for the endpoint.
Table 2 Basic endpoint information Parameter
Example Value
Description
Endpoint Type
FlinkJob
Set the endpoint type to FlinkJob.
Endpoint Name
flink-job-01
The endpoint name cannot be changed after the endpoint is created.
The endpoint name is a unique identifier within the system and is used in API calls and CLI commands.
Naming rules:
- The name can contain only lowercase letters, digits, and hyphens (-), and must start with a letter and end with a letter or digit.
- The name can contain a maximum of 63 characters.
Example: flink-endpoint-001 and my-flink-job
Endpoint Display Name
Dedicated FlinkJob endpoint for data analysis team-01
The endpoint display name can be changed after the endpoint is created.
The endpoint display name appears on the console to help users identify the endpoint.
Naming rules:
- The name can contain only letters, digits, and hyphens (-).
- The name can contain a maximum of 63 characters.
Example: Flink engine in the production environment and endpoint dedicated to data analysts
Add Description
Dedicated to the data analysis team
Enter a description to specify the purpose and background of the endpoint, helping other users understand its application scenarios.
- Configure the resource settings for the endpoint.
Table 3 Endpoint resource settings Parameter
Example Value
Description
Resource Allocation
Reserved
Select a resource allocation mode.
- Reserved: Uses reserved resource pools to provide exclusive resources. This mode is ideal for stable, continuous workloads, such as production projects and critical tasks.
- Hybrid: Provides baseline stability and automatic scaling. Reserved resources are used first, and auto scaling is triggered automatically during peak hours. This mode is best for workloads with predictable fluctuations.
- On-demand: Charges you based on the actual time taken to execute each task. No costs are incurred when no tasks are running. This mode is well-suited for short-term or ad-hoc tasks, including development, testing, and temporary projects.
For details about billing, see Billing Overview.
- Configure the storage settings for the endpoint.
Table 4 Storage settings Parameter
Example Value
Description
Temporary File Storage
aidatalake-obs
Select an existing parallel file system from the drop-down list. This file system is used to store job logs, temporary data, checkpoints, savepoints, and HA data.
The OBS parallel file system cannot be changed after the endpoint is created. If no file system is available, click Create OBS parallel file system to create one.
Agency
aidatalake-agency-job
Configure an IAM agency to grant read and write access to job logs, temporary data, checkpoints, savepoints, and HA data.
Select an existing agency from the drop-down list. The agency cannot be changed after the endpoint is created. If no agency is available, click Create agency to create one. For details, see Creating a Custom Agency.
- Configure the network settings.
If the resource allocation mode is set to On-demand and no network exists in the workspace, you must create a network and select it for the endpoint. Each workspace supports only one network.
- Configure the job agency settings for the endpoint.
Table 5 Job agency settings Parameter
Example Value
Description
Job Agency Settings
aidatalake-agency-job
Agency-assigned endpoint permissions are automatically converted to credentials and updated in the Flink cluster at runtime. You can use agency credentials in the Flink cluster to access the required services. Before using the Flink engine to run jobs, you must create the required agency on the IAM console and select it here. For details, see Creating a Custom Agency.
- After completing the configuration, click Create Now. The system will begin creating the endpoint.
Once the endpoint is created, you can view its details in the endpoint list. After the status changes to Running, you can submit jobs to the endpoint.
Step 4: Submit a Job via an API and View Execution Results
- Use a tool such as Postman to call the job submission API and submit a job.
The following is an example API request for submitting a job:
POST /v2/workspaces/{workspace_id}/flink-jobs { "name": "flink-jar-job", "endpoint_name": "flink-job-01", "execution_mode": "STREAMING", "resource_config": { "parallelism": 1, "jobmanager_resource_spec": { "cpu": 500, "memory": 2048, "disk": 30 }, "taskmanager_resource_spec": { "cpu": 500, "memory": 2048, "disk": 30 } }, "job_config": { "type": "FLINK_JAR_JOB", "main_jar": "obs://aidatalake/jars/WindowJoin.jar", "main_class": "org.apache.flink.streaming.examples.join.WindowJoin", "main_args": [ "--output", "obs://aidatalake/jars/tmp/" ] }, "logging_config": { "root_logger_level": "INFO" } } - Locate the obs://aidatalake/jars/tmp/2026-xx-xx--18/part-7efbxxxx-bbe7-xxxx-8482-ecaeca0cb6e9-0 file in the OBS parallel file system to view the JAR job results.
The expected result is as follows:
orderId=order1, amount=100, payMethod=credit orderId=order4, amount=300, payMethod=debit orderId=order2, amount=200, payMethod=debit orderId=order3, amount=150, payMethod=credit
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot