数据导入

1. 功能概述

数据导入工具import-data.sh 位于 tools 目录下,可以将 CSVSQL、及 TsFile (开源时序文件格式)的数据导入 UDB-DS

文件格式 UDB-DS工具 具体介绍
CSV import-data.sh 可用于单个或一个目录的 CSV 文件批量导入 UDB-DS
SQL 可用于单个或一个目录的 SQL 文件批量导入 UDB-DS
TsFile 可用于单个或一个目录的 TsFile 文件批量导入 UDB-DS

2. 数据导入工具

2.1 公共参数

参数缩写 参数全称 参数含义 是否为必填项 默认值
-ft --file_type 导入文件的类型,可以选择:csv、sql、tsfile
-h -- host 主机名 127.0.0.1
-p --port 端口号 7777
-u --username 用户名 root
-pw --password 密码 root
-s --source 待加载的脚本文件(夹)的本地目录路径
如果为csv sql tsfile这三个支持的格式,直接导入
不支持的格式,报错提示The file name must end with "csv" or "sql"or "tsfile"!
-tn --thread_num 最大并行线程数 8
范围:0~Integer.Max=2147483647
-tz --timezone 时区设置,例如+08:00-01:00 本机系统时间
-help --help 显示帮助信息,支持分开展示和全部展示-help-help csv

2.2 CSV 格式

2.2.1 运行命令

> tools/import-data.sh -ft<format>  [-h <host>] [-p <port>] [-u <username>] [-pw <password>]
       -s <source> [-fd <fail_dir>] [-lpf <lines_per_failed_file>] [-aligned <use the aligned interface>] 
      [-ti <type_infer>] [-tp <timestamp precision (ms/us/ns)>] [-tz <timezone>] [-batch <batch_size>] 
      [-tn <thread_num>]

2.2.2 私有参数

参数缩写 参数全称 参数含义 是否为必填项 默认值
-fd --fail_dir 指定保存失败文件的目录 YOUR_CSV_FILE_PATH
-lpf --lines_per_failed_file 指定失败文件最大写入数据的行数 100000
范围:0~Integer.Max=2147483647
-aligned --use_aligned 是否导入为对齐序列 false
-batch --batch_size 指定每调用一次接口处理的数据行数(最小值为1,最大值为Integer.​MAX_VALUE​) 100000
范围:0~Integer.Max=2147483647
-ti --type_infer 通过选项定义类型信息,例如"boolean=text,int=long, ..."
-tp --timestamp_precision 时间戳精度 否:
1. ms(毫秒)
2. us(微秒)
3. ns(纳秒)
ms

2.2.3 运行示例

# 正确示例
> tools/import-data.sh -ft csv -h 127.0.0.1 -p 7777 -u root -pw root -s /path/sql 
      -fd /path/failure/dir -lpf 100  -aligned true -ti "BOOLEAN=text,INT=long,FLOAT=double" 
      -tp ms -tz +08:00 -batch 5000  -tn 4
      
# 异常示例
> tools/import-data.sh -ft csv -s /non_path
error: Source file or directory /non_path does not exist

> tools/import-data.sh -ft csv -s /path/sql -tn 0
error: Invalid thread number '0'. Please set a positive integer.

2.3.4 导入说明

  1. CSV 导入规范

  • 特殊字符转义规则:若Text类型的字段中包含特殊字符(例如逗号,),需使用反斜杠(\)​进行转义处理。

  • 支持的时间格式:yyyy-MM-dd’T’HH:mm:ss, yyy-MM-dd HH:mm:ss, 或者 yyyy-MM-dd’T’HH:mm:ss.SSSZ。

  • 时间戳列​必须作为数据文件的首列存在。

  1. CSV 文件示例

  • 时间对齐

-- header 中不包含数据类型
  Time,root.test.t1.str,root.test.t2.str,root.test.t2.var
  1970-01-01T08:00:00.001+08:00,"123hello world","123\,abc",100
  1970-01-01T08:00:00.002+08:00,"123",,

-- header 中包含数据类型(Text 类型数据支持加双引号和不加双引号)
Time,root.test.t1.str(TEXT),root.test.t2.str(TEXT),root.test.t2.var(INT32)
1970-01-01T08:00:00.001+08:00,"123hello world","123\,abc",100
1970-01-01T08:00:00.002+08:00,123,hello world,123
1970-01-01T08:00:00.003+08:00,"123",,
1970-01-01T08:00:00.004+08:00,123,,12
  • 设备对齐

-- header 中不包含数据类型
  Time,Device,str,var
  1970-01-01T08:00:00.001+08:00,root.test.t1,"123hello world",
  1970-01-01T08:00:00.002+08:00,root.test.t1,"123",
  1970-01-01T08:00:00.001+08:00,root.test.t2,"123\,abc",100

-- header 中包含数据类型(Text 类型数据支持加双引号和不加双引号)
Time,Device,str(TEXT),var(INT32)
1970-01-01T08:00:00.001+08:00,root.test.t1,"123hello world",
1970-01-01T08:00:00.002+08:00,root.test.t1,"123",
1970-01-01T08:00:00.001+08:00,root.test.t2,"123\,abc",100
1970-01-01T08:00:00.002+08:00,root.test.t1,hello world,123

2.3 SQL 格式

2.2.1 运行命令

> tools/import-data.sh -ft<format>  [-h <host>] [-p <port>] [-u <username>] [-pw <password>] 
        -s<source> [-fd <fail_dir>] [-lpf <lines_per_failed_file>] [-tz <timezone>] 
        [-batch <batch_size>] [-tn <thread_num>]

2.2.2 私有参数

参数缩写 参数全称 参数含义 是否为必填项 默认值
-fd --fail_dir 指定保存失败文件的目录 YOUR_CSV_FILE_PATH
-lpf --lines_per_failed_file 指定失败文件最大写入数据的行数 100000
范围:0~Integer.Max=2147483647
-batch --batch_size 指定每调用一次接口处理的数据行数(最小值为1,最大值为Integer.​MAX_VALUE​) 100000
范围:0~Integer.Max=2147483647

2.2.3 运行示例

# 正确示例
> tools/import-data.sh -ft sql -h 127.0.0.1 -p 7777 -u root -pw root -s /path/sql 
        -fd /path/failure/dir -lpf 500  -tz +08:00 
        -batch 100000  -tn 4
      
# 异常示例
> tools/import-data.sh -ft sql -s /path/sql -fd /non_path
error: Source file or directory /path/sql does not exist


> tools/import-data.sh -ft sql -s /path/sql -tn 0
error: Invalid thread number '0'. Please set a positive integer.

2.4 TsFile 格式

2.4.1 运行命令

> tools/import-data.sh -ft <format> [-h <host>] [-p <port>] [-u <username>] [-pw <password>] 
        -s <source> -os <on_success> [-sd <success_dir>] -of <on_fail> [-fd <fail_dir>]
        [-tn <thread_num> ] [-tz <timezone>] [-tp <timestamp precision (ms/us/ns)>]

2.4.2 私有参数

参数缩写 参数全称 参数含义 是否为必填项 默认值
-os --on_succcess 1. none:不删除
2. mv:移动成功的文件到目标文件夹
3. cp:硬连接(拷贝)成功的文件到目标文件夹
4. delete:删除
-sd --success_dir --on_succcess为mv或cp时,mv或cp的目标文件夹。文件的文件名变为文件夹打平后拼接原有文件名 --on_succcess为mv或cp时需要填写 ${EXEC_DIR}/success
-of --on_fail 1. none:跳过
2. mv:移动失败的文件到目标文件夹
3. cp:硬连接(拷贝)失败的文件到目标文件夹
4. delete:删除
-fd --fail_dir --on_fail指定为mv或cp时,mv或cp的目标文件夹。文件的文件名变为文件夹打平后拼接原有文件名 --on_fail指定为mv或cp时需要填写 ${EXEC_DIR}/fail
-tp --timestamp_precision 时间戳精度
tsfile非远程导入:-tp 指定tsfile文件的时间精度 手动校验和服务器的时间戳是否一致 不一致返回报错信息
远程导入:-tp 指定tsfile文件的时间精度 pipe自动校验时间戳精度是否一致 不一致返回pipe报错信息
否:
1. ms(毫秒)
2. us(微秒)
3. ns(纳秒)
ms

2.4.3 运行示例

# 正确示例
> tools/import-data.sh -ft tsfile -h 127.0.0.1 -p 7777 -u root -pw root 
      -s /path/sql -os mv -of cp -sd /path/success/dir -fd /path/failure/dir 
      -tn 8 -tz +08:00 -tp ms
      
# 异常示例
> tools/import-data.sh -ft tsfile -s /path/sql -os mv -of cp 
           -fd /path/failure/dir  -tn 8 
error: Missing option --success_dir (or -sd) when --on_success is 'mv' or 'cp'

> tools/import-data.sh -ft tsfile -s /path/sql -os mv -of cp 
          -sd /path/success/dir -fd /path/failure/dir  -tn 0 
error: Invalid thread number '0'. Please set a positive integer.