MySQL leads data to Redis

Source: Internet
Author: User
Tags redis version

A faster way to import MySQL data into Redis

1. When big data is imported to Redis at once

You can write all the insert commands into a TXT file, such as inserting key-value

Set TEST0 ABC set test1 BCD set test3 ABCD

  leave a space before each set command , save as Data.txt

Then use the Redis client REDIS-CLI pipeline Transfer (Redis version is greater than 2.6)

Use the command under Linux:

Cat data.txt | redis-cli--pipe

If successful, the following results will appear:

All data transferred. Waiting for the last reply ...

Last reply received from server.

errors:0, Replies: 3

2. Using data in Redis protocol format

Although the first method is convenient, the problem is that sometimes redis cannot interpret the data correctly, and all the recommended second ways

The format of this protocol data is as follows:

*3<cr><lf>$3<cr><lf>set<cr><lf>$3<cr><lf>key<cr><lf >$5<cr><lf>value<cr><lf>

The meanings are as follows:

The first line: *3<cr><lf>: Asterisk * is the specified format ,3 is the number of parameters (such as: SET, key, value);<CR > is ' \ r '; <lf> is ' \ n'.

Line second to third: $3<cr><lf>: $ is the prescribed format ;3 is the length of the corresponding command set (3 letters);<cr> <lf> Ibid.

So the above format can also be written as:

*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n

3. Using MySQL to import large amounts of data at once is the same principle

The data is exported in the format of the above protocol and then imported via REDIS-CLI--pipe

Example:

To build a table statement:

CREATE TABLE ' Device_type ' (  ' id ' int (ten) unsigned NOT NULL auto_increment,  ' name ' varchar ($) NOT null,  ' CL The ' class_guid ' varchar (+) NOT NULL,  '  PRIMARY key ' (' ID '),  key ' Idx_name ' (' name '),  KEY ' Idx_class ' (' Class ' (), ' Class_guid ' (200))) Engine=myisam auto_increment=287 DEFAULT Charset=utf8; SELECT * from Drivers_lookup_com.driver_files;

Export the data and convert it into a format that REDIS-CLI can handle:

Select CONCAT (   "*8\r\n",   ' $ ', Length (redis_cmd), ' \ r \ n ', Redis_cmd, ' \ r \ n ',   ' $ ', Length (redis_key), ' \ r \ n ', Redis_key, ' \ r \ n ',   ' $ ', LENGTH (hkey1), ' \ r \ n ', Hkey1, ' \ r \ n ', ' $ ', LENGTH (hval1), ' \ r \ n ', Hval1, ' \ r \ n '   $ ', Length (hkey2), ' \ r \ n ', Hkey2, ' \ r \ n ', ' $ ', Length (hval2), ' \ r \ n ', Hval2, ' \ r \ n '   $ ', LENGTH (hkey3), ' \ r \ n ', Hkey3, ' \ r \ n ', ' $ ', LENGTH (hval3), ' \ r \ n ', Hval3, ' \ R ') from (   SELECT ' Hmset ' as redis_cmd,   ID as Redis_key,   ' name ' as Hkey1, name as Hval1,   ' class ' as hkey2,class as Hval2,   ' Class_guid ' as hkey3,class_guid as Hval3 from   device _type) as T

The command to convert is as follows:

mysql-h192.168.0.104-uroot-p123 device--skip-column-names--raw < Device.sql | SRC/REDIS-CLI--pipe

  

MySQL leads data to Redis

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.