NOTE The Flink connector also supports writing the data read by Flink to another cluster or storage system. See Continuously load data from Apache Flink®.
Background information
Unlike the JDBC connector provided by Flink, the Flink connector of the database supports reading data from multiple BEs of your cluster in parallel, greatly accelerating read tasks. The following comparison shows the difference in implementation between the two connectors.-
Flink connector of the database
With the Flink connector of the database, Flink can first obtain the query plan from the responsible FE, then distribute the obtained query plan as parameters to all the involved BEs, and finally obtain the data returned by the BEs.

-
JDBC connector of Flink
With the JDBC connector of Flink, Flink can only read data from individual FEs, one at a time. Data reads are slow.

Version requirements
Prerequisites
Flink has been deployed. If Flink has not been deployed, follow these steps to deploy it:-
Install Java 8 or Java 11 in your operating system to ensure Flink can run properly. You can use the following command to check the version of your Java installation:
For example, if the following information is returned, Java 8 has been installed:
-
Download and unzip the Flink package of your choice.
NOTE We recommend that you use Flink v1.14 or later. The minimum Flink version supported is v1.11.
-
Start your Flink cluster.
Before you begin
Deploy the Flink connector
Follow these steps to deploy the Flink connector:-
Select and download the flink-connector-starrocks JAR package matching the Flink version that you are using. If code debugging is needed, compile the Flink connector package to suit your business requirements.
NOTICE We recommend that you download the Flink connector package whose version is 1.2.x or later and whose matching Flink version has the same first two digits as the Flink version that you are using. For example, if you use Flink v1.14.x, you can download
flink-connector-starrocks-1.2.4_flink-1.14_x.yy.jar. -
Place the Flink connector package you downloaded or compiled into the
libdirectory of Flink. - Restart your Flink cluster.
Network configuration
Ensure that the machine where Flink is located can access the FE nodes of the cluster via thehttp_port (default: 8030) and query_port (default: 9030), and the BE nodes via the be_port (default: 9060).
Parameters
Common parameters
The following parameters apply to both the Flink SQL and Flink DataStream reading methods.Parameters for Flink DataStream
The following parameters apply only to the Flink DataStream reading method.
Assume that in Flink you create a table that consists of three columns, which are
c1, c2, c3. To read the rows whose values in the c1 column of this Flink table are equal to 100, you can specify two filter conditions "scan.columns, "c1" and "scan.filter, "c1 = 100".
Data type mapping between the database and Flink
The following data type mapping is valid only for Flink reading data from the database. For the data type mapping used for Flink writing data into the database, see Continuously load data from Apache Flink®.Examples
The following examples assume you have created a database namedtest in your cluster and you have the permissions of user root.
NOTE If a read task fails, you must re-create it.
Data example
-
Go to the
testdatabase and create a table namedscore_board. -
Insert data into the
score_boardtable. -
Query the
score_boardtable.
Read data using Flink SQL
-
In your Flink cluster, create a table named
flink_testbased on the schema of the source table (which isscore_boardin this example). In the table creation command, you must configure the read task properties, including the information about the Flink connector, the source StarRock database, and the source table. -
Use SELECT to read data from the database.
- You can use only SQL statements like
SELECT ... FROM <table_name> WHERE ...to read data from the database. Of all aggregate functions, onlycountis supported. - Predicate pushdown is supported. For example, if your query contains a filter condition
char_1 <> 'A' and int_1 = -126, the filter condition will be pushed down to the Flink connector and transformed into a statement that can be executed by the database before the query is run. You do not need to perform extra configurations. - The LIMIT statement is not supported.
- The database does not support the checkpointing mechanism. As a result, data consistency cannot be guaranteed if the read task fails.
- The order of the fields in the created table must be the same as in the table.
Read data using Flink DataStream
-
Add the following dependencies to the
pom.xmlfile:You must replacex.x.xin the preceding code example with the latest Flink connector version that you are using. See Version information. -
Call the Flink connector to read data from the database:
FAQ
I got an error “Failed to get next from be” while exporting data with Flink Connector. What should I do?
You can set the BE configurationscan_context_gc_interval_min (Default: 5, Unit: Minutes) to a larger value to increase the time interval at which the Scan Context is cleaned.
What’s next
After Flink successfully reads data from the database, you can use the Flink WebUI to monitor the read task. For example, you can view thetotalScannedRows metric on the Metrics page of the WebUI to obtain the number of rows that are successfully read. You can also use Flink SQL to perform calculations such as joins on the data you have read.