2

So I have been trying to migrate our DB from mysql to Redis using Jedis. I understand that on Redis, everything is key and value. But I have few basic questions.

1) Let's say I have a Mammals class which have eyes,ears,nose,hand,legs as members. So now I can have multiple values e.g.

2,2,1,2,2

2,3,1,1,2

1,2,1,6,2

Each row denote the number they have. 2 eyes,2 ears, 1 nose, 2 hand,2 legs.

I wanted to store these data, like we used to do in mySQL, over there, each column will have their values. But using Jedis how do I achieve that?

I tried using auto increment key value and storing all of them using jedis.sadd(jedis.get("counter"),stringg_value)

When I try to print any value using the index, I can very clearly see that, the values are not in the order in which I added. If I add the string value as "one,two, three, four" the outcome will be "four, two, three, one". And this is very random, it's not like all of the values will be in this format, they change too.

My question is how do I store csv formatted data into Redis?

2) how do I fetch data from Jedis based on multiple conditions . like a mammal with 2 ear, 2 eyes and 4 hands. What approcah should I use?

Tague Griffith
  • 3,963
  • 2
  • 20
  • 24
driftking9987
  • 1,673
  • 1
  • 32
  • 63
  • RDBMS and key value stores are different things. You're observing some of the differences first hand. Use the right tool for the the right job. "Migrating" from MySQL to Redis is like converting your car to a boat. – Kayaman Apr 24 '17 at 13:03
  • I do understand what you are stating but then I have a requirement where I have to select-insert-select for some data matching pattern. Over there, SQL is taking too much time, but I believe in that scenario, redis will work like charm. – driftking9987 Apr 24 '17 at 13:15
  • I tried optimizing the SQL side, but most often or not, i get some kind of lock and that ultimately ends up slowing down the system. The whole computation takes around 150ms, but then these many SQL connections end up taking around 250ms to complete. Am still exploring the options. – driftking9987 Apr 24 '17 at 13:24
  • That just sounds like you're not very experienced with SQL either. Changing technologies doesn't really help in that case. A better idea would be to create a new question where you explain your SQL problem. It would be probably solved a lot faster than this one. – Kayaman Apr 24 '17 at 13:25
  • You are right about being inexperienced, will like to get some directions as on how to get rid of the slowness. This is one of the questions which I posted earlier on SO : http://stackoverflow.com/questions/42899224/getting-connection-from-connection-pool-taking-too-much-time-in-java – driftking9987 Apr 24 '17 at 13:28
  • Well that's definitely an issue with the connection pool. It should be taking < 1ms to get a connection from the pool, so you should look at other questions involving DBCP to see how they've configured it. Or move to a better connection pool like HikariCP. – Kayaman Apr 24 '17 at 13:33

1 Answers1

2

One approach to storing the values would be use use the Redis hash type to store each of the rows. With a hash you can have a flexible schema and store each property by name. You will also need a unique key to lookup each row, possibly name. The code would look something like:

 Map<String,String> row = new HashMap<>();

 row.put("eyes", String.valueOf(2));
 row.put("ears", String.valueOf(2));

 ...

 jedis.hmset("wombat", row);

Querying is harder. Redis doesn't provide a general purpose query language like you have in SQL. You need to determine your queries up front and either maintain secondary indexing structure or map the row data to different data structures based on your queries.

Take a look at Redis.io for the data types available. You may find, given your queries, you might make more sense to take an entirely different approach to storing your data.

You may want to restructure your data to create sets of the different animals that have a certain property.

ears_2 = ('wombat', 'dragon')
ears_3 = ('mutant wombat', 'mutant dragon')
legs_4 = ('wombat', 'dragon')

You can then simulate your AND queries using set intersection.

Tague Griffith
  • 3,963
  • 2
  • 20
  • 24
  • But then, if one is using redis, he/she will need storing as well as querying. If redis can only help in storing then how is it helpful? Does it mean that a person will have to use redis and another RDBMS database as well? – driftking9987 Apr 24 '17 at 13:20
  • 1
    If you have a strong need for a general purpose query language and can't enumerate the queries in your application, using Redis to store and query the data is probably not the best solution. An alternative approach that might give you better results, is to cache the results of queries in Redis and work to optimize your MySQL. – Tague Griffith Apr 24 '17 at 13:31