Flink Doris Connector

This document applies to flink-doris-connector versions after 1.1.0, for versions before 1.1.0 refer to here

The Flink Doris Connector can support operations (read, insert, modify, delete) data stored in Doris through Flink.

Github: https://github.com/apache/incubator-doris-flink-connector

  • Doris table can be mapped to DataStream or Table.

Note:

  1. Modification and deletion are only supported on the Unique Key model
  2. The current deletion is to support Flink CDC to access data to achieve automatic deletion. If it is to delete other data access methods, you need to implement it yourself. For the data deletion usage of Flink CDC, please refer to the last section of this document

Version Compatibility

Connector VersionFlink VersionDoris VersionJava VersionScala Version
1.0.31.11+0.15+82.11,2.12
1.1.01.14+1.0+82.11,2.12
1.2.01.15+1.0+8-

Build and Install

Ready to work

1.Modify the custom_env.sh.tpl file and rename it to custom_env.sh

2.Specify the thrift installation directory

  1. ##source file content
  2. #export THRIFT_BIN=
  3. #export MVN_BIN=
  4. #export JAVA_HOME=
  5. ##amend as below,MacOS as an example
  6. export THRIFT_BIN=/opt/homebrew/Cellar/thrift@0.13.0/0.13.0/bin/thrift
  7. #export MVN_BIN=
  8. #export JAVA_HOME=
  9. Install `thrift` 0.13.0 (Note: `Doris` 0.15 and the latest builds are based on `thrift` 0.13.0, previous versions are still built with `thrift` 0.9.3)
  10. Windows:
  11. 1. Download: `http://archive.apache.org/dist/thrift/0.13.0/thrift-0.13.0.exe`
  12. 2. Modify thrift-0.13.0.exe to thrift
  13. MacOS:
  14. 1. Download: `brew install thrift@0.13.0`
  15. 2. default address: /opt/homebrew/Cellar/thrift@0.13.0/0.13.0/bin/thrift
  16. Note: Executing `brew install thrift@0.13.0` on MacOS may report an error that the version cannot be found. The solution is as follows, execute it in the terminal:
  17. 1. `brew tap-new $USER/local-tap`
  18. 2. `brew extract --version='0.13.0' thrift $USER/local-tap`
  19. 3. `brew install thrift@0.13.0`
  20. Reference link: `https://gist.github.com/tonydeng/02e571f273d6cce4230dc8d5f394493c`
  21. Linux:
  22. 1.Download source package`wget https://archive.apache.org/dist/thrift/0.13.0/thrift-0.13.0.tar.gz`
  23. 2.Install dependencies`yum install -y autoconf automake libtool cmake ncurses-devel openssl-devel lzo-devel zlib-devel gcc gcc-c++`
  24. 3.`tar zxvf thrift-0.13.0.tar.gz`
  25. 4.`cd thrift-0.13.0`
  26. 5.`./configure --without-tests`
  27. 6.`make`
  28. 7.`make install`
  29. Check the version after installation is completethrift --version
  30. Note: If you have compiled Doris, you do not need to install thrift, you can directly use $DORIS_HOME/thirdparty/installed/bin/thrift

Execute following command in source dir:

  1. sh build.sh
  2. Usage:
  3. build.sh --flink version --scala version # specify flink and scala version
  4. build.sh --tag # this is a build from tag
  5. e.g.:
  6. build.sh --flink 1.14.3 --scala 2.12
  7. build.sh --tag
  8. Then, for example, execute the command to compile according to the version you need:
  9. sh build.sh --flink 1.14.3 --scala 2.12

After successful compilation, the file flink-doris-connector-1.14_2.12-1.0.0-SNAPSHOT.jar will be generated in the output/ directory. Copy this file to ClassPath in Flink to use Flink-Doris-Connector. For example, Flink running in Local mode, put this file in the lib/ folder. Flink running in Yarn cluster mode, put this file in the pre-deployment package.

Remarks:

  1. Doris FE should be configured to enable http v2 in the configuration

    conf/fe.conf

  1. enable_http_server_v2 = true

Using Maven

Add flink-doris-connector and necessary Flink Maven dependencies

  1. <dependency>
  2. <groupId>org.apache.flink</groupId>
  3. <artifactId>flink-java</artifactId>
  4. <version>${flink.version}</version>
  5. <scope>provided</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.flink</groupId>
  9. <artifactId>flink-streaming-java_${scala.version}</artifactId>
  10. <version>${flink.version}</version>
  11. <scope>provided</scope>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.flink</groupId>
  15. <artifactId>flink-clients_${scala.version}</artifactId>
  16. <version>${flink.version}</version>
  17. <scope>provided</scope>
  18. </dependency>
  19. <!-- flink table -->
  20. <dependency>
  21. <groupId>org.apache.flink</groupId>
  22. <artifactId>flink-table-planner_${scala.version}</artifactId>
  23. <version>${flink.version}</version>
  24. <scope>provided</scope>
  25. </dependency>
  26. <!-- flink-doris-connector -->
  27. <dependency>
  28. <groupId>org.apache.doris</groupId>
  29. <artifactId>flink-doris-connector-1.14_2.12</artifactId>
  30. <version>1.1.0</version>
  31. </dependency>

Notes

  1. Please replace the corresponding Connector and Flink dependency versions according to different Flink and Scala versions. Version 1.1.0 only supports Flink1.14

How to use

There are three ways to use Flink Doris Connector.

  • SQL
  • DataStream

Parameters Configuration

Flink Doris Connector Sink writes data to Doris by the Stream load, and also supports the configurations of Stream load, For specific parameters, please refer to here.

  • SQL configured by sink.properties. in the WITH
  • DataStream configured by DorisExecutionOptions.builder().setStreamLoadProp(Properties)

SQL

  • Source
  1. CREATE TABLE flink_doris_source (
  2. name STRING,
  3. age INT,
  4. price DECIMAL(5,2),
  5. sale DOUBLE
  6. )
  7. WITH (
  8. 'connector' = 'doris',
  9. 'fenodes' = 'FE_IP:8030',
  10. 'table.identifier' = 'database.table',
  11. 'username' = 'root',
  12. 'password' = 'password'
  13. );
  • Sink
  1. -- enable checkpoint
  2. SET 'execution.checkpointing.interval' = '10s';
  3. CREATE TABLE flink_doris_sink (
  4. name STRING,
  5. age INT,
  6. price DECIMAL(5,2),
  7. sale DOUBLE
  8. )
  9. WITH (
  10. 'connector' = 'doris',
  11. 'fenodes' = 'FE_IP:8030',
  12. 'table.identifier' = 'db.table',
  13. 'username' = 'root',
  14. 'password' = 'password',
  15. 'sink.label-prefix' = 'doris_label'
  16. );
  • Insert
  1. INSERT INTO flink_doris_sink select name,age,price,sale from flink_doris_source

DataStream

  • Source
  1. DorisOptions.Builder builder = DorisOptions.builder()
  2. .setFenodes("FE_IP:8030")
  3. .setTableIdentifier("db.table")
  4. .setUsername("root")
  5. .setPassword("password");
  6. DorisSource<List<?>> dorisSource = DorisSourceBuilder.<List<?>>builder()
  7. .setDorisOptions(builder.build())
  8. .setDorisReadOptions(DorisReadOptions.builder().build())
  9. .setDeserializer(new SimpleListDeserializationSchema())
  10. .build();
  11. env.fromSource(dorisSource, WatermarkStrategy.noWatermarks(), "doris source").print();
  • Sink

String Stream

  1. // enable checkpoint
  2. env.enableCheckpointing(10000);
  3. // using batch mode for bounded data
  4. env.setRuntimeMode(RuntimeExecutionMode.BATCH);
  5. DorisSink.Builder<String> builder = DorisSink.builder();
  6. DorisOptions.Builder dorisBuilder = DorisOptions.builder();
  7. dorisBuilder.setFenodes("FE_IP:8030")
  8. .setTableIdentifier("db.table")
  9. .setUsername("root")
  10. .setPassword("password");
  11. Properties properties = new Properties();
  12. /**
  13. json format to streamload
  14. properties.setProperty("format", "json");
  15. properties.setProperty("read_json_by_line", "true");
  16. **/
  17. DorisExecutionOptions.Builder executionBuilder = DorisExecutionOptions.builder();
  18. executionBuilder.setLabelPrefix("label-doris") //streamload label prefix
  19. .setStreamLoadProp(properties);
  20. builder.setDorisReadOptions(DorisReadOptions.builder().build())
  21. .setDorisExecutionOptions(executionBuilder.build())
  22. .setSerializer(new SimpleStringSerializer()) //serialize according to string
  23. .setDorisOptions(dorisBuilder.build());
  24. //mock string source
  25. List<Tuple2<String, Integer>> data = new ArrayList<>();
  26. data.add(new Tuple2<>("doris",1));
  27. DataStreamSource<Tuple2<String, Integer>> source = env.fromCollection(data);
  28. source.map((MapFunction<Tuple2<String, Integer>, String>) t -> t.f0 + "\t" + t.f1)
  29. .sinkTo(builder.build());

RowData Stream

  1. // enable checkpoint
  2. env.enableCheckpointing(10000);
  3. // using batch mode for bounded data
  4. env.setRuntimeMode(RuntimeExecutionMode.BATCH);
  5. //doris sink option
  6. DorisSink.Builder<RowData> builder = DorisSink.builder();
  7. DorisOptions.Builder dorisBuilder = DorisOptions.builder();
  8. dorisBuilder.setFenodes("FE_IP:8030")
  9. .setTableIdentifier("db.table")
  10. .setUsername("root")
  11. .setPassword("password");
  12. // json format to streamload
  13. Properties properties = new Properties();
  14. properties.setProperty("format", "json");
  15. properties.setProperty("read_json_by_line", "true");
  16. DorisExecutionOptions.Builder executionBuilder = DorisExecutionOptions.builder();
  17. executionBuilder.setLabelPrefix("label-doris") //streamload label prefix
  18. .setStreamLoadProp(properties); //streamload params
  19. //flink rowdata‘s schema
  20. String[] fields = {"city", "longitude", "latitude"};
  21. DataType[] types = {DataTypes.VARCHAR(256), DataTypes.DOUBLE(), DataTypes.DOUBLE()};
  22. builder.setDorisReadOptions(DorisReadOptions.builder().build())
  23. .setDorisExecutionOptions(executionBuilder.build())
  24. .setSerializer(RowDataSerializer.builder() //serialize according to rowdata
  25. .setFieldNames(fields)
  26. .setType("json") //json format
  27. .setFieldType(types).build())
  28. .setDorisOptions(dorisBuilder.build());
  29. //mock rowdata source
  30. DataStream<RowData> source = env.fromElements("")
  31. .map(new MapFunction<String, RowData>() {
  32. @Override
  33. public RowData map(String value) throws Exception {
  34. GenericRowData genericRowData = new GenericRowData(3);
  35. genericRowData.setField(0, StringData.fromString("beijing"));
  36. genericRowData.setField(1, 116.405419);
  37. genericRowData.setField(2, 39.916927);
  38. return genericRowData;
  39. }
  40. });
  41. source.sinkTo(builder.build());

General

KeyDefault ValueRequiredComment
fenodesYDoris FE http address, support multiple addresses, separated by commas
table.identifierYDoris table identifier, eg, db1.tbl1
usernameYDoris username
passwordYDoris password
doris.request.retries3NNumber of retries to send requests to Doris
doris.request.connect.timeout.ms30000NConnection timeout for sending requests to Doris
doris.request.read.timeout.ms30000NRead timeout for sending request to Doris
doris.request.query.timeout.s3600NQuery the timeout time of doris, the default is 1 hour, -1 means no timeout limit
doris.request.tablet.sizeInteger.MAX_VALUENThe number of Doris Tablets corresponding to an Partition. The smaller this value is set, the more partitions will be generated. This will increase the parallelism on the flink side, but at the same time will cause greater pressure on Doris.
doris.batch.size1024NThe maximum number of rows to read data from BE at one time. Increasing this value can reduce the number of connections between Flink and Doris. Thereby reducing the extra time overhead caused by network delay.
doris.exec.mem.limit2147483648NMemory limit for a single query. The default is 2GB, in bytes.
doris.deserialize.arrow.asyncfalseNWhether to support asynchronous conversion of Arrow format to RowBatch required for flink-doris-connector iteration
doris.deserialize.queue.size64NAsynchronous conversion of the internal processing queue in Arrow format takes effect when doris.deserialize.arrow.async is true
doris.read.fieldNList of column names in the Doris table, separated by commas
doris.filter.queryNFilter expression of the query, which is transparently transmitted to Doris. Doris uses this expression to complete source-side data filtering.
sink.label-prefixYThe label prefix used by stream load imports. In the 2pc scenario, global uniqueness is required to ensure the EOS semantics of Flink.
sink.properties.*NThe stream load parameters.

eg:
sink.properties.column_separator’ = ‘,’

Setting ‘sink.properties.escape_delimiters’ = ‘true’ if you want to use a control char as a separator, so that such as ‘\x01’ will translate to binary 0x01

Support JSON format import, you need to enable both ‘sink.properties.format’ =’json’ and ‘sink.properties.strip_outer_array’ =’true’
sink.enable-deletetrueNWhether to enable deletion. This option requires Doris table to enable batch delete function (0.15+ version is enabled by default), and only supports Uniq model.
sink.enable-2pctrueNWhether to enable two-phase commit (2pc), the default is true, to ensure Exactly-Once semantics. For two-phase commit, please refer to here.
sink.max-retries1NIn the 2pc scenario, the number of retries after the commit phase fails.
sink.buffer-size1048576(1MB)NWrite data cache buffer size, in bytes. It is not recommended to modify, the default configuration is sufficient.
sink.buffer-count3NThe number of write data cache buffers, it is not recommended to modify, the default configuration is sufficient.
Doris TypeFlink Type
NULL_TYPENULL
BOOLEANBOOLEAN
TINYINTTINYINT
SMALLINTSMALLINT
INTINT
BIGINTBIGINT
FLOATFLOAT
DOUBLEDOUBLE
DATEDATE
DATETIMETIMESTAMP
DECIMALDECIMAL
CHARSTRING
LARGEINTSTRING
VARCHARSTRING
DECIMALV2DECIMAL
TIMEDOUBLE
HLLUnsupported datatype
  1. SET 'execution.checkpointing.interval' = '10s';
  2. CREATE TABLE cdc_mysql_source (
  3. id int
  4. ,name VARCHAR
  5. ,PRIMARY KEY (id) NOT ENFORCED
  6. ) WITH (
  7. 'connector' = 'mysql-cdc',
  8. 'hostname' = '127.0.0.1',
  9. 'port' = '3306',
  10. 'username' = 'root',
  11. 'password' = 'password',
  12. 'database-name' = 'database',
  13. 'table-name' = 'table'
  14. );
  15. -- Support delete event synchronization (sink.enable-delete='true'), requires Doris table to enable batch delete function
  16. CREATE TABLE doris_sink (
  17. id INT,
  18. name STRING
  19. )
  20. WITH (
  21. 'connector' = 'doris',
  22. 'fenodes' = '127.0.0.1:8030',
  23. 'table.identifier' = 'database.table',
  24. 'username' = 'root',
  25. 'password' = '',
  26. 'sink.properties.format' = 'json',
  27. 'sink.properties.read_json_by_line' = 'true',
  28. 'sink.enable-delete' = 'true',
  29. 'sink.label-prefix' = 'doris_label'
  30. );
  31. insert into doris_sink select id,name from cdc_mysql_source;

Java example

samples/doris-demo/ An example of the Java version is provided below for reference, see here

Best Practices

Application scenarios

The most suitable scenario for using Flink Doris Connector is to synchronize source data to Doris (Mysql, Oracle, PostgreSQL) in real time/batch, etc., and use Flink to perform joint analysis on data in Doris and other data sources. You can also use Flink Doris Connector

Other

  1. The Flink Doris Connector mainly relies on Checkpoint for streaming writing, so the interval between Checkpoints is the visible delay time of the data.
  2. To ensure the Exactly Once semantics of Flink, the Flink Doris Connector enables two-phase commit by default, and Doris enables two-phase commit by default after version 1.1. 1.0 can be enabled by modifying the BE parameters, please refer to two_phase_commit.

common problem

  1. Bitmap type write
  1. CREATE TABLE bitmap_sink (
  2. dt int,
  3. page string,
  4. user_id int
  5. )
  6. WITH (
  7. 'connector' = 'doris',
  8. 'fenodes' = '127.0.0.1:8030',
  9. 'table.identifier' = 'test.bitmap_test',
  10. 'username' = 'root',
  11. 'password' = '',
  12. 'sink.label-prefix' = 'doris_label',
  13. 'sink.properties.columns' = 'dt,page,user_id,user_id=to_bitmap(user_id)'
  14. )
  1. errCode = 2, detailMessage = Label [label_0_1] has already been used, relate to txn [19650]

In the Exactly-Once scenario, the Flink Job must be restarted from the latest Checkpoint/Savepoint, otherwise the above error will be reported. When Exactly-Once is not required, it can also be solved by turning off 2PC commits (sink.enable-2pc=false) or changing to a different sink.label-prefix.