Synchronizing Kafka Data to ClickHouse

This section describes how to create a Kafka table to automatically synchronize Kafka data to the ClickHouse cluster.

Prerequisites

Constraints

Currently, ClickHouse cannot interconnect with Kafka clusters with security mode enabled.

Syntax of the Kafka Table

How to Synchronize Kafka Data to ClickHouse

  1. Switch to the Kafka client installation directory. For details, see Using the Kafka Client.

    1. Log in to the node where the Kafka client is installed as the Kafka client installation user.
    2. Run the following command to go to the client installation directory:

      cd /opt/client

    3. Run the following command to configure environment variables:

      source bigdata_env

    4. If Kerberos authentication is enabled for the current cluster, run the following command to authenticate the current user. If Kerberos authentication is disabled for the current cluster, skip this step.
      1. Run the following command first for an MRS 3.1.0 cluster:

        export CLICKHOUSE_SECURITY_ENABLED=true

      2. kinit Component service user

  2. Run the following command to create a Kafka topic. For details, see Managing Kafka Topics.

    kafka-topics.sh --topic kafkacktest2 --create --zookeeper IP address of the Zookeeper role instance:2181/kafka --partitions 2 --replication-factor 1

    • --topic is the name of the topic to be created, for example, kafkacktest2.
    • --zookeeper is the IP address of the node where the ZooKeeper role instances are located, which can be the IP address of any of the three role instances. You can obtain the IP address of the node by performing the following steps:
      • For versions earlier than MRS 3.x, click the cluster name to go to the cluster details page and choose Components > ZooKeeper > Instances. View the IP addresses of the ZooKeeper role instances.
      • For MRS 3.x or later, log in to FusionInsight Manager. For details, see Accessing FusionInsight Manager (MRS 3.x or Later). Choose Cluster > Name of the desired cluster > Services > ZooKeeper > Instance. View the IP addresses of the ZooKeeper role instances.
    • --partitions and --replication-factor are the topic partitions and topic backup replicas, respectively. The number of the two parameters cannot exceed the number of Kafka role instances.

  3. Log in to the ClickHouse client by referring to Using ClickHouse from Scratch.

    1. Run the following command to go to the client installation directory:

      cd /opt/Bigdata/client

    2. Run the following command to configure environment variables:

      source bigdata_env

    3. If Kerberos authentication is enabled for the current cluster, run the following command to authenticate the current user. The user must have the permission to create ClickHouse tables. Therefore, you need to bind the corresponding role to the user. For details, see ClickHouse User and Permission Management. If Kerberos authentication is disabled for the current cluster, skip this step.

      kinit Component service user

      Example: kinit clickhouseuser

    4. Run the following command to connect to the ClickHouse instance node to which data is to be imported:

      clickhouse client --host IP address of the ClickHouse instance --user Login username --password --port ClickHouse port number --database Database name --multiline

      Enter the user password.

  4. Create a Kafka table in ClickHouse by referring to Syntax of the Kafka Table. For example, the following table creation statement is used to create a Kafka table whose name is kafka_src_tbl3, topic name is kafkacktest2, and message format is JSONEachRow in the default database.

    create table kafka_src_tbl3 on cluster default_cluster 
    (id UInt32, age UInt32, msg String)  
    ENGINE=Kafka() 
    SETTINGS 
     kafka_broker_list='IP address 1 of the Kafka broker instance:9092,IP address 2 of the Kafka broker instance:9092,IP address 3 of the Kafka broker instance:9092',
     kafka_topic_list='kafkacktest2',
     kafka_group_name='cg12',
     kafka_format='JSONEachRow';

  5. Create a ClickHouse replicated table, for example, the ReplicatedMergeTree table named kafka_dest_tbl3.

    create table kafka_dest_tbl3 on cluster default_cluster 
    ( id UInt32, age UInt32, msg String )
    engine = ReplicatedMergeTree('/clickhouse/tables/{shard}/default/kafka_dest_tbl3', '{replica}')
    partition by age 
    order by id;

  6. Create a materialized view, which converts data in Kafka in the background and saves the data to the created ClickHouse table.

    create materialized view consumer3 on cluster default_cluster to kafka_dest_tbl3 as select * from kafka_src_tbl3;

  7. Perform 1 again to go to the Kafka client installation directory.
  8. Run the following command to send a message to the topic created in 2:

    kafka-console-producer.sh --broker-list IP address 1 of the kafka broker instance:9092,IP address 2 of the kafka broker instance:9092,IP address 3 of the kafka broker instance:9092 --topic kafkacktest2
    >{"id":31, "age":30, "msg":"31 years old"}
    >{"id":32, "age":30, "msg":"31 years old"}
    >{"id":33, "age":30, "msg":"31 years old"}
    >{"id":35, "age":30, "msg":"31 years old"}

  9. Use the ClickHouse client to log in to the ClickHouse instance node in 3 and query the ClickHouse table data, for example, to query the replicated table kafka_dest_tbl3. It shows that the data in the Kafka message has been synchronized to this table.

    select * from kafka_dest_tbl3;