This article demonstrates how to build a Paimon data lake analytics pipeline using Realtime Compute for Apache Flink and EMR Serverless Spark. The process includes writing data to Object Storage Service (OSS), running interactive queries, and performing offline data compaction. EMR Serverless Spark is fully compatible with Paimon. It interoperates with other cloud products, such as Realtime Compute for Apache Flink, through a built-in Data Lake Formation (DLF) metastore, providing a complete stream-batch unification solution. It supports flexible job execution and parameter configurations for various requirements in real-time analysis and production scheduling.
Background information
Realtime Compute for Apache Flink
Alibaba Cloud Realtime Compute for Apache Flink is a fully managed, serverless cloud service for Apache Flink. It provides a ready-to-use one-stop platform for development, operations, and maintenance and offers flexible billing. The service includes full lifecycle capabilities such as job development, data debugging, job execution and monitoring, automatic tuning, and intelligent diagnostics. For more information, see What is Realtime Compute for Apache Flink?.
Apache Paimon
Apache Paimon is a unified data lake storage format that uses Flink and Spark to enable a real-time lakehouse architecture for stream-batch unification. Paimon innovatively combines lakehouse formats with Log-structured Merge-tree (LSM) technology, giving the data lake real-time streaming updates and full stream processing capabilities. For more information, see Apache Paimon.
Procedure
Step 1: Create a Paimon catalog
A Paimon catalog lets you manage all Paimon tables within the same warehouse directory and connect them to other Alibaba Cloud products. For more information about how to create and use a Paimon catalog, see Manage a Paimon catalog.
-
Log on to the Realtime Compute for Apache Flink console.
-
In the Actions column of your target workspace, click Console.
-
Create a Paimon catalog.
Add a Paimon catalog of the DLF type by using the UI. You do not need to enter AccessKey information manually. For detailed instructions, see Manage a Paimon catalog.
-
Create a Paimon table.
In the Script editor, enter the following command. Then, select the code and click Run.
CREATE TABLE IF NOT EXISTS `paimon`.`test_paimon_db`.`test_append_tbl` ( id STRING, data STRING, category INT, ts STRING, dt STRING, hh STRING ) PARTITIONED BY (dt, hh) WITH ( 'write-only' = 'true' ); -
Create a stream job.
-
Create a new job.
-
In the left-side navigation pane, choose Development > ETL.
-
Create a new stream job. In the New Blank Stream Draft dialog box, configure the job parameters.
Parameter
Description
Name
The name of the job.
NoteThe job name must be unique within the current project.
Engine Version
The Flink engine version for the current job. For more information about engine version numbers, version compatibility, and lifecycle milestones, see Engine versions.
-
Click create.
-
-
Write the code.
In the new stream draft, enter the following code to continuously generate data using the datagen connector and write it to the Paimon table.
CREATE TEMPORARY TABLE datagen ( id string, data string, category int ) WITH ( 'connector' = 'datagen', 'rows-per-second' = '100', 'fields.category.kind' = 'random', 'fields.category.min' = '1', 'fields.category.max' = '10' ); INSERT INTO `paimon`.`test_paimon_db`.`test_append_tbl` SELECT id, data, category, cast(LOCALTIMESTAMP as string) as ts, cast(CURRENT_DATE as string) as dt, cast(hour(LOCALTIMESTAMP) as string) as hh FROM datagen; -
Click Deploy to publish the job to the production environment.
-
On the Deployments page, start the job. For more information, see Start a job.
-
Step 2: Create an SQL session
An SQL session supports SQL development and queries. For more information about sessions, see Session Manager.
-
Go to the Sessions page.
-
Log on to the EMR console.
-
In the left-side navigation pane, choose EMR Serverless > Spark.
-
On the Spark page, click the name of the target workspace.
-
On the EMR Serverless Spark page, click Sessions in the left-side navigation pane.
-
-
In the data catalog of your Serverless Spark workspace, add the DLF Catalog that you created in Step 1.
-
Create an SQL session.
-
On the SQL Session tab, click Connect to SQL Session.
-
On the Create SQL Session page, configure the following parameter, leave the other parameters at their defaults, and then click create.
Parameter
Description
Name
A custom name for the SQL session. For example, paimon_compute.
-
In the Actions column, click START.
-
Step 3: Run interactive queries or task orchestration
EMR Serverless Spark offers two operational modes for different needs: interactive query and task orchestration. The interactive query mode is ideal for quick queries and debugging, while the task orchestration mode supports the full lifecycle of job development, publishing, and maintenance.
While data is being written, you can use EMR Serverless Spark to run an interactive query on the Paimon table at any time to check the real-time data status and perform quick analysis. In addition, you can publish developed jobs and create a workflow to orchestrate various tasks. You can configure scheduling policies to automate tasks, ensuring efficient and automated data processing and analysis.
Interactive query
-
Create a SparkSQL job.
-
On the EMR Serverless Spark page, click Development in the left-side navigation pane.
-
On the Development tab, click Create.
-
In the dialog box that appears, enter a Name (for example, paimon_compact), set Type to SparkSQL, and then click OK.
-
In the upper-right corner, select the data catalog, database, and the SQL session that you started in the previous step.
-
Enter an SQL statement in the new job editor.
-
Example 1: Query the first 10 rows from the
test_append_tbltable.SELECT * FROM paimon.test_paimon_db.test_append_tbl limit 10;NoteReplace the catalog name, database name, and table name in the SQL statement to match your environment.
The result includes the id, data, category, ts, dt, and hh fields, where the id and data columns are hash strings, category is a numeric value, ts is a full timestamp (for example,
2024-06-24 19:00:00.446), dt is the date, and hh is the hour. -
Example 2: Count the number of rows that meet specific criteria in the
test_append_tbltable.SELECT COUNT(*) FROM paimon.test_paimon_db.test_append_tbl WHERE dt = '2024-06-24' AND hh = '19';NoteReplace the catalog name, database name, table name, and partition conditions (dt and hh) in the SQL statement to match your environment.
The query returns a single row with a
count(1)value of360000.
-
-
-
Run and publish the job.
-
Click Run.
You can view the results on the Execution Results tab. If any errors occur, check the Execution Issues tab.
-
After you confirm that the job runs as expected, click Publish in the upper-right corner.
-
In the Publish dialog box, you can enter release notes and then click OK.
-
Task orchestration
-
Check file information before compaction.
On the Development page, create a SparkSQL job to query the Paimon $files system table to quickly retrieve data about the files before compaction. For more information about how to create a SparkSQL job, see Develop a SparkSQL job.
SELECT file_path, record_count, file_size_in_bytes FROM paimon.test_paimon_db.`test_append_tbl$files`;NoteReplace the catalog name and database name in the SQL statement to match your environment.
Before compaction, the query result shows 14 ORC data files. Most files have a
record_countof 18,100 (some are 18,200, and the first is 8,700), andfile_size_in_bytesvalues range from approximately 1,752,386 to 3,664,183. -
In the SparkSQL job that you created (paimon_compact), enter the following compaction SQL, and then publish the job.
CALL paimon.sys.compact ( table => 'test_paimon_db.test_append_tbl', partitions => 'dt=\"2024-06-24\",hh=\"19\"', order_strategy => 'zorder', order_by => 'category' ); -
Create a workflow.
-
On the EMR Serverless Spark page, click Workflows in the left-side navigation pane.
-
On the Workflows page, click Create Workflow.
-
In the Create Workflow panel, enter a Name (for example, paimon_workflow_task), and then click Next.
Configure the parameters in the Other Settings section as needed. For more information about the parameters, see Manage a workflow.
-
On the new node canvas, click Add Node.
-
From the Path drop-down list, select the published SparkSQL job (paimon_compact). Enter the Spark Configuration parameters, and then click Save.
Parameter
Description
Name
A custom name for the SQL session. For example, paimon_compute.
-
On the new node canvas, click Publish Workflow, and then click OK.
-
-
Run the workflow.
-
On the Workflows page, click the Name of the new workflow (for example, paimon_workflow_task).
-
On the Workflow Runs page, click Manually Run.
-
In the Run Workflow dialog box, click OK.
-
-
Verify the compaction results.
After the workflow runs successfully, execute the same SQL query again to compare the number, record count, and size of the files before and after compaction.
SELECT file_path, record_count, file_size_in_bytes FROM paimon.test_paimon_db.`test_append_tbl$files`;After compaction, the query result shows that only three ORC files remain in the table:
data-c971b2a7-…-0.orc(record_count 144,591, file_size_in_bytes 29,122,164),data-17d0b4c6-…-0.orc(record_count 179,571, file_size_in_bytes 36,159,348), anddata-a8f27c6b-…-0.orc(record_count 35,838, file_size_in_bytes 7,243,404). This indicates that the small files have been successfully merged.