DataX doriswriter

DataX doriswriter plug-in, used to synchronize data from other data sources to Doris through DataX.

The plug-in uses Doris’ Stream Load function to synchronize and import data. It needs to be used with DataX service.

About DataX

DataX is an open source version of Alibaba Cloud DataWorks data integration, an offline data synchronization tool/platform widely used in Alibaba Group. DataX implements efficient data synchronization functions between various heterogeneous data sources including MySQL, Oracle, SqlServer, Postgre, HDFS, Hive, ADS, HBase, TableStore (OTS), MaxCompute (ODPS), Hologres, DRDS, etc.

More details can be found at: https://github.com/alibaba/DataX/

Usage

The code of DataX doriswriter plug-in can be found here.

This directory is the doriswriter plug-in development environment of Alibaba DataX.

Because the doriswriter plug-in depends on some modules in the DataX code base, and these module dependencies are not submitted to the official Maven repository, when we develop the doriswriter plug-in, we need to download the complete DataX code base to facilitate our development and compilation of the doriswriter plug-in.

Directory structure

  1. doriswriter/

    This directory is the code directory of doriswriter, and this part of the code should be in the Doris code base.

    The help doc can be found in doriswriter/doc

  2. init-env.sh

    The script mainly performs the following steps:

    1. Git clone the DataX code base to the local

    2. Softlink the doriswriter/ directory to DataX/doriswriter.

    3. Add <module>doriswriter</module> to the original DataX/pom.xml

    4. Change httpclient version from 4.5 to 4.5.13 in DataX/core/pom.xml

      httpclient v4.5 can not handle redirect 307 correctly.

    After that, developers can enter DataX/ for development. And the changes in the DataX/doriswriter directory will be reflected in the doriswriter/ directory, which is convenient for developers to submit code.

How to build

Doris code base compilation

  1. Run init-env.sh

  2. Modify code of doriswriter in DataX/doriswriter if you need.

  3. Build doriswriter

    1. Build doriswriter along:

      mvn clean install -pl plugin-rdbms-util,doriswriter -DskipTests

    2. Build DataX:

      mvn package assembly:assembly -Dmaven.test.skip=true

      The output will be in target/datax/datax/.

      hdfsreader, hdfswriter and oscarwriter needs some extra jar packages. If you don’t need to use these components, you can comment out the corresponding module in DataX/pom.xml.

    3. Compilation error

      If you encounter the following compilation errors:

      1. Could not find artifact com.alibaba.datax:datax-all:pom:0.0.1-SNAPSHOT ...

      You can try the following solutions:

      1. Download alibaba-datax-maven-m2-20210928.tar.gz
      2. After decompression, copy the resulting alibaba/datax/ directory to .m2/repository/com/alibaba/ corresponding to the maven used.
      3. Try to compile again.
  4. Commit code of doriswriter in doriswriter if you need.

Datax code base compilation

Pull the code from the datax code library and execute the compilation

  1. git clone https://github.com/alibaba/DataX.git
  2. cd datax
  3. mvn package assembly:assembly -Dmaven.test.skip=true

After compiling, you can see the datax.tar.gz package under datax/target/Datax

Datax DorisWriter parameter introduction:

  • jdbcUrl

    • Description: Doris’s JDBC connection string, the user executes preSql or postSQL.
    • Mandatory: Yes
    • Default: None
  • loadUrl

    • Description: As a connection target for Stream Load. The format is “ip:port”. Where IP is the FE node IP, port is the http_port of the FE node. You can fill in more than one, separated by a semicolon in English: ;, doriswriter will visit in a polling manner.
    • Mandatory: Yes
    • Default: None
  • username

    • Description: The username to access the Doris database
    • Mandatory: Yes
    • Default: None
  • password

    • Description: Password to access Doris database
    • Mandatory: No
    • Default: empty
  • connection.selectedDatabase

    • Description: The name of the Doris database that needs to be written.
    • Mandatory: Yes
    • Default: None
  • connection. table

    • Description: The name of the Doris table that needs to be written.
      • Mandatory: Yes
      • Default: None
  • flushInterval

    • Description: The time interval at which data is written in batches. If this time interval is set too small, it will cause Doris write blocking problem, error code -235, and if you set this time interval too small, maxBatchRows and batchSize parameters are set too large, then it may not be able to reach you The data size set by this will also be imported.
    • Mandatory: No
    • Default: 30000 (ms)
  • column

    • Description: The fields that the destination table needs to write data into, these fields will be used as the field names of the generated Json data. Fields are separated by commas. For example: “column”: [“id”,”name”,”age”].
    • Mandatory: Yes
    • Default: No
  • preSql

    • Description: Before writing data to the destination table, the standard statement here will be executed first.
    • Mandatory: No
    • Default: None
  • postSql

    • Description: After writing data to the destination table, the standard statement here will be executed.
    • Mandatory: No
    • Default: None
  • maxBatchRows

    • Description: The maximum number of rows for each batch of imported data. Together with batchSize, it controls the number of imported record rows per batch. When each batch of data reaches one of the two thresholds, the data of this batch will start to be imported.
    • Mandatory: No
    • Default: 500000
  • batchSize

    • Description: The maximum amount of data imported in each batch. Works with maxBatchRows to control the number of imports per batch. When each batch of data reaches one of the two thresholds, the data of this batch will start to be imported.
    • Mandatory: No
    • Default: 104857600
  • maxRetries

    • Description: The number of retries after each batch of failed data imports.
    • Mandatory: No
    • Default: 3
  • labelPrefix

    • Description: The label prefix for each batch of imported tasks. The final label will have labelPrefix + UUID to form a globally unique label to ensure that data will not be imported repeatedly
    • Mandatory: No
    • Default: datax_doris_writer_
  • loadProps

    • Description: The request parameter of StreamLoad. For details, refer to the StreamLoad introduction page. Stream load - Apache Doris

      This includes the imported data format: format, etc. The imported data format defaults to csv, which supports JSON. For details, please refer to the type conversion section below, or refer to the official information of Stream load above.

    • Mandatory: No

    • Default: None

Example

1. Stream reads the data and imports it to Doris

For instructions on using the doriswriter plug-in, please refer to here.

2.Mysql reads the data and imports it to Doris

1.Mysql table structure

  1. CREATE TABLE `t_test`(
  2. `id`bigint(30) NOT NULL,
  3. `order_code` varchar(30) DEFAULT NULL COMMENT '',
  4. `line_code` varchar(30) DEFAULT NULL COMMENT '',
  5. `remark` varchar(30) DEFAULT NULL COMMENT '',
  6. `unit_no` varchar(30) DEFAULT NULL COMMENT '',
  7. `unit_name` varchar(30) DEFAULT NULL COMMENT '',
  8. `price` decimal(12,2) DEFAULT NULL COMMENT '',
  9. PRIMARY KEY(`id`) USING BTREE
  10. )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='';

2.Doris table structure

  1. CREATE TABLE `ods_t_test` (
  2. `id`bigint(30) NOT NULL,
  3. `order_code` varchar(30) DEFAULT NULL COMMENT '',
  4. `line_code` varchar(30) DEFAULT NULL COMMENT '',
  5. `remark` varchar(30) DEFAULT NULL COMMENT '',
  6. `unit_no` varchar(30) DEFAULT NULL COMMENT '',
  7. `unit_name` varchar(30) DEFAULT NULL COMMENT '',
  8. `price` decimal(12,2) DEFAULT NULL COMMENT ''
  9. ENGINE=OLAP
  10. UNIQUE KEY(id`, `order_code`)
  11. DISTRIBUTED BY HASH(`order_code`) BUCKETS 1
  12. PROPERTIES (
  13. "replication_allocation" = "tag.location.default: 3",
  14. "in_memory" = "false",
  15. "storage_format" = "V2"
  16. );

3.Create datax script

my_import.json

  1. {
  2. "job": {
  3. "content": [
  4. {
  5. "reader": {
  6. "name": "mysqlreader",
  7. "parameter": {
  8. "column": ["id","order_code","line_code","remark","unit_no","unit_name","price"],
  9. "connection": [
  10. {
  11. "jdbcUrl": ["jdbc:mysql://localhost:3306/demo"],
  12. "table": ["employees_1"]
  13. }
  14. ],
  15. "username": "root",
  16. "password": "xxxxx",
  17. "where": ""
  18. }
  19. },
  20. "writer": {
  21. "name": "doriswriter",
  22. "parameter": {
  23. "loadUrl": ["127.0.0.1:8030"],
  24. "loadProps": {
  25. },
  26. "column": ["id","order_code","line_code","remark","unit_no","unit_name","price"],
  27. "username": "root",
  28. "password": "xxxxxx",
  29. "postSql": ["select count(1) from all_employees_info"],
  30. "preSql": [],
  31. "flushInterval":30000,
  32. "connection": [
  33. {
  34. "jdbcUrl": "jdbc:mysql://127.0.0.1:9030/demo",
  35. "selectedDatabase": "demo",
  36. "table": ["all_employees_info"]
  37. }
  38. ],
  39. "loadProps": {
  40. "format": "json",
  41. "strip_outer_array":"true",
  42. "line_delimiter": "\\x02"
  43. }
  44. }
  45. }
  46. }
  47. ],
  48. "setting": {
  49. "speed": {
  50. "channel": "1"
  51. }
  52. }
  53. }
  54. }

Remark:

  1. "loadProps": {
  2. "format": "json",
  3. "strip_outer_array": "true",
  4. "line_delimiter": "\\x02"
  5. }
  1. Here we use JSON format to import data
  2. line_delimiter defaults to a newline character, which may conflict with the value in the data, we can use some special characters or invisible characters to avoid import errors
  3. strip_outer_array : Represents multiple rows of data in a batch of imported data. Doris will expand the array when parsing, and then parse each Object in it as a row of data in turn.
  4. For more parameters of Stream load, please refer to [Stream load document]([Stream load - Apache Doris](https://doris.apache.org/zh-CN/docs/dev/data-operate/import/import-way /stream-load-manual))
  5. If it is in CSV format, we can use it like this
  1. "loadProps": {
  2. "format": "csv",
  3. "column_separator": "\\x01",
  4. "line_delimiter": "\\x02"
  5. }

CSV format should pay special attention to row and column separators to avoid conflicts with special characters in the data. Hidden characters are recommended here. The default column separator is: \t, row separator: \n

4.Execute the datax task, refer to the specific datax official website

  1. python bin/datax.py my_import.json

After execution, we can see the following information

  1. 2022-11-16 14:28:54.012 [job-0] INFO JobContainer - jobContainer starts to do prepare ...
  2. 2022-11-16 14:28:54.012 [job-0] INFO JobContainer - DataX Reader.Job [mysqlreader] do prepare work .
  3. 2022-11-16 14:28:54.013 [job-0] INFO JobContainer - DataX Writer.Job [doriswriter] do prepare work .
  4. 2022-11-16 14:28:54.020 [job-0] INFO JobContainer - jobContainer starts to do split ...
  5. 2022-11-16 14:28:54.020 [job-0] INFO JobContainer - Job set Channel-Number to 1 channels.
  6. 2022-11-16 14:28:54.023 [job-0] INFO JobContainer - DataX Reader.Job [mysqlreader] splits to [1] tasks.
  7. 2022-11-16 14:28:54.023 [job-0] INFO JobContainer - DataX Writer.Job [doriswriter] splits to [1] tasks.
  8. 2022-11-16 14:28:54.033 [job-0] INFO JobContainer - jobContainer starts to do schedule ...
  9. 2022-11-16 14:28:54.036 [job-0] INFO JobContainer - Scheduler starts [1] taskGroups.
  10. 2022-11-16 14:28:54.037 [job-0] INFO JobContainer - Running by standalone Mode.
  11. 2022-11-16 14:28:54.041 [taskGroup-0] INFO TaskGroupContainer - taskGroupId=[0] start [1] channels for [1] tasks.
  12. 2022-11-16 14:28:54.043 [taskGroup-0] INFO Channel - Channel set byte_speed_limit to -1, No bps activated.
  13. 2022-11-16 14:28:54.043 [taskGroup-0] INFO Channel - Channel set record_speed_limit to -1, No tps activated.
  14. 2022-11-16 14:28:54.049 [taskGroup-0] INFO TaskGroupContainer - taskGroup[0] taskId[0] attemptCount[1] is started
  15. 2022-11-16 14:28:54.052 [0-0-0-reader] INFO CommonRdbmsReader$Task - Begin to read record by Sql: [select taskid,projectid,taskflowid,templateid,template_name,status_task from dwd_universal_tb_task
  16. ] jdbcUrl:[jdbc:mysql://localhost:3306/demo?yearIsDateType=false&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&rewriteBatchedStatements=true].
  17. Wed Nov 16 14:28:54 GMT+08:00 2022 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
  18. 2022-11-16 14:28:54.071 [0-0-0-reader] INFO CommonRdbmsReader$Task - Finished read record by Sql: [select taskid,projectid,taskflowid,templateid,template_name,status_task from dwd_universal_tb_task
  19. ] jdbcUrl:[jdbc:mysql://localhost:3306/demo?yearIsDateType=false&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&rewriteBatchedStatements=true].
  20. 2022-11-16 14:28:54.104 [Thread-1] INFO DorisStreamLoadObserver - Start to join batch data: rows[2] bytes[438] label[datax_doris_writer_c4e08cb9-c157-4689-932f-db34acc45b6f].
  21. 2022-11-16 14:28:54.104 [Thread-1] INFO DorisStreamLoadObserver - Executing stream load to: 'http://127.0.0.1:8030/api/demo/dwd_universal_tb_task/_stream_load', size: '441'
  22. 2022-11-16 14:28:54.224 [Thread-1] INFO DorisStreamLoadObserver - StreamLoad response :{"Status":"Success","BeginTxnTimeMs":0,"Message":"OK","NumberUnselectedRows":0,"CommitAndPublishTimeMs":17,"Label":"datax_doris_writer_c4e08cb9-c157-4689-932f-db34acc45b6f","LoadBytes":441,"StreamLoadPutTimeMs":1,"NumberTotalRows":2,"WriteDataTimeMs":11,"TxnId":217056,"LoadTimeMs":31,"TwoPhaseCommit":"false","ReadDataTimeMs":0,"NumberLoadedRows":2,"NumberFilteredRows":0}
  23. 2022-11-16 14:28:54.225 [Thread-1] INFO DorisWriterManager - Async stream load finished: label[datax_doris_writer_c4e08cb9-c157-4689-932f-db34acc45b6f].
  24. 2022-11-16 14:28:54.249 [taskGroup-0] INFO TaskGroupContainer - taskGroup[0] taskId[0] is successed, used[201]ms
  25. 2022-11-16 14:28:54.250 [taskGroup-0] INFO TaskGroupContainer - taskGroup[0] completed it's tasks.
  26. 2022-11-16 14:29:04.048 [job-0] INFO StandAloneJobContainerCommunicator - Total 2 records, 214 bytes | Speed 21B/s, 0 records/s | Error 0 records, 0 bytes | All Task WaitWriterTime 0.000s | All Task WaitReaderTime 0.000s | Percentage 100.00%
  27. 2022-11-16 14:29:04.049 [job-0] INFO AbstractScheduler - Scheduler accomplished all tasks.
  28. 2022-11-16 14:29:04.049 [job-0] INFO JobContainer - DataX Writer.Job [doriswriter] do post work.
  29. Wed Nov 16 14:29:04 GMT+08:00 2022 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
  30. 2022-11-16 14:29:04.187 [job-0] INFO DorisWriter$Job - Start to execute preSqls:[select count(1) from dwd_universal_tb_task]. context info:jdbc:mysql://172.16.0.13:9030/demo.
  31. 2022-11-16 14:29:04.204 [job-0] INFO JobContainer - DataX Reader.Job [mysqlreader] do post work.
  32. 2022-11-16 14:29:04.204 [job-0] INFO JobContainer - DataX jobId [0] completed successfully.
  33. 2022-11-16 14:29:04.204 [job-0] INFO HookInvoker - No hook invoked, because base dir not exists or is a file: /data/datax/hook
  34. 2022-11-16 14:29:04.205 [job-0] INFO JobContainer -
  35. [total cpu info] =>
  36. averageCpu | maxDeltaCpu | minDeltaCpu
  37. -1.00% | -1.00% | -1.00%
  38. [total gc info] =>
  39. NAME | totalGCCount | maxDeltaGCCount | minDeltaGCCount | totalGCTime | maxDeltaGCTime | minDeltaGCTime
  40. PS MarkSweep | 1 | 1 | 1 | 0.017s | 0.017s | 0.017s
  41. PS Scavenge | 1 | 1 | 1 | 0.007s | 0.007s | 0.007s
  42. 2022-11-16 14:29:04.205 [job-0] INFO JobContainer - PerfTrace not enable!
  43. 2022-11-16 14:29:04.206 [job-0] INFO StandAloneJobContainerCommunicator - Total 2 records, 214 bytes | Speed 21B/s, 0 records/s | Error 0 records, 0 bytes | All Task WaitWriterTime 0.000s | All Task WaitReaderTime 0.000s | Percentage 100.00%
  44. 2022-11-16 14:29:04.206 [job-0] INFO JobContainer -
  45. 任务启动时刻 : 2022-11-16 14:28:53
  46. 任务结束时刻 : 2022-11-16 14:29:04
  47. 任务总计耗时 : 10s
  48. 任务平均流量 : 21B/s
  49. 记录写入速度 : 0rec/s
  50. 读出记录总数 : 2
  51. 读写失败总数 : 0