Importing MySQL data into Redis

Source: Internet
Author: User

Simulate the client REDIS-CLI implementation of Redis with a script: (Note: MySQL address is 10.86.30.203 Redis address is 10.86.31.50)
1. First determine the columns of the MySQL table:

desc  test;+-----------+-------------+------+-----+---------+-------+| Field     | Type        | Null | Key | Default | Extra |+-----------+-------------+------+-----+---------+-------+| id    | int(11)     | YES  |     | NULL    |       || test1  | varchar(50) | YES  |     | NULL    |       || test2  | varchar(64) | YES  |     | NULL    |       || test3 | int(11)     | YES  |     | NULL    |       |+-----------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)

2. Write the SQL script:

vi /home/darren/test.sqlSELECT 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, CONCAT(‘id:‘,id) AS redis_key,‘test1‘ AS hkey1,test1  AS hval1,‘test2‘ AS hkey2,test2 AS hval2,‘test3‘ AS hkey3,test3 AS hval3 FROM test ) AS t

Note: Where *8 means a total of 8 parameters, $length () is the length of the parameter. The TEST1,TEST2,TEST3 in the script are all MySQL column names and can be modified by themselves.

3. Execute the Import command:

mysql -uroot -pxxx -h 10.86.30.203 test --skip-column-names --raw < /home/darren/test.sql | redis-cli  --pipe----------------------------------------------------------------------------------------------------------------mysql: [Warning] Using a password on the command line interface can be insecure.All data transferred. Waiting for the last reply...Last reply received from server.errors: 0, replies: 1

The errors above appears to be 0 for import success.

–raw: Make MySQL do not convert the line break in field values.
–skip-column-names: Does not include the column name in each line of MySQL output.

Importing MySQL data into 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.