Skip to main content
The database provides a self-developed connector named StarRocks Connector for Apache Flink® (Flink connector for short) to help you read data in bulk from a cluster by using Flink. The Flink connector supports two reading methods: Flink SQL and Flink DataStream. Flink SQL is recommended.
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. - Flink connector of the database
  • 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. JDBC connector of Flink

Version requirements

Prerequisites

Flink has been deployed. If Flink has not been deployed, follow these steps to deploy it:
  1. 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:
  2. 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.
  3. Start your Flink cluster.
You can also deploy Flink by following the instructions provided in Flink documentation.

Before you begin

Follow these steps to deploy the Flink connector:
  1. 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.
  2. Place the Flink connector package you downloaded or compiled into the lib directory of Flink.
  3. Restart your Flink cluster.

Network configuration

Ensure that the machine where Flink is located can access the FE nodes of the cluster via the http_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. 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". 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 named test in your cluster and you have the permissions of user root.
NOTE If a read task fails, you must re-create it.

Data example

  1. Go to the test database and create a table named score_board.
  2. Insert data into the score_board table.
  3. Query the score_board table.
  1. In your Flink cluster, create a table named flink_test based on the schema of the source table (which is score_board in 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.
  2. Use SELECT to read data from the database.
When you read data by using Flink SQL, take note of the following points:
  • You can use only SQL statements like SELECT ... FROM <table_name> WHERE ... to read data from the database. Of all aggregate functions, only count is 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.
  1. Add the following dependencies to the pom.xml file:
    You must replace x.x.x in the preceding code example with the latest Flink connector version that you are using. See Version information.
  2. Call the Flink connector to read data from the database:

FAQ

You can set the BE configuration scan_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 the totalScannedRows 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.