FileSystem Connector
Overview
The FileSystem connector is used to exchange data with the file system in Flink jobs of AI DataLake. This connector supports unified batch and stream read and write operations on storage backends such as the local file system and OBS. It provides multi-file format and partitioned write capabilities, and is applicable to scenarios such as data archiving, file export, and batch processing ETL.
| Category | Details |
|---|---|
| Supported type | Source table and result table |
| Data format | CSV, JSON, Avro, Parquet, Orc, Debezium-JSON, Canal-JSON, Raw |
| API type | SQL, DataStream |
| Whether result table can be updated or deleted | No |
Application Scenarios
- Data archiving: Stream data is written to HDFS or OBS in time-partitioned rolling files for long-term archival storage.
- File export: Processing results are exported to the file system in CSV or JSON format for consumption by downstream systems.
- Batch processing ETL: Data is read from the file system in batches, cleansed, transformed, and then written to the target storage.
- Columnar storage optimization: Data is written to files in Parquet or ORC format to improve downstream query performance.
- Multi-source data aggregation: Data from multiple sources is written to the file system in a unified manner to build a data lake.
Notes and Constraints
- The file format must match the configuration of the format parameter. Otherwise, an error is reported during reading.
- Exactly-once semantics depends on the checkpoint mechanism. Ensure that checkpoints are triggered properly and at a reasonable interval.
Syntax Structure
Source
CREATE TABLE fs_source (
{column_name} {column_type},
...
) WITH (
'connector' = 'filesystem',
'path' = '{file_system_path}',
'format' = '{file_format}'
); Sink
CREATE TABLE fs_sink (
{column_name} {column_type},
...
) PARTITIONED BY ({partition_columns}) WITH (
'connector' = 'filesystem',
'path' = '{file_system_path}',
'format' = '{file_format}',
'sink.rolling-policy.file-size' = '{size}',
'sink.rolling-policy.rollover-interval' = '{interval}'
); WITH Parameters
Common parameters
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| connector | Yes | String | None | filesystem | Specifies the connector type as FileSystem. This parameter has a fixed value and cannot be modified. You must set this parameter to filesystem. The system loads the corresponding file system connector implementation based on the value of this parameter. |
| path | Yes | String | None | Valid file system path | Specifies the read and write path of the file system. Local paths (file:///), OBS paths (obs://), and other paths are supported. Ensure that the Flink job has read and write permissions on the path. |
| format | Yes | String | None | csv, json, avro, parquet, orc | Specifies the serialization format of the file.
You need to select a format based on data features and query requirements. |
Source-specific parameters
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| source.monitor-interval | No | Duration | None | Valid duration, for example, 10 seconds or 1 minute. | Specifies the interval for monitoring new files in the directory in streaming mode. After this parameter is configured, the source periodically scans the directory and automatically reads new files. If this parameter is not configured, the source reads all files in the directory only once. You need to adjust the scanning interval based on the file generation frequency. |
Sink-specific parameters
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| sink.rolling-policy.file-size | No | MemorySize | 128 MB | Valid memory size, for example, 64 MB or 256 MB. | Specifies the target size of a rolling file. When a single file reaches this size, a new file is generated. You need to balance the number of files and the size of a single file. If the file size is too small, too many small files are generated, affecting query performance. If the file size is too large, write latency is affected. |
| sink.rolling-policy.rollover-interval | No | Duration | 30min | Valid duration, for example, 30 seconds or 5 minutes. | Specifies the interval for rolling files. When the file opening time exceeds this interval, a new file is generated. You need to adjust the interval based on data visibility requirements. If the interval is too short, too many small files are generated. If the interval is too long, data latency is high. |
| sink.rolling-policy.check-interval | No | Duration | 60s | Valid duration, for example, 30 seconds or 5 minutes. | Check interval of the time-based rolling policy. This parameter controls the frequency at which the system checks whether some part files should be rolled based on the sink.rolling-policy.rollover-interval parameter. |
| sink.partition-commit.policy.kind | No | String | None | success-file, metadata, success-file+metadata | Specifies the partition commit policy.
After this parameter is configured, the system triggers the commit operation after the partition data is written, and notifies the downstream that the partition is ready. |
| sink.partition-commit.delay | No | Duration | 0s | Valid duration, for example, 1 minute or 10 minutes. | Specifies the delay time for committing a partition. After the partition data is written, the system waits for the specified time before committing the data. This ensures that the delayed data is written to the correct partition. Set this parameter based on the data delay. If the delay is too short, data may be written to an incorrect partition. |
| sink.partition-commit.trigger | No | String | process-time | process-time, partition-time | Specifies the trigger type for committing a partition.
You are advised to use partition-time to ensure data consistency. |
| auto-compaction | No | Boolean | false | true, false | Specifies whether to enable automatic file merging. After this function is enabled, the system automatically merges small files into files of the target size during checkpoint submission. You are advised to enable this function in scenarios where a large number of small files are generated due to a short rolling interval. |
| compaction.file-size | No | MemorySize | None | Valid memory size, for example, 128 MB or 256 MB. | Specifies the size of the target file after file merging. This parameter takes effect only when auto-compaction is set to true. It must be configured in coordination with sink.rolling-policy.file-size. You are advised to set this parameter to a value that is one to two times the value of file-size. |
Usage Example
- Example 1: Read CSV files from HDFS in batches and output the data to the Print result table.
-- Create a FileSystem source table (CSV format, batch read). CREATE TABLE fs_csv_source ( order_id BIGINT, user_id BIGINT, amount DOUBLE, order_time TIMESTAMP(3) ) WITH ( 'connector' = 'filesystem', 'path' = 'obs://xxx/data/orders/', 'format' = 'csv' ); -- Create the Print result table. CREATE TABLE print_sink ( order_id BIGINT, user_id BIGINT, amount DOUBLE, order_time TIMESTAMP(3) ) WITH ( 'connector' = 'print', 'print-identifier' = 'fs_csv_orders' ); -- Submit the job. INSERT INTO print_sink SELECT order_id, user_id, amount, order_time FROM fs_csv_source;
- Example 2: Use DataGen to generate simulated data and write the data to HDFS in Parquet columnar format, partitioned by day.
-- Create a DataGen source table. CREATE TABLE event_source ( event_id BIGINT, event_type STRING, event_time TIMESTAMP(3), dt STRING ) WITH ( 'connector' = 'datagen', 'rows-per-second' = '200', 'fields.event_id.kind' = 'sequence', 'fields.event_id.start' = '1', 'fields.event_id.end' = '1000000', 'fields.event_type.length' = '10', 'fields.dt.kind' = 'sequence', 'fields.dt.start' = '20260101', 'fields.dt.end' = '20260131' ); -- Create the FileSystem sink result table (Parquet format, partitioned by day). CREATE TABLE fs_parquet_sink ( event_id BIGINT, event_type STRING, event_time TIMESTAMP(3), dt STRING ) PARTITIONED BY (dt) WITH ( 'connector' = 'filesystem', 'path' = 'obs://xxx/data/events/', 'format' = 'parquet', 'sink.rolling-policy.file-size' = '256mb', 'sink.rolling-policy.rollover-interval' = '5min', 'sink.partition-commit.policy.kind' = 'success-file', 'sink.partition-commit.trigger' = 'partition-time' ); -- Submit the job. INSERT INTO fs_parquet_sink SELECT event_id, event_type, event_time, dt FROM event_source;
- Example 3: Stream and monitor new JSON files added to an HDFS directory, and output them to a Print result table in real time.
-- Create a FileSystem source table (JSON format, stream monitoring). CREATE TABLE fs_json_source ( log_id BIGINT, log_level STRING, log_message STRING, log_time TIMESTAMP(3) ) WITH ( 'connector' = 'filesystem', 'path' = 'obs://xxx/data/logs/', 'format' = 'json', 'source.monitor-interval' = '10s' ); -- Create the Print result table. CREATE TABLE log_print_sink ( log_id BIGINT, log_level STRING, log_message STRING, log_time TIMESTAMP(3) ) WITH ( 'connector' = 'print', 'print-identifier' = 'fs_json_logs' ); -- Submit the job. INSERT INTO log_print_sink SELECT log_id, log_level, log_message, log_time FROM fs_json_source;
- Example 4: Consume order data from Kafka, write the data to HDFS in ORC columnar format with hourly partitioning, and enable automatic file merging.
-- Create a Kafka source table. CREATE TABLE kafka_order_source ( order_id STRING, user_id BIGINT, amount DECIMAL(10, 2), status STRING, event_time TIMESTAMP(3), WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND, dt STRING, hh STRING ) WITH ( 'connector' = 'kafka', 'topic' = 'orders', 'properties.bootstrap.servers' = 'kafka-broker:9092', 'properties.group.id' = 'fs_orc_group', 'scan.startup.mode' = 'latest-offset', 'format' = 'json', 'json.ignore-parse-errors' = 'true' ); -- Create the FileSystem sink result table (ORC format, partitioned by day and hour). CREATE TABLE fs_orc_sink ( order_id STRING, user_id BIGINT, amount DECIMAL(10, 2), status STRING, event_time TIMESTAMP(3), dt STRING, hh STRING ) PARTITIONED BY (dt, hh) WITH ( 'connector' = 'filesystem', 'path' = 'obs://xxx/data/orders_orc/', 'format' = 'orc', 'sink.rolling-policy.file-size' = '128mb', 'sink.rolling-policy.rollover-interval' = '1min', 'sink.partition-commit.policy.kind' = 'success-file', 'sink.partition-commit.trigger' = 'partition-time', 'auto-compaction' = 'true', 'compaction.file-size' = '256mb' ); -- Submit the ETL job. INSERT INTO fs_orc_sink SELECT order_id, user_id, amount, status, event_time, DATE_FORMAT(event_time, 'yyyy-MM-dd') AS dt, DATE_FORMAT(event_time, 'HH') AS hh FROM kafka_order_source; - Example 5: Read Avro files from HDFS in batch mode, filter the data, and write the filtered results to another HDFS directory in JSON format.
-- Create a FileSystem source table (Avro format). CREATE TABLE fs_avro_source ( user_id BIGINT, user_name STRING, age INT, city STRING, register_time TIMESTAMP(3) ) WITH ( 'connector' = 'filesystem', 'path' = 'obs://xxx/data/users_avro/', 'format' = 'avro' ); -- Create the FileSystem sink result table (JSON format). CREATE TABLE fs_json_sink ( user_id BIGINT, user_name STRING, age INT, city STRING, register_time TIMESTAMP(3) ) WITH ( 'connector' = 'filesystem', 'path' = 'obs://xxx/data/users_json/', 'format' = 'json', 'sink.rolling-policy.file-size' = '128mb', 'sink.rolling-policy.rollover-interval' = '5min' ); -- Submit the job to filter adult users. INSERT INTO fs_json_sink SELECT user_id, user_name, age, city, register_time FROM fs_avro_source WHERE age >= 18;
- Example 6: Use DataGen to generate simulated data and write it to the local file system in CSV format. This example is applicable to local test scenarios.
-- Create a DataGen source table. CREATE TABLE metric_source ( metric_id BIGINT, metric_name STRING, metric_value DOUBLE, metric_time TIMESTAMP(3) ) WITH ( 'connector' = 'datagen', 'rows-per-second' = '100', 'fields.metric_id.kind' = 'sequence', 'fields.metric_id.start' = '1', 'fields.metric_id.end' = '100000', 'fields.metric_name.length' = '15', 'fields.metric_value.min' = '0.0', 'fields.metric_value.max' = '100.0' ); -- Create the FileSystem sink result table (CSV format, local file system). CREATE TABLE fs_local_csv_sink ( metric_id BIGINT, metric_name STRING, metric_value DOUBLE, metric_time TIMESTAMP(3) ) WITH ( 'connector' = 'filesystem', 'path' = 'file:///tmp/flink/metrics/', 'format' = 'csv', 'sink.rolling-policy.file-size' = '64mb', 'sink.rolling-policy.rollover-interval' = '30s' ); -- Submit the job. INSERT INTO fs_local_csv_sink SELECT metric_id, metric_name, metric_value, metric_time FROM metric_source;
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