1
00:00:01,730 --> 00:00:09,010
So in this session we are going to learn, how we can use python in order to interact with Redis. So I

2
00:00:09,010 --> 00:00:14,650
have created a file, which I am going to load it to Redis first and then we are going to create a python

3
00:00:14,650 --> 00:00:20,020
program and in that python program we'll be passing the Key and we are going to get the Values from

4
00:00:20,030 --> 00:00:20,440
Redis.

5
00:00:26,300 --> 00:00:26,680
So right

6
00:00:26,690 --> 00:00:35,280
Now let's see, what is the  keys available in Redis. So if i do Keys *

7
00:00:35,280 --> 00:00:37,490
I do not have anything available in Redis, right now.

8
00:00:45,270 --> 00:00:50,030
Now I have created a student.csv.redis_format File.

9
00:00:50,080 --> 00:00:57,670
So this file contains a Data Set which can be directly uploaded to,  or  which can be directly inserted

10
00:00:57,670 --> 00:01:06,770
to Redis.  So here if you see we have SET Keyword as the first column.

11
00:01:07,150 --> 00:01:12,070
And then second one we have actually the Key Name.  So ID is going to be the key name,  which is going

12
00:01:12,070 --> 00:01:18,580
to be inserted to Redis and remaining all the values which are, First Name, Last Name, Full Name and address.

13
00:01:18,820 --> 00:01:26,320
I have actually merged everything in to  single value by quoting it.

14
00:01:26,380 --> 00:01:33,580
So what Redis is going to do is,  Redis is going to set this value as the key and then all this remaining

15
00:01:33,580 --> 00:01:37,970
value is going to be set as Value against key 1

16
00:01:38,320 --> 00:01:47,430
Similarly against Key 3,  all this value is going to be set as a Value in Redis.

17
00:01:47,500 --> 00:01:49,770
So now let's load all these value to Redis.

18
00:01:49,780 --> 00:01:56,710
So as we have learnt,  we can use Mass Insertion or  Redis cli Pipe to perform this bulk insertion.

19
00:01:58,460 --> 00:02:06,660
So the way to do that is,  using cat/tmp/ name of the file.

20
00:02:07,300 --> 00:02:14,770
And then you use this Pipe and then you use Redis-cli.  You provide the  Port Name on which

21
00:02:14,770 --> 00:02:22,600
Redis  service is running and then you use --pipe and hit enter.

22
00:02:22,790 --> 00:02:30,660
Now It says it has inserted 5 keys.  So basically whatever was there in the file is,  treated as one line

23
00:02:30,660 --> 00:02:31,240
item.

24
00:02:31,260 --> 00:02:34,650
So we have 5 Line Items,  which is inserted to Redis.

25
00:02:34,680 --> 00:02:37,710
Now let's log into Redis and verify the information.

26
00:02:43,680 --> 00:02:43,970
okay.

27
00:02:44,000 --> 00:02:48,580
So we see,  5 items has been inserted to Redis.

28
00:02:48,600 --> 00:02:54,050
Now if I do Get 1.  I'm going to get  all the Values which was against key 1.

29
00:02:54,480 --> 00:03:00,620
or you see when I do get 1,  I get this thing in return. okay.

30
00:03:00,640 --> 00:03:04,510
So right now we are interacting using Redis cli.

31
00:03:04,510 --> 00:03:10,250
Now let's see how you can make use of Python to interact with this and get the same output.

32
00:03:10,270 --> 00:03:12,310
So I have already created a Python program.

33
00:03:18,280 --> 00:03:19,300
So in this case,

34
00:03:25,600 --> 00:03:29,640
OK we need to Import 1 Library,  which is called Redis.  OK.

35
00:03:29,680 --> 00:03:36,430
So if you're running Python, you can use either Pip to install Redis or if you're using Conda you can

36
00:03:36,430 --> 00:03:41,620
use Redis, I mean you can install Redis library using Conda

37
00:03:42,520 --> 00:03:42,840
as well. OK.

38
00:03:42,850 --> 00:03:48,820
So once that is done,  you can import Redis library and then you need to define a connection to connect

39
00:03:48,820 --> 00:03:50,820
to Redis.

40
00:03:50,830 --> 00:03:55,060
So the way to do that is using Redis. StrictRedis

41
00:03:59,540 --> 00:04:05,680
and then you provide the Host name on which Redis is running. So in our case my python program

42
00:04:05,780 --> 00:04:07,720
and also Redis,  both are on the same machine.

43
00:04:07,730 --> 00:04:12,780
So I have just put local host and specify the Port, on which Redis is  running.

44
00:04:12,820 --> 00:04:20,730
So in my case Redis is running on 6370.  So I provided that Port and then whenever actually

45
00:04:20,740 --> 00:04:26,050
Redis is replying you back,  sometimes the information is stored as a byte.  okay.

46
00:04:26,070 --> 00:04:30,970
If you just want to Decode that you can put this Decode Response to True.

47
00:04:31,030 --> 00:04:35,100
However,  it doesn't require.  Here you can check whether you need to put it or not

48
00:04:40,600 --> 00:04:40,930
okay.

49
00:04:40,940 --> 00:04:48,900
So in this program basically i have just created a Function,  which is called Get values.  in the Get values

50
00:04:48,920 --> 00:04:55,490
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

51
00:04:55,490 --> 00:05:04,090
based on that,  what it is going to do is,  it is going to call Get function. Ok   which is similar to how

52
00:05:04,090 --> 00:05:06,650
we run Get command  from Redis-cli.

53
00:05:07,420 --> 00:05:12,880
And then it is going to pass the Key Name.  So in return Redis is going to return all the values against

54
00:05:13,120 --> 00:05:20,080
this key and that value we're capturing in value and then we are returning the value.   OK if we do not

55
00:05:20,080 --> 00:05:27,320
find anything,  we are just exiting and also printing a message,  saying there is error in execution and in

56
00:05:27,320 --> 00:05:33,190
the Print Statement,  you can see I am calling this function Get Values and I'm passing the First Argument,

57
00:05:33,220 --> 00:05:35,970
which is received while running this program.

58
00:05:35,980 --> 00:05:40,360
So I'm assuming,  when we are running this program. We are also going to pass ID.

59
00:05:41,470 --> 00:05:41,710
OK.

60
00:05:41,740 --> 00:05:44,100
So now let's run this program and see this in action.

61
00:05:46,950 --> 00:06:06,790
So the name of the program is load student info.py

62
00:06:06,840 --> 00:06:13,100
So to run this program.  I'm going to type, Python space followed by the name of the python program.

63
00:06:13,270 --> 00:06:19,540
And also we need to pass the key number,  against which we want to see the value.  So let's say if I pass

64
00:06:19,550 --> 00:06:24,580
3. Our expected value should be whatever is stored here.

65
00:06:24,910 --> 00:06:25,230
Okay.

66
00:06:25,250 --> 00:06:26,960
which is stored against key number 3

67
00:06:29,960 --> 00:06:32,620
So let's hit enter and see what is the result we get.

68
00:06:33,600 --> 00:06:36,440
Okay right now I'm getting error that sys  is not defined.

69
00:06:36,450 --> 00:06:45,940
So in the beginning I had removed sys,  we also need to put sys. Okay because I'm here I'm using sys.

70
00:06:46,130 --> 00:06:53,610
So that's the reason we also need to import this library.  So if I run now it should be OK.

71
00:06:53,650 --> 00:06:53,960
All right.

72
00:06:53,990 --> 00:06:57,370
So this time we do not get any error and program is running successfully.

73
00:06:57,380 --> 00:07:03,230
So we have provided Input Value of 3 and it has returned the value which was stored in Redis against

74
00:07:03,230 --> 00:07:03,770
this key.

75
00:07:07,170 --> 00:07:10,720
So if i change the key to 1

76
00:07:10,910 --> 00:07:13,280
So you see,  the Return Value has also changed.

77
00:07:16,870 --> 00:07:17,180
okay.

78
00:07:17,220 --> 00:07:20,430
So if I passed 1,  this is output which I have received.

79
00:07:24,270 --> 00:07:24,770
OK.

80
00:07:24,800 --> 00:07:30,740
Again this is,  this was a very simple program.  Just to basically demonstrate you,  that there are libraries

81
00:07:30,740 --> 00:07:36,560
available.  Which you can make use of in order to connect to Redis using Python program.
