So in this session we are going to learn, how we can use python in order to interact with Redis. So I
have created a file, which I am going to load it to Redis first and then we are going to create a python
program and in that python program we'll be passing the Key and we are going to get the Values from
Redis.
So right
Now let's see, what is the keys available in Redis. So if i do Keys *
I do not have anything available in Redis, right now.
Now I have created a student.csv.redis_format File.
So this file contains a Data Set which can be directly uploaded to, or which can be directly inserted
to Redis. So here if you see we have SET Keyword as the first column.
And then second one we have actually the Key Name. So ID is going to be the key name, which is going
to be inserted to Redis and remaining all the values which are, First Name, Last Name, Full Name and address.
I have actually merged everything in to single value by quoting it.
So what Redis is going to do is, Redis is going to set this value as the key and then all this remaining
value is going to be set as Value against key 1
Similarly against Key 3, all this value is going to be set as a Value in Redis.
So now let's load all these value to Redis.
So as we have learnt, we can use Mass Insertion or Redis cli Pipe to perform this bulk insertion.
So the way to do that is, using cat/tmp/ name of the file.
And then you use this Pipe and then you use Redis-cli. You provide the Port Name on which
Redis service is running and then you use --pipe and hit enter.
Now It says it has inserted 5 keys. So basically whatever was there in the file is, treated as one line
item.
So we have 5 Line Items, which is inserted to Redis.
Now let's log into Redis and verify the information.
okay.
So we see, 5 items has been inserted to Redis.
Now if I do Get 1. I'm going to get all the Values which was against key 1.
or you see when I do get 1, I get this thing in return. okay.
So right now we are interacting using Redis cli.
Now let's see how you can make use of Python to interact with this and get the same output.
So I have already created a Python program.
So in this case,
OK we need to Import 1 Library, which is called Redis. OK.
So if you're running Python, you can use either Pip to install Redis or if you're using Conda you can
use Redis, I mean you can install Redis library using Conda
as well. OK.
So once that is done, you can import Redis library and then you need to define a connection to connect
to Redis.
So the way to do that is using Redis. StrictRedis
and then you provide the Host name on which Redis is running. So in our case my python program
and also Redis, both are on the same machine.
So I have just put local host and specify the Port, on which Redis is running.
So in my case Redis is running on 6370. So I provided that Port and then whenever actually
Redis is replying you back, sometimes the information is stored as a byte. okay.
If you just want to Decode that you can put this Decode Response to True.
However, it doesn't require. Here you can check whether you need to put it or not
okay.
So in this program basically i have just created a Function, which is called Get values. in the Get values
we are going to pass a Number, a Key Number, which is going to be let's say either 1 or 2 or 3 and
based on that, what it is going to do is, it is going to call Get function. Ok which is similar to how
we run Get command from Redis-cli.
And then it is going to pass the Key Name. So in return Redis is going to return all the values against
this key and that value we're capturing in value and then we are returning the value. OK if we do not
find anything, we are just exiting and also printing a message, saying there is error in execution and in
the Print Statement, you can see I am calling this function Get Values and I'm passing the First Argument,
which is received while running this program.
So I'm assuming, when we are running this program. We are also going to pass ID.
OK.
So now let's run this program and see this in action.
So the name of the program is load student info.py
So to run this program. I'm going to type, Python space followed by the name of the python program.
And also we need to pass the key number, against which we want to see the value. So let's say if I pass
3. Our expected value should be whatever is stored here.
Okay.
which is stored against key number 3
So let's hit enter and see what is the result we get.
Okay right now I'm getting error that sys is not defined.
So in the beginning I had removed sys, we also need to put sys. Okay because I'm here I'm using sys.
So that's the reason we also need to import this library. So if I run now it should be OK.
All right.
So this time we do not get any error and program is running successfully.
So we have provided Input Value of 3 and it has returned the value which was stored in Redis against
this key.
So if i change the key to 1
So you see, the Return Value has also changed.
okay.
So if I passed 1, this is output which I have received.
OK.
Again this is, this was a very simple program. Just to basically demonstrate you, that there are libraries
available. Which you can make use of in order to connect to Redis using Python program.