Creating a FlinkServer Job to Interconnect with a Kafka Message Queue
This section applies to MRS 3.1.2 or later.
Scenarios
This section describes the data definition language (DDL) of Kafka as a source or sink table, as well as the WITH parameters and example code for creating a table, and provides guidance on how to perform operations on the FlinkServer job management page.
If your Kafka cluster is in security mode, the following example SQL statements can be used.
Prerequisites
- The HDFS, YARN, Kafka, and Flink services have been installed in a cluster.
- The client that contains the Kafka service has been installed, for example, in the /opt/client directory.
- You have created a user (for example, flink_admin) with FlinkServer administrator permissions for accessing the Flink web UI. For details, see Creating a FlinkServer Role.
Creating a Job
- Log in to Manager as user flink_admin and choose Cluster > Services > Flink. In the Basic Information area, click the link on the right of Flink WebUI to access the Flink web UI.
- Create a streaming Flink SQL job by referring to Creating a FlinkServer Job. On the job development page, configure the job parameters as follows and start the job. In Basic Parameter, select Enable CheckPoint, set Time Interval(ms) to 60000, and retain the default value for Mode.
CREATE TABLE KafkaSource ( `user_id` VARCHAR, `user_name` VARCHAR, `age` INT ) WITH ( 'connector' = 'kafka', 'topic' = 'test_source', 'properties.bootstrap.servers' = 'Service IP address of the Kafka Broker instance:Kafka port', 'properties.group.id' = 'testGroup', 'scan.startup.mode' = 'latest-offset', 'format' = 'csv', 'properties.sasl.kerberos.service.name' = 'kafka',--This parameter is not required for clusters in normal mode. Delete the comma (,) in the previous line. 'properties.security.protocol' = 'SASL_PLAINTEXT',--This parameter is not required for clusters in normal mode. 'properties.kerberos.domain.name' = 'hadoop.System domain name'--This parameter is not required for clusters in normal mode. ); CREATE TABLE KafkaSink( `user_id` VARCHAR, `user_name` VARCHAR, `age` INT ) WITH ( 'connector' = 'kafka', 'topic' = 'test_sink', 'properties.bootstrap.servers' = 'Service IP address of the Kafka Broker instance:Kafka port', 'scan.startup.mode' = 'latest-offset', 'value.format' = 'csv', 'properties.sasl.kerberos.service.name' = 'kafka',--This parameter is not required for clusters in normal mode. Delete the comma (,) in the previous line. 'properties.security.protocol' = 'SASL_PLAINTEXT',--This parameter is not required for clusters in normal mode. 'properties.kerberos.domain.name' = 'hadoop.System domain name'--This parameter is not required for clusters in normal mode. ); Insert into KafkaSink select * from KafkaSource;
- The Kafka Broker instance IP address and Kafka port number are as follows:
- To obtain the instance IP address, log in to FusionInsight Manager, choose Cluster > Services > Kafka, click Instances, and query the instance IP address on the instance list page.
- Value of sasl.port when Authentication Mode of the cluster is Security Mode. The default value is 21007.
- Value of port when Authentication Mode of the cluster is Normal Mode. The default value is 9092. If the port number is set to 9092, set allow.everyone.if.no.acl.found to true. The procedure is as follows:
Log in to FusionInsight Manager and choose Cluster > Services > Kafka. Click Configurations and then All Configurations. On the displayed page, search for allow.everyone.if.no.acl.found, set it to true, and click Save.
- System domain name: You can log in to FusionInsight Manager, choose System > Permission > Domain and Mutual Trust, and check the value of Local Domain.
- You need to restart Flink jobs after expanding a Kafka Topic partition if you are using Flink 1.15.0 or an earlier version. Otherwise, new partitions may not be detected and consumption data may be missed. Alternatively, you can enable Kafka Topic partition detection in Flink.
You can add the scan.topic-partition-discovery.interval parameter to the WITH property of the SQL Kafka source table and set the parameter to a dynamic refresh interval, for example, 5min.
- The Kafka Broker instance IP address and Kafka port number are as follows:
- On the job management page, check whether the job status is Running.
- Run the following command to check whether data is received in the sink table, that is, check whether data is properly written to the Kafka topic after 5 is performed. For details, see Using Kafka to Produce Consumption Data.
sh kafka-console-consumer.sh --topic test_sink --bootstrap-server Service IP address of the Kafka Broker instance:Kafka port --consumer.config /opt/client/Kafka/kafka/config/consumer.properties - View the topic and write data to the Kafka topic by referring to Using Kafka to Produce Consumption Data. After the data is written, view the execution result in the window in 4.
Check the Kafka topic.
./kafka-topics.sh --list --bootstrap-server Service IP address of the Kafka Broker instance:Kafka port --command-config Client directory/Kafka/kafka/config/client.properties
Write data to Kafka.
sh kafka-console-producer.sh --broker-list IP address of the node where the Kafka instance is deployed:Kafka port --topic Topic name --producer.config Client directory/Kafka/kafka/config/producer.properties
In this example, the topic name is test_source.
sh kafka-console-producer.sh --broker-list IP address of the node where the Kafka instance is deployed:Kafka port --topic test_source --producer.config /opt/client/Kafka/kafka/config/producer.propertiesEnter the message content.1,clw,33
Press Enter to send the message.
WITH Parameters
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| connector | Yes | String | Connector to be used. kafka is used for Kafka. |
| topic |
| String | Topic name.
|
| topic-pattern | No (Kafka functions as a source table.) | String | Topic pattern. This parameter is available when Kafka is used as a source table. The topic name must be a regular expression. topic-pattern and topic cannot be set at the same time. |
| properties.bootstrap.servers | Yes | String | List of Kafka brokers, which are separated by commas (,). |
| properties.group.id | Yes (Kafka functions as a source table.) | String | Kafka user group ID. |
| format | Yes | String | Format used to deserialize and serialize the value part of Kafka messages. |
| properties.* | No | String | Authentication-related parameters that need to be added in security mode. |
| scan.topic-partition-discovery.interval | No | Duration | Interval at which the consumer dynamically discovers the created partition. The default value is 5 minutes. |
Initial Consumption Offset
Startup Mode
The scan.startup.mode configuration item determines the startup mode of the Kafka Consumer. Valid values include:
- group-offsets: Read from the committed offset of a specified group ID. You can use properties.group.id to specify a group ID.
- earliest-offset: Start from the earliest offset possible.
- latest-offset: Start from the latest offset.
- timestamp: Read from the first message whose timestamp is later than or equal to the specified time. You can use scan.startup.timestamp-millis to specify the timestamp.
- specific-offsets: Consume from a specific partition offset. You can use scan.startup.specific-offsets to specify the value.
Initial Offset Priority
- Offsets in the checkpoint or savepoint.
- You can use scan.startup.mode to specify the initial offset for the WITH parameter.
- If scan.startup.mode is not specified, use group-offsets to start consumption.
- If scan.startup.mode is not specified, the system starts from the committed offset group-offsets by default. A common case is to use a new group ID to start consumption. The source table queries the committed offset of the group from the Kafka cluster. Because the group ID is used for the first time, no valid offset is found. Then, the policy configured by using properties.auto.offset.reset is used for resetting. Therefore, when a new group ID is used for consumption, properties.auto.offset.reset must be configured to specify the offset resetting policy. If you do not set this configuration item, the exception org.apache.kafka.clients.consumer.NoOffsetForPartitionException: Undefined offset with no reset policy for partitions will be generated, asking you to perform required operations.
- If timestamp is used, another configuration item scan.startup.timestamp-millis must be used to specify a timestamp in milliseconds since the Unix epoch (January 1, 1970, 00:00:00.000 UTC) as the start time.
- If specific-offsets is used, another configuration item scan.startup.specific-offsets must be used to specify the initial offset for each partition. For example, the value partition:0,offset:42;partition:1,offset:300 indicates that partition 0 starts from offset 42 and partition 1 starts from offset 300.
Sample code:
CREATE TABLE kafka_source ( ... ) WITH ( 'connector' = 'kafka', ... -- Example of the initial offset. You can select one based on service requirements. -- Start consumption from the earliest offset. 'scan.startup.mode' = 'earliest-offset', -- Start consumption from the latest offset. 'scan.startup.mode' = 'latest-offset', --Start consumption from the committed offset of consumer group my-group. 'properties.group.id' = 'my-group', 'scan.startup.mode' = 'group-offsets', 'properties.auto.offset.reset' = 'earliest', --If my-group is used for the first time, the consumption starts from the earliest offset. 'properties.auto.offset.reset' = 'latest', -- If my-group is used for the first time, the consumption starts from the latest offset. -- Consumption starts from the specified timestamp 1655395200000 (in milliseconds). 'scan.startup.mode' = 'timestamp', 'scan.startup.timestamp-millis' = '1655395200000', -- Consumption starts from a specified offset. 'scan.startup.mode' = 'specific-offsets', 'scan.startup.specific-offsets' = 'partition:0,offset:42;partition:1,offset:300' );
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot