简体   繁体   中英

Migrating MySql to NoSql

I'm in the process of migrating a mysql database to noSql for a user management system, and would like to know if/how it is possible to select users that fall into a certain age group..

a) I keep a country called UID which I increment by 1 for each user

b) for each user I do:

hset user: name joe hset user: age 1-99

how would I go about retrieving all the users who are between 20 and 25 for example ?

Is this possible ?

Thanks

You left out which system you were performing your desired retrieval query on, so I'll answer both.

Retrieving users that match your criteria from mySQL is just a matter of having the correct where clause on your SELECT statement.

SELECT * FROM user WHERE age BETWEEN 20 AND 25

In Redis, hset sets a key to a value , and calling subsequent hsets on the same key will replace the existing value with the new one. Since you are performing hsets for each user and age, you are actually rapidly replacing each value in the redis datastore with the next record from your migration query, leaving only the final record to be migrated in redis.

So, if the final record that you migrated from mySQL is "Joe, age 20", redis will have only the following stored from mySQL, everything else will be lost:

Key    Value
Name   Joe
Age    20

In short, your retrieval is not possible, because the data was not modeled correctly; in fact, I'd go so far to say that this choice of NoSQL store is not appropriate for what you are attempting to do.

Redis is only designed to store key-value pairs; migrating to that from a full mySQL table would make very little sense unless there was literally only two columns in the source table. Your use case would make more sense if you were, say, caching frequently accessed datapoints from mySQL or using redis to model a single datapoint that required fast updates. Redis can complement another datastore, it can be used for logging, it can be used for messaging, but it cannot replace an RDBMS, nor does it desire to.

If you truly wish to replace mySQL with a NoSQL store, ask yourself why you are attempting this migration, how it benefits your application, and what risks and drawbacks you are willing to accept. These answers will drive your decision: If you need to process large amounts of data across multiple machines in a tabular format, a Column datastore, like Cassandra will do. If you need to deal with semi-structured data, a Document datastore, like Mongo supports that need. If you need to model relationships, Neo4j is great. If you don't need any of those things, then stop right there and don't migrate at all. NoSQL may be trendy and exciting, but not everything needs a NoSQL solution, and if it does, you need to take extra special care to select the correct datastore based on what you need, and what drawbacks you are willing to accept.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM