Skip to main content
Version: Next

CosFile

Cos file source connector

Support Those Engines

Spark
Flink
SeaTunnel Zeta

Key features

Read all the data in a split in a pollNext call. What splits are read will be saved in snapshot.

Description

Read data from aliyun Cos file system.

tip

If you use spark/flink, In order to use this connector, You must ensure your spark/flink cluster already integrated hadoop. The tested hadoop version is 2.x.

If you use SeaTunnel Engine, It automatically integrated the hadoop jar when you download and install SeaTunnel Engine. You can check the jar package under ${SEATUNNEL_HOME}/lib to confirm this.

To use this connector you need put hadoop-cos-{hadoop.version}-{version}.jar and cos_api-bundle-{version}.jar in ${SEATUNNEL_HOME}/lib dir, download: Hadoop-Cos-release. It only supports hadoop version 2.6.5+ and version 8.0.2+.

Options

nametyperequireddefault value
pathstringyes-
file_format_typestringyes-
bucketstringyes-
secret_idstringyes-
secret_keystringyes-
regionstringyes-
read_columnslistyes-
delimiter/field_delimiterstringno\001
parse_partition_from_pathbooleannotrue
skip_header_row_numberlongno0
date_formatstringnoyyyy-MM-dd
datetime_formatstringnoyyyy-MM-dd HH:mm:ss
time_formatstringnoHH:mm:ss
schemaconfigno-
sheet_namestringno-
xml_row_tagstringno-
xml_use_attr_formatbooleanno-
file_filter_patternstringno
compress_codecstringnonone
archive_compress_codecstringnonone
encodingstringnoUTF-8
common-optionsno-

path [string]

The source file path.

file_format_type [string]

File type, supported as the following file types:

text csv parquet orc json excel xml binary

If you assign file type to json, you should also assign schema option to tell connector how to parse data to the row you want.

For example:

upstream data is the following:


{"code": 200, "data": "get success", "success": true}

You can also save multiple pieces of data in one file and split them by newline:


{"code": 200, "data": "get success", "success": true}
{"code": 300, "data": "get failed", "success": false}

you should assign schema as the following:


schema {
fields {
code = int
data = string
success = boolean
}
}

connector will generate data as the following:

codedatasuccess
200get successtrue

If you assign file type to parquet orc, schema option not required, connector can find the schema of upstream data automatically.

If you assign file type to text csv, you can choose to specify the schema information or not.

For example, upstream data is the following:


tyrantlucifer#26#male

If you do not assign data schema connector will treat the upstream data as the following:

content
tyrantlucifer#26#male

If you assign data schema, you should also assign the option field_delimiter too except CSV file type

you should assign schema and delimiter as the following:


field_delimiter = "#"
schema {
fields {
name = string
age = int
gender = string
}
}

connector will generate data as the following:

nameagegender
tyrantlucifer26male

If you assign file type to binary, SeaTunnel can synchronize files in any format, such as compressed packages, pictures, etc. In short, any files can be synchronized to the target place. Under this requirement, you need to ensure that the source and sink use binary format for file synchronization at the same time. You can find the specific usage in the example below.

bucket [string]

The bucket address of Cos file system, for example: Cos://tyrantlucifer-image-bed

secret_id [string]

The secret id of Cos file system.

secret_key [string]

The secret key of Cos file system.

region [string]

The region of cos file system.

read_columns [list]

The read column list of the data source, user can use it to implement field projection.

delimiter/field_delimiter [string]

delimiter parameter will deprecate after version 2.3.5, please use field_delimiter instead.

Only need to be configured when file_format is text.

Field delimiter, used to tell connector how to slice and dice fields

default \001, the same as hive's default delimiter

parse_partition_from_path [boolean]

Control whether parse the partition keys and values from file path

For example if you read a file from path cosn://hadoop-cluster/tmp/seatunnel/parquet/name=tyrantlucifer/age=26

Every record data from file will be added these two fields:

nameage
tyrantlucifer26

Tips: Do not define partition fields in schema option

skip_header_row_number [long]

Skip the first few lines, but only for the txt and csv.

For example, set like following:

skip_header_row_number = 2

then SeaTunnel will skip the first 2 lines from source files

date_format [string]

Date type format, used to tell connector how to convert string to date, supported as the following formats:

yyyy-MM-dd yyyy.MM.dd yyyy/MM/dd

default yyyy-MM-dd

datetime_format [string]

Datetime type format, used to tell connector how to convert string to datetime, supported as the following formats:

yyyy-MM-dd HH:mm:ss yyyy.MM.dd HH:mm:ss yyyy/MM/dd HH:mm:ss yyyyMMddHHmmss

default yyyy-MM-dd HH:mm:ss

time_format [string]

Time type format, used to tell connector how to convert string to time, supported as the following formats:

HH:mm:ss HH:mm:ss.SSS

default HH:mm:ss

schema [config]

Only need to be configured when the file_format_type are text, json, excel, xml or csv ( Or other format we can't read the schema from metadata).

fields [Config]

The schema of upstream data.

sheet_name [string]

Only need to be configured when file_format is excel.

Reader the sheet of the workbook.

xml_row_tag [string]

Only need to be configured when file_format is xml.

Specifies the tag name of the data rows within the XML file.

xml_use_attr_format [boolean]

Only need to be configured when file_format is xml.

Specifies Whether to process data using the tag attribute format.

file_filter_pattern [string]

Filter pattern, which used for filtering files.

The pattern follows standard regular expressions. For details, please refer to https://en.wikipedia.org/wiki/Regular_expression. There are some examples.

File Structure Example:

/data/seatunnel/20241001/report.txt
/data/seatunnel/20241007/abch202410.csv
/data/seatunnel/20241002/abcg202410.csv
/data/seatunnel/20241005/old_data.csv
/data/seatunnel/20241012/logo.png

Matching Rules Example:

Example 1: Match all .txt files,Regular Expression:

/data/seatunnel/20241001/.*\.txt

The result of this example matching is:

/data/seatunnel/20241001/report.txt

Example 2: Match all file starting with abc,Regular Expression:

/data/seatunnel/20241002/abc.*

The result of this example matching is:

/data/seatunnel/20241007/abch202410.csv
/data/seatunnel/20241002/abcg202410.csv

Example 3: Match all file starting with abc,And the fourth character is either h or g, the Regular Expression:

/data/seatunnel/20241007/abc[h,g].*

The result of this example matching is:

/data/seatunnel/20241007/abch202410.csv

Example 4: Match third level folders starting with 202410 and files ending with .csv, the Regular Expression:

/data/seatunnel/202410\d*/.*\.csv

The result of this example matching is:

/data/seatunnel/20241007/abch202410.csv
/data/seatunnel/20241002/abcg202410.csv
/data/seatunnel/20241005/old_data.csv

compress_codec [string]

The compress codec of files and the details that supported as the following shown:

  • txt: lzo none
  • json: lzo none
  • csv: lzo none
  • orc/parquet:
    automatically recognizes the compression type, no additional settings required.

archive_compress_codec [string]

The compress codec of archive files and the details that supported as the following shown:

archive_compress_codecfile_formatarchive_compress_suffix
ZIPtxt,json,excel,xml.zip
TARtxt,json,excel,xml.tar
TAR_GZtxt,json,excel,xml.tar.gz
GZtxt,json,excel,xml.gz
NONEall.*

Note: gz compressed excel file needs to compress the original file or specify the file suffix, such as e2e.xls ->e2e_test.xls.gz

encoding [string]

Only used when file_format_type is json,text,csv,xml. The encoding of the file to read. This param will be parsed by Charset.forName(encoding).

common options

Source plugin common parameters, please refer to Source Common Options for details.

Example


CosFile {
path = "/seatunnel/orc"
bucket = "cosn://seatunnel-test-1259587829"
secret_id = "xxxxxxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxxxx"
region = "ap-chengdu"
file_format_type = "orc"
}


CosFile {
path = "/seatunnel/json"
bucket = "cosn://seatunnel-test-1259587829"
secret_id = "xxxxxxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxxxx"
region = "ap-chengdu"
file_format_type = "json"
schema {
fields {
id = int
name = string
}
}
}

Transfer Binary File


env {
parallelism = 1
job.mode = "BATCH"
}

source {
CosFile {
bucket = "cosn://seatunnel-test-1259587829"
secret_id = "xxxxxxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxxxx"
region = "ap-chengdu"
path = "/seatunnel/read/binary/"
file_format_type = "binary"
}
}
sink {
// you can transfer local file to s3/hdfs/oss etc.
CosFile {
bucket = "cosn://seatunnel-test-1259587829"
secret_id = "xxxxxxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxxxx"
region = "ap-chengdu"
path = "/seatunnel/read/binary2/"
file_format_type = "binary"
}
}

Filter File

env {
parallelism = 1
job.mode = "BATCH"
}

source {
CosFile {
bucket = "cosn://seatunnel-test-1259587829"
secret_id = "xxxxxxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxxxx"
region = "ap-chengdu"
path = "/seatunnel/read/binary/"
file_format_type = "binary"
// file example abcD2024.csv
file_filter_pattern = "abc[DX]*.*"
}
}

sink {
Console {
}
}

Changelog

next version

  • Add file cos source connector (4979)