Data Processing Using Flink SQL
Scenarios
Flink jobs can use other services as data sources and sinks for real-time data computing.
This example describes how to create and submit a Flink OpenSource SQL job for real-time computing using Kafka as the data source and RDS as the data sink.
Procedure
Before using the following sample, complete the necessary preparation steps.
- Preparing a Data Source: Create a Kafka instance to serve as the data source.
- Preparing a Data Sink: Create an RDS instance to serve as the data sink.
- Planning and Creating an OBS 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 FlinkJob 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 enabled LakeFormation and created an instance.
- You have enabled Kafka data ingestion.
- You have obtained OBS permissions and configured the required agency.
- You have obtained an IAM user token. For details, see Obtaining an IAM User Token.
Step 1: Prepare a Data Source
In this example, Kafka is the data source.
- Create the resources required by Kafka.
Before creating a Kafka instance, prepare the required resources (including a VPC, a subnet, and a security group) and configure the security group.
- For details on how to create a VPC and subnet, see Creating a VPC with a Subnet. If you need to create and use a new subnet in an existing VPC, see Creating a Subnet for an Existing VPC.
- For details about creating a security group, see Creating a Security Group. To add rules to a security group, see Adding a Security Group Rule.
For details, see Preparing Required Resources in the Distributed Message Service for Kafka User Guide.
- The created VPC and the Kafka instance you will create must be in the same region.
- When creating a VPC and subnet, keep the default settings unless specified otherwise.
- Enable Kafka data ingestion. For details, see Buying a Kafka Instance.
- Click the purchased Kafka instance name. On the page that appears, select Topic Management > Create Topic to create a topic.
Step 2: Prepare a Data Sink
Use RDS as the data sink, and create an RDS for MySQL instance.
- Create an RDS for MySQL instance. For details, see Buying an RDS for MySQL Instance.
- Log in to the RDS for MySQL instance and create a database named flink. For details, see Creating a Database.
- Create the orders table in the flink database.
Log in to the RDS for MySQL instance and click the flink database. On the page that appears, click SQL Window. Enter the following statement in the SQL editor to create the table:
CREATE TABLE `flink`.`orders` ( `order_id` VARCHAR(32) NOT NULL, `order_channel` VARCHAR(32) NULL, `order_time` VARCHAR(32) NULL, `pay_amount` DOUBLE UNSIGNED NOT NULL, `real_pay` DOUBLE UNSIGNED NULL, `pay_time` VARCHAR(32) NULL, `user_id` VARCHAR(32) NULL, `user_name` VARCHAR(32) NULL, `area_id` VARCHAR(32) NULL, PRIMARY KEY (`order_id`) ) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
Step 3: Plan and Create an OBS Parallel File System
AI DataLake Flink uses OBS to submit jobs and store data. You must create a parallel file system on the OBS console, and then import sample data or job scripts.
- 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 SQL script is kafkaSource.sql, and its content is as follows:
CREATE TABLE kafkaSource ( order_id string, order_channel string, order_time string, pay_amount double, real_pay double, pay_time string, user_id string, user_name string, area_id string ) WITH ( 'connector' = 'kafka', 'topic' = 'testkafkatopic', -- Topic of the Kafka data source. Log in to the Kafka console, click the created Kafka instance name, and view the topic name on the Topic Management page. 'properties.bootstrap.servers' = '192.168.x.xxx:xx92', -- Replace it with the private IP address and port number of the Kafka instance. 'properties.group.id' = 'GroupId', 'scan.startup.mode' = 'latest-offset', 'format' = 'json' ); CREATE TABLE jdbcSink ( order_id string, order_channel string, order_time string, pay_amount double, real_pay double, pay_time string, user_id string, user_name string, area_id string, PRIMARY KEY (order_id) NOT ENFORCED ) WITH ( 'connector' = 'jdbc', 'url' = 'jdbc:mysql://172.16.x.xxx:xx06/testrdsdb', -- testrdsdb is the name of the created RDS database. Replace the IP address and port number with those of your RDS for MySQL instance. 'table-name' = 'orders', 'username' = 'root', 'password' = '<your_database_password>', 'sink.buffer-flush.max-rows' = '1' ); INSERT INTO jdbcSink SELECT * FROM kafkaSource;
Step 4: 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 5: 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 6: 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-sql-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_SQL_JOB", "sql_script_file": "obs://aidatalake/sqls/kafkaSource.sql" }, "logging_config": { "root_logger_level": "INFO" } } - Connect to the Kafka cluster and send the following test data to the target Kafka topic:
{"order_id":"202607101000000001", "order_channel":"webShop", "order_time":"2026-07-10 10:00:00", "pay_amount":100.00, "real_pay":100.00, "pay_time":"2026-07-10 10:02:03", "user_id":"0001", "user_name":"Alice", "area_id":"330106"} {"order_id":"202607101606060001", "order_channel":"appShop", "order_time":"2026-07-10 16:06:06", "pay_amount":200.00, "real_pay":180.00, "pay_time":"2026-07-10 16:10:06", "user_id":"0001", "user_name":"Alice", "area_id":"330106"} - Run the following SQL statement in the MySQL database to view the table data:
select * from orders;
The expected result is as follows:
202607101000000001,webShop,2026-07-10 10:00:00,100,100,2026-07-10 10:02:03,0001,Alice,330106 202607101606060001,appShop,2026-07-10 16:06:06,200,180,2026-07-10 16:10:06,0001,Alice,330106
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