Is there an easy way to turn a mysql table into a redis equivalent?
有没有一种简单的方法可以将mysql表转换为redis等效表?
I have a myisam table in MySQL that is basically used as a key-value store that I want to "move" to Redis so it will be super fast. Is there an easy way to do this?
我在MySQL中有一个myisam表,它基本上用作键值存储,我想“移动”到Redis,所以它会超级快。是否有捷径可寻?
Thanks.
3 个解决方案
#1
8
The easiest way is to get a dump of the mysql table and parse the relevant data entries into redis commands.
最简单的方法是获取mysql表的转储并将相关数据条目解析为redis命令。
For example, a data dump would produce something like the following:
例如,数据转储会产生如下内容:
CREATE TABLE carousel(
id int(11),
path varchar(200),
title varchar(200),
comments varchar(200)
);
INSERT INTO carousel VALUES (3,'7.jpg','Inspirar','inspiration');
INSERT INTO carousel VALUES (4,'d.jpg','Pilotar','pilotar');
INSERT INTO carousel VALUES (5,'8.jpg','Sentir','sentir');
INSERT INTO carousel VALUES (6,'6.jpg','Volar','volar');
First you need to decide on the key structure you want to use in redis. One idea is to use the table as the key and store the ids of each row in a set.
首先,您需要决定要在redis中使用的密钥结构。一种想法是使用表作为键并将每行的ID存储在一个集合中。
For each table you would need to create a key that will hold the ids, lets call them idx:$table. Using our example we would create idx:carousel.
对于每个表,您需要创建一个容纳ID的键,让我们称之为idx:$ table。使用我们的示例,我们将创建idx:carousel。
As we parse the file, we would pull the ids from the first column of values (in this case) and store them in idx:carousel. Also we would store each INSERT as a hash. To do this we name the key carousel:$id and use the command hmset. The first INSERT in the example would be stored like this:
当我们解析文件时,我们会从第一列值中拉出id(在本例中)并将它们存储在idx:carousel中。我们还将每个INSERT存储为哈希。为此,我们将密钥轮播命名为:$ id并使用命令hmset。示例中的第一个INSERT将存储如下:
hmset carousel:3 path '7.jpg' title 'Inspirar' comments 'inspiration'
hmset carousel:3路'7.jpg'标题'Inspirar'评论'灵感'
It probably sounds more complicated than it actually is, but it is quite straightforward. If you think it is a little too difficult I am willing to write one for you.
它可能听起来比实际上更复杂,但它非常简单。如果你认为这有点太困难,我愿意为你写一个。
Hope that helps.
希望有所帮助。
#2
0
I don't know of any utilities that will perform such a thing automatically, you'll likely have to write one yourself. You can get a client library for your preferred language at http://redis.io/clients.
我不知道任何会自动执行此类操作的实用程序,您可能必须自己编写一个。您可以在http://redis.io/clients上获取首选语言的客户端库。
It sounds like a fairly straightforward task, but the ease will depend entirely on your comfort and experience with the language you write it in.
这听起来像是一项相当简单的任务,但轻松将完全取决于您使用您所编写的语言的舒适度和体验。
#3
0
Create Redis lists of each column and put all data of each column in that list. and make hashes to store the lists of each table. e.g. a table having columns email, id, username, surname will can be stored in redis by creating lists of email, id ,username and surname and a hashes which will contain email, id, username.
创建每列的Redis列表,并将每列的所有数据放入该列表中。并使哈希值存储每个表的列表。例如通过创建电子邮件,ID,用户名和姓氏列表以及包含电子邮件,ID,用户名的哈希,可以将包含列电子邮件,ID,用户名,姓氏的表格存储在redis中。
#1
8
The easiest way is to get a dump of the mysql table and parse the relevant data entries into redis commands.
最简单的方法是获取mysql表的转储并将相关数据条目解析为redis命令。
For example, a data dump would produce something like the following:
例如,数据转储会产生如下内容:
CREATE TABLE carousel(
id int(11),
path varchar(200),
title varchar(200),
comments varchar(200)
);
INSERT INTO carousel VALUES (3,'7.jpg','Inspirar','inspiration');
INSERT INTO carousel VALUES (4,'d.jpg','Pilotar','pilotar');
INSERT INTO carousel VALUES (5,'8.jpg','Sentir','sentir');
INSERT INTO carousel VALUES (6,'6.jpg','Volar','volar');
First you need to decide on the key structure you want to use in redis. One idea is to use the table as the key and store the ids of each row in a set.
首先,您需要决定要在redis中使用的密钥结构。一种想法是使用表作为键并将每行的ID存储在一个集合中。
For each table you would need to create a key that will hold the ids, lets call them idx:$table. Using our example we would create idx:carousel.
对于每个表,您需要创建一个容纳ID的键,让我们称之为idx:$ table。使用我们的示例,我们将创建idx:carousel。
As we parse the file, we would pull the ids from the first column of values (in this case) and store them in idx:carousel. Also we would store each INSERT as a hash. To do this we name the key carousel:$id and use the command hmset. The first INSERT in the example would be stored like this:
当我们解析文件时,我们会从第一列值中拉出id(在本例中)并将它们存储在idx:carousel中。我们还将每个INSERT存储为哈希。为此,我们将密钥轮播命名为:$ id并使用命令hmset。示例中的第一个INSERT将存储如下:
hmset carousel:3 path '7.jpg' title 'Inspirar' comments 'inspiration'
hmset carousel:3路'7.jpg'标题'Inspirar'评论'灵感'
It probably sounds more complicated than it actually is, but it is quite straightforward. If you think it is a little too difficult I am willing to write one for you.
它可能听起来比实际上更复杂,但它非常简单。如果你认为这有点太困难,我愿意为你写一个。
Hope that helps.
希望有所帮助。
#2
0
I don't know of any utilities that will perform such a thing automatically, you'll likely have to write one yourself. You can get a client library for your preferred language at http://redis.io/clients.
我不知道任何会自动执行此类操作的实用程序,您可能必须自己编写一个。您可以在http://redis.io/clients上获取首选语言的客户端库。
It sounds like a fairly straightforward task, but the ease will depend entirely on your comfort and experience with the language you write it in.
这听起来像是一项相当简单的任务,但轻松将完全取决于您使用您所编写的语言的舒适度和体验。
#3
0
Create Redis lists of each column and put all data of each column in that list. and make hashes to store the lists of each table. e.g. a table having columns email, id, username, surname will can be stored in redis by creating lists of email, id ,username and surname and a hashes which will contain email, id, username.
创建每列的Redis列表,并将每列的所有数据放入该列表中。并使哈希值存储每个表的列表。例如通过创建电子邮件,ID,用户名和姓氏列表以及包含电子邮件,ID,用户名的哈希,可以将包含列电子邮件,ID,用户名,姓氏的表格存储在redis中。