0

I'm very new to noSQL and the first task is with redis. I have to create a key-value database witih a composite key but as I searched there is no such thing in Redis (or it is called differently). My sql DB would have 4 attributes: Name, Surname, weight, height. Where Name and Surname is a composite key.

I have tried to write simple key-value code in python:

client = redis.Redis(host='localhost', port=6379, db=0)
client.set('Name', 'Tom')
client.set('Surname', 'Spencer')
client.set('Weight(kg)', '65')
client.set('Height(cm)', '178')

print (client.get('Name'))

And I have no idea how to write to make Name ar Surname as a composite key to make it primary.

Also, would be cool if someone could give an example how to get Person_1, Person_2 and ect with all those details (name, surname, weight, height)

I have seen some examples with hash-maps and ect but im not allowed to use it in my program.

Naumedis
  • 1
  • 1

1 Answers1

0

If you do :

client = redis.Redis(host='localhost', port=6379, db=0)
client.set('Name', 'Tom')
client.set('Surname', 'Spencer')
client.set('Weight(kg)', '65')
client.set('Height(cm)', '178')

print (client.get('Name'))

Then your database will be able to hold only one person, if you try to add one, it will overwrite the values stored at 'Name' etc...

You can "compose" keys to have unique key identifiers :

client.set('userid:n', 'Tom')
client.set('userid:s', 'Spencer')
client.set('userid:w', '65')
client.set('userid:h', '178')

To optimise ram consumption try to have shorter keys and values eg "...:s", "...:w" rather than "...:Height(cm)" or at least "...:h:cm" if you store multiple height units.

Of course the best data type is a hash map but it seams you cant use it.

JeanJacquesGourdin
  • 1,496
  • 5
  • 25