Kafka Integration

Apache Kafka is an open-source distributed event streaming platform. It can function as a messaging system for reading and writing data streams, enabling message publishing and subscription. It also supports building scalable stream-processing applications for real-time scenarios, and integrates with databases to securely store streaming data in a distributed, replicated, and fault-tolerant cluster. The "event streams" it produces act as the central nervous system of data transmission.

If you plan to write data from Kafka into a YMatrix cluster, this document is essential. YMatrix Database supports seamless Kafka integration, allowing continuous and automatic ingestion of Kafka data into YMatrix tables, with support for GUI-based operations.
Currently supported data formats are CSV and JSON. We will walk through a simple example to demonstrate how to integrate Kafka data using the YMatrix Management Platform.

1 Prerequisites

1.1 Set Up a Kafka Environment

First, set up a healthy Kafka environment by following the official guide: Kafka Quickstart.

1.2 Create a Kafka Topic

Now that you have set up Kafka on your server, enter the directory with the following command or by other means:

$ cd packages/kafka_2.13-3.2.0

Then start the Kafka service and create a test topic (Topic). The default port is 9092:

$ bin/kafka-topics.sh --create --topic csv_test --bootstrap-server localhost:9092

1.3 Write Test Data

Then write several test records one by one, ending with ctrl-c:

$ bin/kafka-console-producer.sh --topic csv_test --bootstrap-server localhost:9092
>1,Beijing,123.05,true,1651043583,1651043583123,1651043583123456
>2,Shanghai,112.95,true,1651043583,1651043583123,1651043583123456
>3,Shenzhen,100.0,false,1651043583,1651043583123,1651043583123456
>4,Guangxi,88.5,false,1651043583,1651043583123,1651043583123456
>^C

The above commands write four records to the newly created csv_test topic, each a comma-separated CSV line.
Preparation is complete. The following sections describe how to create a Kafka data stream to ingest data.

2 Creating a Kafka Data Stream

In a browser, enter the IP address (by default the IP of the master node) and the port number of the host where MatrixUI is located:

http://<IP>:8240

After a successful login, the main interface appears. The interface is divided into the Processes view and the Tasks view. The Processes view shows the running MatrixGate processes, and the Tasks view shows all data ingestion tasks. One MatrixGate process can contain multiple tasks.

Click "Import Data from Kafka".

Select the Kafka data source. The steps are first explained with a CSV file as the example.

2.1 CSV Data Stream

2.1.1 Select or Create a Target Table

First, you can select an existing table or create a new table as the target table that receives Kafka data.

2.1.1.1 Select an Existing Table

In the Target table field, select an existing table and the Importing mode. Here the selected table is assumed to be test in the public schema of the postgres database. After the selection, the table structure (field name, type, whether NULL is allowed, and whether the field is auto-increment) is listed below for field verification.

The Importing mode determines how duplicate data is handled. Choose it according to the constraints of the target table:

  • UPSERT: Combines UPDATE and INSERT so that a single SQL statement automatically inserts or updates: if the record does not exist, it is inserted; if it exists, it is updated. The target table must have a unique constraint that does not contain serial-type fields, and "Device ID (unique identifier)" must be specified in the mapping.
  • DEDUPLICATE: To ensure data uniqueness, when multiple records have the same unique-constraint field values, only the first record is kept. The target table must likewise have a unique constraint that does not contain serial-type fields.
  • INSERT: Data is not processed; it is imported in the order in which it is produced.

2.1.1.2 Create a New Table

For a new table, here the schema is assumed to be public and the table name test1.

When creating a new table, first select the Table type:

  • Partitioned table: Automatically partitioned by the time column; suitable for continuously written time-series data. A time column must be selected during field mapping.
  • Non-partitioned table: Creates an unpartitioned MARS3 table. No time column needs to be selected.

You do not define the table structure at this step. Field names and types are determined in step 3, "Mapping", through Automatic mapping or manual editing. Only the "UPSERT" and "INSERT" Importing modes are supported for new tables.

When "Partitioned table" is selected, the Automatic partition settings appear below:

When "Non-partitioned table" is selected, no automatic partitioning is involved:

After the table is created successfully, a red New marker appears.

Select the Importing mode and the Automatic partition strategy for it. Here the partition strategy uses auto_partitioning by default. For a non-partitioned table, only the Importing mode is selected; no Automatic partition strategy is involved.

2.1.2 Source data

After the target table is selected, the second step configures the source data connection. You must complete the following operations before proceeding to the next step:

  • First, in the left menu, enter the Kafka broker addresses and ports in the address box in the format shown in the figure. If there are multiple URLs, separate them with commas.
  • Then, select the Auth method required to connect to Kafka, and verify the username and password in Kafka.
  • Finally, in the right menu, select or search for the Kafka topics to connect to.

The address and authentication information can be saved as a "Kafka profile" for repeated use: after filling them in, click "Save as profile", enter a profile name, and save. When you create a data stream later, you can directly select a saved profile; the interface displays "Using profile: ". Click "Manual configuration" when you need to make a temporary change. Deleting a profile does not affect data streams that have already been created.

When GSSAPI is selected as the Auth method, you need to upload keytab and Kerberos files:

If the GSSAPI connection fails, check the DisablePAFXFAST option in the authentication area: when enabled, GSSAPI fast authentication (PAFXFAST) is not used. Some Kerberos environments require disabling fast authentication to connect.

2.1.3 Mapping

The third step configures the Sample data and maps the existing or new table to ingest Kafka data.

2.1.3.1 Basic Configuration

Select the CSV source data format and specify or configure the Sample data. You need to specify or manually configure the sample data format to ensure accurate mapping.

When configuring the Sample data, you can Select from topic or Edit data directly.

  • If the Kafka topic contains complete data, select it from the topic as the sample data.
  • When the data in the topic is incomplete (for example, when there are columns with empty values), you can Edit data directly and provide one sample record following the field structure of the target table. The sample data is used only for parsing and mapping the field structure; it is not imported into the target table.

Note!
Tables created through the GUI use the MARS3 storage engine by default. For more information, see MARS3.

2.1.3.2 Perform Mapping

After the basic mapping configuration is complete, map the source data fields to the target table fields. The operations differ between existing tables and new tables.

  • Mapping an existing table

    First select a Sample data record, then find the corresponding target field in the mapping relationship. Click Save after the settings are complete to establish a mapping.

    You can also perform one-click Automatic mapping, which is convenient while keeping the mapping correct. Starting from 6.8.4, field names generated by Automatic mapping drop redundant underscores for better readability.

    After Automatic mapping is complete, the mapping relationship displays the output value column by column, and you can Clear or Edit at any time:

  • Mapping a new table

    In addition to establishing mapping rules, a new table requires attention to the following settings: When the source field value is a UNIX timestamp and the target field type is timestamptz, a Store as timestamp checkbox appears to the right of the output value. When this checkbox is selected, the output value is automatically converted to the database timestamp format.

    After the mapping rules are saved, two settings must also be confirmed: "Device ID (unique identifier)" and "Time column":

    • Device ID (unique identifier): At least one must be selected in UPSERT mode, to identify the same record that needs to be updated. It is optional in INSERT mode; when selected, it is used for data distribution and sorting, and when it is not selected, the database uses the first column as the distribution key by default.
    • Time column: A partitioned table must have a valid time column selected (target table type timestamptz); a non-partitioned table does not need a time column.

    The mapping page of a new table has one more column than that of an existing table: "Device ID (unique identifier)":

2.1.4 Submission

Finally, review all configuration information of the data stream. If anything is wrong, go back and fix it; if everything is correct, submit.

2.1.4.1 Existing Table

The page displays all configuration information of the data stream. After confirming the information, click the "Submit" button.

2.1.4.2 New Table

Compared with an existing table, it additionally displays unique identifier information: for a partitioned table it is the combination "Device ID + Time column"; a non-partitioned table displays no unique identifier information. After confirming the information, click the "Submit" button.

The submission page in 6.8.4 also displays the Table type and Importing mode of the target table:

Whether for an existing table or a new table, a success page appears after a successful submission, and the Kafka data stream has been created successfully.

2.2 JSON Data Stream

This section mainly describes the differences from CSV ingestion, which are concentrated in the Mapping step.

  • First, select the JSON data format and configure the sample data.

  • Second, JSON columns are indexed with the $["column_name"] semantics. For the indexing rules, refer to JSONPath.

  • Finally, JSON data supports multi-level nesting. YMatrix supports selecting source data fields at different levels to create mapping rules. Click ⊕ in the Sample data column to expand the nested levels.

3 Managing Kafka Data Streams

After you create a Kafka data stream with the YMatrix GUI, data is continuously imported into YMatrix. If the import needs to be interrupted temporarily, for example for service debugging, you can pause or stop the task in the GUI, and later resume or start it.

For a running Kafka data stream, you can manage ingestion from two entrances: the Processes view and the Tasks view. The Processes view manages all tasks under a MatrixGate process, and the Tasks view manages each task individually.

Note!
Stop and pause operations may take 1-10 seconds to take effect. Do not perform other operations on the data stream during that time. After pausing or stopping completes, Kafka data ingestion stops. After resuming or starting, the data stream resumes ingestion immediately, and the page displays the latest ingestion information within at most 5 seconds.

3.1 Processes View

The Processes view lists, by MatrixGate process, the host name, PID, type, task status, throughput, and creation/exit time. Exited processes remain in the list.

"View" in the actions column opens the details page of the process, which contains three tabs: "Performance", "Active Tasks", and "Basic Information":

  • Performance: The data throughput and execution time of the process.
  • Active Tasks: The data source, target table, status, and throughput of each task under the process. In each task's actions column, you can "View" the task log, data volume, and ingestion details, and "Pause"/"Resume" the ingestion of that task.
  • Basic Information: The configuration information of the process.

The "···" on a list row manages all tasks under the process, and can delete the process record from the list:

  • Pause all tasks: Pauses all tasks under the process; they no longer accept ingestion.
  • Resume all tasks: Resumes all paused tasks under the process; ingestion continues.
  • Delete: Deletes the process record from the list; it is no longer displayed.

3.2 Tasks View

The expand button in the actions menu provides the Start, Reset offsets, Stop, and Remove task operations.

  • Start: Starts a stopped task and begins ingesting data.
  • Reset offsets: Resets the consumer offsets of a stopped task. See the section "Resetting Kafka Consumer Offsets" for details.
  • Stop: Stops the task; the associated MatrixGate process exits accordingly. It can be started again later.
  • Remove task: Removes the task from the list; it is no longer displayed.

The task list displays only tasks that are "Active in: last 1 hour" by default. Switch the time filter to view earlier tasks.

4 Resetting Kafka Consumer Offsets

If you need to re-consume from another position in Kafka, for example to re-import data after accidental deletion or to skip backlogged data, you can reset the consumer offsets of a stopped data stream. Supported since 6.8.4, this can be done in the Tasks view of the GUI, or with the mxgate offset-reset command.

Note!
Resetting offsets is an offline operation. Stop the task first, and confirm that all associated MatrixGate processes have exited before resetting offsets.

Note!
Re-consumption follows at-least-once delivery semantics. Make sure the target table can handle duplicate data, for example by using UPSERT or deduplicate mode.

4.1 Using the GUI

  1. Stop the target task in the Tasks view.

  2. Click "Reset offsets". On the "Reset consumer offsets" page, select where to re-consume from:

    • Earliest retained offset
    • Latest offset (skip backlog)
    • First message at or after a date and time
    • Specify offsets by partition, in a format such as 0:100,1:200

    When "First message at or after a date and time" is selected, specify a date and time:

  3. Click "Generate plan" to view the topic, consumer group, Target table, Retained start, Log end, Consumed to, Consume from, Catalog rows to purge, and other information. After confirming that everything is correct, click "Confirm reset".

  4. After the reset completes, start the task in the Tasks view to continue consuming from the new offsets.

4.2 Using the Command Line

mxgate offset-reset resets the consumer offsets of a stopped data stream. The command reads the data stream configuration from the configuration file specified by -C; it uses the file only to locate the data stream and does not modify it.

Prerequisites

  • All MatrixGate processes running the data stream have exited, and nothing (systemd, Kubernetes, etc.) restarts them before the command completes.
  • When a consumer group is used, the command checks whether the group has active members. A simple consumer that is still running cannot be detected by the command; you must confirm yourself that the process has exited.

Specifying the Target Position (exactly one of the four methods must be specified)

Parameter Description
--to-earliest Reset to the earliest offset still retained by Kafka
--to-latest Reset to the log end, skipping backlogged data
--to-datetime <time> Reset to the first message at or after that time. The time uses the RFC3339 format with a time zone, for example 2026-08-28T10:00:00+08:00
--to-offsets <partition:offset,...> Specify offsets by partition, for example 0:100,1:200

Selecting the Data Stream to Reset

--topic, --consumer-group, and --job are used only to match an existing data stream in the configuration file; they do not override or assemble configuration.

  • When the configuration file contains only one Kafka data stream, the command selects it automatically and no selection parameters are needed.
  • When there are multiple data streams, use --topic, --consumer-group, and --job to specify exactly one of them. A simple consumer has no consumer group; omit --consumer-group.
  • When only some selection conditions are given, when nothing matches, or when multiple streams match, the command refuses to run and lists the candidate configurations.

Other Parameters

Parameter Description
-C, --config Configuration file of the data stream
--dry-run Show the plan only, without modifying any data
-y, --yes Skip interactive confirmation; suitable for automation
--output Output format: text or json. json writes only a single object to stdout and must be used with --dry-run or -y

Example: Preview the Plan

When the configuration file contains only one data stream, no selection parameters are needed:

$ mxgate offset-reset -C mxgate.conf --to-earliest --dry-run

The command first warns that this is an offline operation, and then shows the plan. The following output is an example; actual output depends on your environment:

offset-reset is an offline command. Before continuing, make sure that
  - every mxgate instance running this flow is stopped and its process has exited;
  - nothing restarts it (systemd, Kubernetes, ...) until this command has succeeded or you have recovered from its error.
Only the consumer group is checked for live members; a simple consumer that is still running cannot be detected.

Offset reset plan of topic csv_test (job job_csv_to_public.demo_csv -> "public"."demo_csv", consumer group "", mode earliest):

partition  kafka-oldest   kafka-newest   consumed-to      consume-from   purge-rows
0          0              4              3                0              1

dry-run: nothing has been changed

Each row in the plan corresponds to one partition: kafka-oldest and kafka-newest are the offset range retained by Kafka, consumed-to is the currently consumed position, consume-from is where consumption starts after the reset, and purge-rows is the number of records that this reset removes from the consumption progress.

Example: Perform the Reset

When the configuration file contains multiple data streams, specify one with the selection parameters, and use -y to skip interactive confirmation:

$ mxgate offset-reset -C mxgate.toml \
    --topic orders \
    --consumer-group mxgate-orders \
    --job j_orders \
    --to-offsets '0:100' \
    -y

Execution and Recovery

  • A real run also shows the plan first and then waits for interactive confirmation. Data is modified only after confirmation.
  • After the run completes, restart the data stream to continue consuming from the reset position.
  • Writing the consumption progress to the database and committing the Kafka consumer group offsets are two independent operations and cannot be completed in the same transaction. The command commits and reads back the consumer group offsets within 30 seconds. When they are still unconfirmed, it returns a "partial success" error and gives the exact --to-offsets recovery parameters. Complete the recovery as prompted, and do not start the data stream until the Kafka offsets are confirmed to be aligned.

Scope of Impact

  • Messages in the Kafka topic are not modified, and Kafka's data retention policy is not changed.
  • Database modifications made by the reset are committed or rolled back as a whole in a single transaction.
  • For data streams that have never been reset, the start, consumption, and recovery semantics are unchanged.
  • Removed consumption progress and associated error records are archived and retained, and can be queried together via reset_id.