Flink Getting Started Guide
Support Matrix
| LakeSoul | Flink |
|---|---|
| 3.0.0+ | 1.20 |
| 2.4.x+ | 1.17 |
| 2.1.x-2.3.x | 1.14 |
PG Configuration
Add the following configuration to $FLINK_HOME/conf/flink-conf.yaml:
containerized.master.env.LAKESOUL_PG_DRIVER: com.lakesoul.shaded.org.postgresql.Driver
containerized.master.env.LAKESOUL_PG_USERNAME: root
containerized.master.env.LAKESOUL_PG_PASSWORD: root
containerized.master.env.LAKESOUL_PG_URL: jdbc:postgresql://localhost:5432/test_lakesoul_meta?stringtype=unspecified
containerized.taskmanager.env.LAKESOUL_PG_DRIVER: com.lakesoul.shaded.org.postgresql.Driver
containerized.taskmanager.env.LAKESOUL_PG_USERNAME: root
containerized.taskmanager.env.LAKESOUL_PG_PASSWORD: root
containerized.taskmanager.env.LAKESOUL_PG_URL: jdbc:postgresql://localhost:5432/test_lakesoul_meta?stringtype=unspecified
Note that both the master and taskmanager environment variables need to be set.
The connection information, username and password of the Postgres database need to be modified according to the actual deployment.
Note that if you use Session mode to start a job, that is, submit the job to Flink Standalone Cluster as a client, flink run as a client will not read the above configuration, so you need to configure the environment variables separately, namely:
export LAKESOUL_PG_DRIVER=com.lakesoul.shaded.org.postgresql.Driver
export LAKESOUL_PG_URL=jdbc:postgresql://localhost:5432/test_lakesoul_meta?stringtype=unspecified
export LAKESOUL_PG_USERNAME=root
export LAKESOUL_PG_PASSWORD=root
SQL
Download LakeSoul Flink Jar
It can be downloaded from the LakeSoul Release page: https://github.com/lakesoul-io/LakeSoul/releases/download/v4.0.0/lakesoul-flink-1.20_2.12-4.0.0.jar.
Start SQL Client
# Start Flink SQL Client
bin/sql-client.sh embedded -j lakesoul-flink-1.20_2.12-4.0.0.jar
Create Table
- Java
- SQL
TableEnvironment tEnv = TableEnvironment.create(EnvironmentSettings.inBatchMode());
String createUserSql = "create table user_info (" +
"`id` INT," +
"name STRING," +
"score INT," +
"`date` STRING," +
"region STRING," +
" PRIMARY KEY (`id`,`name`) NOT ENFORCED"+
") PARTITIONED BY (`region`,`date`)"+
" WITH (" +
" 'connector'='lakesoul'," +
" 'hashBucketNum'='4'," +
" 'use_cdc'='true'," +
" 'path'='/tmp/lakesoul/flink/sink/test' )";
tEnv. executeSql(createUserSql);
-- Create the test_table table, use id and name as the joint primary key, use region and date as the two-level range partition, catalog is lakesoul, and database is default
create table `lakesoul`.`default`.test_table (
`id` INT,
name STRING,
score INT,
`date` STRING,
region STRING,
PRIMARY KEY (`id`,`name`) NOT ENFORCED
) PARTITIONED BY (`region`,`date`)
WITH (
'connector'='lakesoul',
'hashBucketNum'='4',
'use_cdc'='true',
'path'='file:///tmp/lakesoul/flink/sink/test');
The meaning of the parameters for creating a table
| Parameter | Explanation | Value Format |
|---|---|---|
| PARTITIONED BY | used to specify the range partition field of the table, if there is no range partition field, it will be omitted | PARTITIONED BY (date) |
| PRIMARY KEY | used to specify one or more primary keys | PARIMARY KEY (id, name) NOT ENFORCED |
| connector | data source connector, used to specify the data source type | 'connector'='lakesoul' |
| hashBucketNum | table with primary key(s) must have this property set to a number >= 0 | 'hashBucketNum'='4' |
| path | used to specify the storage path of the table | 'path'='file:///tmp/lakesoul/flink/sink/test' |
| use_cdc | Set whether the table is in CDC format (refer to CDC Table Format ) | 'use_cdc'='true' |
Drop Table
- Java
- SQL
tEnvs.executeSql("DROP TABLE if exists test_table");
DROP TABLE if exists test_table;
Insert Data
- Java
- SQL
tEnvs.executeSql("insert into `lakesoul`.`default`.test_table values (1, 'AAA', 98, '2023-05-10', 'China')"). await();
Batch insert:
insert into `lakesoul`.`default`.test_table values (1,'AAA', 98, '2023-05-10', 'China');
Streaming: read data from another stream source and write into LakeSoul. If upstream source is a Changelog stream, then LakeSoul table should be created with CDC format enabled.
insert into `lakesoul`.`default`.test_table select * from `lakesoul`.`cdcsink`.soure_table;
- For stream writing, checkpoint interval needs to be set, and it is recommended to be more than 1 minute;
- Set the corresponding time zone according to the environment:
SET 'table.local-time-zone' = 'Asia/Shanghai';
-- Set the checkpointing interval
SET 'execution.checkpointing.interval' = '2min';
Update Data
For batch mode only.
- Java
- SQL
tEnvs.executeSql("UPDATE `lakesoul`.`default`.test_table set score = 100 where id = 1") await();
UPDATE `lakesoul`.`default`.test_table set score = 100 where id = 1;
Note that in the case of update, updating the values of primary key and partition columns is not allowed. For the stream execution mode, LakeSoul has been able to support ChangeLog semantics, which can support additions, deletions and modifications.