Skip to main content
You can import semi-structured data (for example, JSON) by using stream load or routine load.

Use Scenarios

  • Stream Load: For JSON data stored in text files, use stream load to import.
  • Routine Load: For JSON data in Kafka, use routine load to import.

Stream Load Import

Sample data:
Example:
The format: json parameter allows you to execute the format of the imported data. jsonpaths is used to execute the corresponding data import path. Related parameters:
  • jsonpaths: Select the JSON path for each column
  • json_root: Select the column where the JSON starts to be parsed
  • strip_outer_array: Crop the outermost array field
  • strict_mode: Strictly filter for column type conversion during import
When the JSON data schema and data schema are not exactly the same, modify the Jsonpath. Sample data:
Import example:
The ETL operation of multiplying k1 by 100 is performed during the import, and the column is matched with the original data by Jsonpath. The import results are as follows:
text
For missing columns, if the column definition is nullable, then NULL will be added, or the default value can be added by ifnull. Sample data:
Import Example-1:
The import results are as follows:
text
Import Example-2:
The import results are as follows:
text

Routine Load Import

Similar to stream load, the message content of Kafka data sources is treated as a complete JSON data.
  1. If a message contains multiple rows of data in array format, all rows will be imported and Kafka’s offset will only be incremented by 1.
  2. If a JSON in Array format represents multiple rows of data, but the parsing of the JSON fails due to a JSON format error, the error row will only be incremented by 1 (given that the parsing fails, the database cannot actually determine how many rows of data it contains, and can only record the error data as one row).

Use Canal to import the database from MySQL with incremental sync binlogs

Canal is an open-source MySQL binlog synchronization tool from Alibaba, through which we can synchronize MySQL data to Kafka. The data is generated in JSON format in Kafka. Here is a demonstration of how to use routine load to synchronize data in Kafka for incremental data synchronization with MySQL.
  • In MySQL we have a data table with the following table creation statement.
  • Prerequisite: Make sure MySQL has binlog enabled and the format is ROW.
  • Create an account and grant privileges to the secondary MySQL server:
  • Then download and install Canal.
  • Modify the configuration (MySQL related).
$ vi conf/example/instance.properties
  • Modify the configuration (Kafka related).
$ vi /usr/local/canal/conf/canal.properties
  • Initiation
bin/startup.sh The corresponding synchronization log is shown in logs/example/example.log and in Kafka, with the following format:
Add json_root and strip_outer_array = true to import data from data.
This completes the near real-time synchronization of data from MySQL to the database. View status and error messages of the import job by show routine load.