WEBVTT

1
00:00:00.180 --> 00:00:01.160
<v ->Let's talk about what I think</v>

2
00:00:01.160 --> 00:00:02.570
is one of the most exciting parts

3
00:00:02.570 --> 00:00:04.650
of the Redis modules, RedisJSON.

4
00:00:04.650 --> 00:00:06.830
This basically turns Redis into a full-featured,

5
00:00:06.830 --> 00:00:10.670
horizontally-scalable NoSQL document store.

6
00:00:10.670 --> 00:00:11.900
That's a big deal, right?

7
00:00:11.900 --> 00:00:14.800
I mean, instead of using some external third-party system

8
00:00:14.800 --> 00:00:15.840
and its own complexity

9
00:00:15.840 --> 00:00:18.540
and having to understand how it works and is configured,

10
00:00:18.540 --> 00:00:21.060
you can do all this within Redis now.

11
00:00:21.060 --> 00:00:22.790
So the nice thing about RedisJSON

12
00:00:22.790 --> 00:00:25.330
is that it allows you to update individual fields

13
00:00:25.330 --> 00:00:27.730
within your structured data atomically, right?

14
00:00:27.730 --> 00:00:29.157
So you might say to yourself,

15
00:00:29.157 --> 00:00:31.060
"Hey, what's the big deal about storing JSON?"

16
00:00:31.060 --> 00:00:33.430
It's just a string at the end of the day, right?

17
00:00:33.430 --> 00:00:34.650
But think about that,

18
00:00:34.650 --> 00:00:36.940
like if you really just wanted to update one field

19
00:00:36.940 --> 00:00:38.350
in a JSON structure,

20
00:00:38.350 --> 00:00:39.360
without RedisJSON,

21
00:00:39.360 --> 00:00:42.300
you'd have to read back that entire JSON structure,

22
00:00:42.300 --> 00:00:43.310
change the piece that you want,

23
00:00:43.310 --> 00:00:44.610
and write the whole thing back.

24
00:00:44.610 --> 00:00:46.990
And parsing that could be time consuming

25
00:00:46.990 --> 00:00:48.470
and resource intensive.

26
00:00:48.470 --> 00:00:50.690
With RedisJSON, you don't have to deal with that.

27
00:00:50.690 --> 00:00:51.523
You can just say,

28
00:00:51.523 --> 00:00:54.270
"Hey, I want to update this piece of my data structure,

29
00:00:54.270 --> 00:00:55.680
go deal with it, RedisJSON,

30
00:00:55.680 --> 00:00:58.500
and do it atomically while you're at it."

31
00:00:58.500 --> 00:00:59.950
Let's look at some examples.

32
00:00:59.950 --> 00:01:03.460
So here's an example JSON data structure, very simple.

33
00:01:03.460 --> 00:01:05.410
Let's say that I'm keeping a little database

34
00:01:05.410 --> 00:01:07.610
of "Star Trek" ships,

35
00:01:07.610 --> 00:01:08.570
and we want to keep track

36
00:01:08.570 --> 00:01:11.750
of what the captains and designations of those ships are.

37
00:01:11.750 --> 00:01:13.540
So for the ship enterprise,

38
00:01:13.540 --> 00:01:18.540
the captain is kirk and its designation is NCC-1701.

39
00:01:18.560 --> 00:01:19.393
How would you do that?

40
00:01:19.393 --> 00:01:22.030
So we could just say, JSON.SET,

41
00:01:22.030 --> 00:01:24.570
these are actual Redis CLI commands here,

42
00:01:24.570 --> 00:01:27.490
enterprise.'"captain" : "kirk"'

43
00:01:27.490 --> 00:01:29.540
So that sets things up with our initial data there

44
00:01:29.540 --> 00:01:32.530
of we're mapping captain and kirk

45
00:01:32.530 --> 00:01:33.850
under the structure enterprise.

46
00:01:33.850 --> 00:01:35.880
So already we have this nested data structure there

47
00:01:35.880 --> 00:01:37.470
with one line of code.

48
00:01:37.470 --> 00:01:39.060
I can get that data back very quickly

49
00:01:39.060 --> 00:01:41.910
using just JSON.GET enterprise.

50
00:01:41.910 --> 00:01:43.900
Again, just captain kirk is what comes back.

51
00:01:43.900 --> 00:01:45.340
That's what you'd expect.

52
00:01:45.340 --> 00:01:47.490
Now let's say I want other add some new data

53
00:01:47.490 --> 00:01:48.510
to that structure.

54
00:01:48.510 --> 00:01:50.040
I can do that with one line as well,

55
00:01:50.040 --> 00:01:53.090
and again, without having to retrieve the whole thing first.

56
00:01:53.090 --> 00:01:57.470
So I could just say JSON.SET enterprise .designation

57
00:01:57.470 --> 00:01:59.300
to add that designation field,

58
00:01:59.300 --> 00:02:01.840
and I'll give it a value of "NCC-1701".

59
00:02:01.840 --> 00:02:03.860
Pretty simple syntax there.

60
00:02:03.860 --> 00:02:05.490
And now I've built up my whole structure.

61
00:02:05.490 --> 00:02:08.170
I can just say JSON.GET enterprise again

62
00:02:08.170 --> 00:02:09.927
and get back the complete structure,

63
00:02:09.927 --> 00:02:13.620
"captain":"kirk","designation":"NCC-1701".

64
00:02:13.620 --> 00:02:14.670
Very cool stuff.

65
00:02:14.670 --> 00:02:16.340
It's just that simple.

66
00:02:16.340 --> 00:02:17.330
Now to actually use that,

67
00:02:17.330 --> 00:02:19.450
you could use the command line interface there,

68
00:02:19.450 --> 00:02:22.150
either directly or through a lower-level API

69
00:02:22.150 --> 00:02:24.120
like Redis-py.

70
00:02:24.120 --> 00:02:26.090
But there's also something I want to introduce now as well

71
00:02:26.090 --> 00:02:27.920
called Redis OM.

72
00:02:27.920 --> 00:02:30.140
And this is a high-level API to Redis

73
00:02:30.140 --> 00:02:33.290
that wraps the complexity of RedisJSON and RediSearch,

74
00:02:33.290 --> 00:02:34.840
so you don't really have to think about

75
00:02:34.840 --> 00:02:36.850
how it works under the hood at a low level.

76
00:02:36.850 --> 00:02:39.290
It stands for Redis Object Mapping.

77
00:02:39.290 --> 00:02:40.776
So it's a high-level API

78
00:02:40.776 --> 00:02:43.670
that just lets you deal with native data structures.

79
00:02:43.670 --> 00:02:45.450
So within Python, for example,

80
00:02:45.450 --> 00:02:47.720
I can just deal with Python classes

81
00:02:47.720 --> 00:02:49.870
and Redis OM will just sort of magically take care

82
00:02:49.870 --> 00:02:51.650
of storing it and querying that data

83
00:02:51.650 --> 00:02:54.350
with very simple commands like save or find

84
00:02:54.350 --> 00:02:55.310
or something like that.

85
00:02:55.310 --> 00:02:57.474
So it's a much easier way to interact

86
00:02:57.474 --> 00:03:00.933
with RedisJSON and RediSearch and even RedisCore.

87
00:03:02.440 --> 00:03:04.330
All the modules and the Redis commands under the hood

88
00:03:04.330 --> 00:03:05.200
are abstracted away.

89
00:03:05.200 --> 00:03:06.730
So you don't have to think about it.

90
00:03:06.730 --> 00:03:08.730
It's just like writing a normal script.

91
00:03:08.730 --> 00:03:11.440
You'll see how simple it is when we go into an example.

92
00:03:11.440 --> 00:03:13.077
It's available for several languages,

93
00:03:13.077 --> 00:03:16.730
.NET, Node.js, Python, and Spring.

94
00:03:16.730 --> 00:03:19.750
Since most of you that are my students are Python people,

95
00:03:19.750 --> 00:03:21.660
we're gonna stick with Python for our example here,

96
00:03:21.660 --> 00:03:25.350
but same concept applies to these other languages as well.

97
00:03:25.350 --> 00:03:26.710
Really easy to use,

98
00:03:26.710 --> 00:03:28.730
for example, here's how you would actually set up

99
00:03:28.730 --> 00:03:30.610
a nested data structure.

100
00:03:30.610 --> 00:03:31.750
This is coming from our activity

101
00:03:31.750 --> 00:03:33.500
that we're about to do, by the way.

102
00:03:33.500 --> 00:03:35.460
All you need to do is say from redis_om,

103
00:03:35.460 --> 00:03:36.690
if we're in Python,

104
00:03:36.690 --> 00:03:37.760
import the stuff we need,

105
00:03:37.760 --> 00:03:42.600
in this case, JsonModel and EmbeddedJsonModel and Field.

106
00:03:42.600 --> 00:03:44.100
So with EmbeddedJsonModel,

107
00:03:44.100 --> 00:03:46.200
we can embed one structure within another

108
00:03:46.200 --> 00:03:48.270
and Redis OM will just figure out

109
00:03:48.270 --> 00:03:51.410
how to make that work in JSON and with RedisJSON.

110
00:03:51.410 --> 00:03:54.308
We are then going to define a Product class here

111
00:03:54.308 --> 00:03:56.560
that contains a StockCode as a string,

112
00:03:56.560 --> 00:03:58.150
as Description as a string, and a UnitPrice,

113
00:03:58.150 --> 00:04:00.500
which is a floating point number in this example.

114
00:04:00.500 --> 00:04:03.160
And since it's deriving from EmbeddedJsonModel,

115
00:04:03.160 --> 00:04:06.460
it will automatically inherit all the Redis OM features

116
00:04:06.460 --> 00:04:08.790
for actually writing that out to Redis

117
00:04:08.790 --> 00:04:10.800
and reading from it or querying it.

118
00:04:10.800 --> 00:04:12.470
Because it's an EmbeddedJsonModel,

119
00:04:12.470 --> 00:04:15.040
I can embed it within another class here.

120
00:04:15.040 --> 00:04:17.260
So I'm setting up an Order class here

121
00:04:17.260 --> 00:04:19.330
that derives from just JsonModel.

122
00:04:19.330 --> 00:04:21.720
It contains an invoice number, which is a string,

123
00:04:21.720 --> 00:04:23.490
a Quantity, which is an integer,

124
00:04:23.490 --> 00:04:25.870
and here I'm saying I'm going to index that

125
00:04:25.870 --> 00:04:27.210
for searching later on.

126
00:04:27.210 --> 00:04:29.480
We'll talk about that more in a little bit.

127
00:04:29.480 --> 00:04:30.550
We're gonna have an InvoiceDate,

128
00:04:30.550 --> 00:04:32.719
that's just the datetime.date, a CustomerID.

129
00:04:32.719 --> 00:04:35.220
And here we're embedding that Product class

130
00:04:35.220 --> 00:04:36.650
and calling it Item.

131
00:04:36.650 --> 00:04:38.290
So that Item field there will expand

132
00:04:38.290 --> 00:04:40.380
into its own Product data structure here as well.

133
00:04:40.380 --> 00:04:41.213
And finally,

134
00:04:41.213 --> 00:04:43.740
we'll end up with a string-based Country code as well.

135
00:04:43.740 --> 00:04:45.790
So that's RedisJSON and Redis OM.

136
00:04:45.790 --> 00:04:48.640
We have enough now to get started and import some data

137
00:04:48.640 --> 00:04:50.610
just using Redis OM in a structured format.

138
00:04:50.610 --> 00:04:52.510
So let's dive in and make that happen.

