One-step migration from MySQL to Redis

Source: Internet
Author: User

When you migrate a large table from MySQL to Redis, you may find that every time you extract, convert, and import a piece of data, it is unbearable! Here is a tip: You can use pipelines to directly input MySQL output to the redis-cli input, which enables both databases to run at their top speed.

Using this technology, I reduced the time for importing 8 million pieces of MySQL Data to Redis from 90 minutes to two minutes.

Data Protocol from Mysql to Redis

The redis-cli command line tool has a batch insert mode, which is specially designed for batch command execution. The first step is to format the Mysql query content into the available data format of redis-cli. Here we go!

My statistical tables:

 
 
  1. CREATE TABLE events_all_time (  
  2.   id int(11) unsigned NOT NULL AUTO_INCREMENT,  
  3.   action varchar(255) NOT NULL,  
  4.   count int(11) NOT NULL DEFAULT 0,  
  5.   PRIMARY KEY (id),  
  6.   UNIQUE KEY uniq_action (action)  
  7. ); 

The redis command to be executed in each row of data is as follows:

 
 
  1. HSET events_all_time [action] [count] 

Follow the above redis command rules to create an events_to_redis. SQL file, which is used to generate an SQL statement in the redis Data Protocol format:

 
 
  1. -- events_to_redis.sql  
  2.  
  3. SELECT CONCAT(  
  4.   "*4\r\n",  
  5.   '$', LENGTH(redis_cmd), '\r\n',  
  6.   redis_cmd, '\r\n',  
  7.   '$', LENGTH(redis_key), '\r\n',  
  8.   redis_key, '\r\n',  
  9.   '$', LENGTH(hkey), '\r\n',  
  10.   hkey, '\r\n',  
  11.   '$', LENGTH(hval), '\r\n',  
  12.   hval, '\r' 
  13. )  
  14. FROM (  
  15.   SELECT 
  16.   'HSET' as redis_cmd,  
  17.   'events_all_time' AS redis_key,  
  18.   action AS hkey,  
  19.   count AS hval  
  20.   FROM events_all_time  
  21. ) AS t 

OK, run the following command:

 
 
  1. mysql stats_db --skip-column-names --raw < events_to_redis.sql | redis-cli --pipe 

Important mysql parameter descriptions:

-- Raw: prevents mysql from converting line breaks in field values.

-- Skip-column-names: so that each line output by mysql does not contain column names.

Source: http://www.oschina.net/translate/mysql-to-redis-in-one-step

MySQL to Redis in One Step

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.