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: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 columnjson_root: Select the column where the JSON starts to be parsedstrip_outer_array: Crop the outermost array fieldstrict_mode: Strictly filter for column type conversion during import
Jsonpath.
Sample data:
Jsonpath.
The import results are as follows:
text
NULL will be added, or the default value can be added by ifnull.
Sample data:
text
text
Routine Load Import
Similar to stream load, the message content of Kafka data sources is treated as a complete JSON data.- 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.
- 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:
json_root and strip_outer_array = true to import data from data.
show routine load.