0:01 Hey guys, welcome to part two of our 0:04 NodeJS Express Cassandra application 0:07 video. Um, in this well in the last 0:10 video we got everything installed. We 0:12 installed Node, we installed Apache 0:14 Cassandra and also uh we installed Git 0:18 along with the command line interface. 0:21 So what I want to do in this video is I 0:23 want to set up our initial database 0:26 um along with the tables. We're actually 0:28 only going to have one table. But before 0:30 we do that, I just want to kind of go 0:32 into what the uh Cassandra's data 0:35 structure because I realize that some of 0:38 you may not even know what Cassandra is. 0:40 And it's basically a NoSQL database. Um 0:44 but it it it differs from things like 0:46 MongoDB or CouchDB. Uh it's not a 0:49 document database. It's actually a 0:52 column family database. 0:54 um which uses properties from a key 0:57 value store as well. So it's kind of 0:59 unique basically um it does have tables 1:03 or columns you can call them and um very 1:07 similar to a relational database in 1:10 terms of how it looks. Um but it's 1:13 actually very different when you look at 1:14 the back end and what goes on. But 1:17 basically we're going to obviously start 1:19 with a database. All right. And that 1:21 database needs to have at least one 1:24 keyspace. And that's basically 1:27 equivalent to a schema if you're talking 1:30 about relational databases. So we're 1:33 going to name our keyspace people. All 1:36 right. And then inside the keyspace, 1:38 we're going to have a table or tables. 1:43 And these are also 1:45 called 1:47 columns. Okay. So, we are actually only 1:50 going to have one column or table. And 1:53 for for simplicity, I'm going to just 1:55 call it a table from now on. Okay. So, 1:58 we're going to have 1:59 a subscribers 2:01 table. And inside the table, we're going 2:05 to specify the fields that we want. All 2:08 right. And right now what I want to do 2:11 is just have a first 2:15 name, last 2:17 name, and email. But uh actually we need 2:21 an ID as 2:24 well. And first name, last name, email 2:28 will all be text fields. The ID, 2:30 however, is going to be a uh UYU ID 2:35 field. Okay? Okay. So, it's going to be 2:37 a UYU ID, which is basically a um it's a 2:42 long string and it's just a unique um 2:46 it's unique identification. It's a 2:48 unique ID so that we can pull the the 2:51 correct rows that we want. And we're not 2:55 going to manually type these in. We can 2:57 actually use a generator that will 2:59 generate these for 3:01 us. All right, so let's go ahead and get 3:03 started. We're going to use the CQL 3:05 shell that came with the um with the 3:09 data stacks package. If you don't have 3:12 this, please go back to the first video 3:14 and just download and install all this 3:17 software. All right, so we are now in 3:21 the CQL shell and one keyword that I 3:25 want to show you is describe. Okay. So, 3:28 you can describe tables, key spaces, 3:31 um pretty much any object. And I'm just 3:34 going to say describe key 3:37 spaces. Okay. And by default, you'll you 3:40 should see these three key spaces. We 3:42 have system system traces and then the 3:45 the openter program, which is the the 3:48 guey, the browser guey program also has 3:51 its own keyspace. Okay. Now, what we 3:54 want to do is create a keyspace. 3:59 And like I said, we're going to call 4:01 this people. Now, we also need to 4:04 specify 4:05 uh our replication values because uh 4:09 Cassandra is used often for uh just huge 4:12 amounts of data sometimes spanning 4:15 across multiple data centers. So you you 4:18 just need to specify how many nodes you 4:20 want for replication and you also need 4:23 to specify the class. All right. And how 4:25 we do that is say 4:28 width replication 4:32 equals and then in here we need a class 4:36 and basically there's two classes we can 4:38 choose from. One the one that we're 4:41 going to use 4:42 is simple 4:45 strategy. All right. And this is used 4:47 for uh typically if you have a single 4:49 node or you have a and a single data 4:52 center you would use this. The other one 4:54 is called network topology strategy and 4:57 that's what you would use for um if 5:00 you're using multiple data centers and 5:02 you just have a huge amount of data. All 5:04 right. So that's the class. And then the 5:06 next thing we need to 5:08 specify is the replication 5:12 factor. And that that basically means 5:15 how many nodes you want to designate for 5:18 replication. And I'm just going to put 5:21 three although it really doesn't matter 5:22 for 5:23 us. Okay. So uh let's see what happened 5:28 here. Unable to find strategy class. 5:32 What did I do here? 5:36 Um 5:38 h oh you know what this the simple 5:41 strategy 5:43 uh has to that needs a capital S 5:47 here. All right so there we go. So now 5:50 if we go back and say uh describe key 5:52 spaces you can now see that we have the 5:54 people keyspace. Now if we want to use 5:57 it we need to say 5:59 use people. And now you can see that 6:02 this prompt here has changed to people. 6:06 All right. 6:07 Now CQL is extremely similar to and in 6:11 most cases um identical to SQL. All 6:15 right. So you can we can say 6:18 select all from and then a table. We can 6:22 do that. Okay. So let's go ahead and 6:25 create a table. 6:27 And again, just like SQL, we're going to 6:29 say create table. And I'm going to call 6:33 this 6:35 subscribers. And we need to specify the 6:38 fields that we want. All right. So, the 6:40 first one is going to be an ID and it's 6:44 going to have the uh it's going to be 6:46 the UU ID 6:48 type. All right. And then the next one 6:51 will be let's do email and that'll be 6:55 text. And then we'll do first name, 6:59 which will be 7:01 text, last name, also 7:05 text, and we'll just leave it at that. 7:09 All right. So now, 7:12 um, we need to specify our primary keys. 7:15 So we're going to say primary 7:18 key and ID. Okay, we're going to put in 7:21 ID. Now, the way that this works is is 7:24 quite different than a relational 7:26 database. Um, if you want to be able to 7:29 sort by a certain field or let's say use 7:33 it in an update or anything like that, 7:37 um, then it needs to be defined as a 7:40 primary key. So, you can have more than 7:42 one primary key. In this case, I want 7:44 I'm going to use email as well. So, I'm 7:46 going to put that in there. Okay. So, 7:49 we'll run that. And now we should have 7:52 that table. We say 7:56 describe 8:00 tables. Okay. So now you can see we have 8:02 our subscribers table. So let's go ahead 8:05 and insert some data. So we'll say 8:08 insert 8:11 into subscribers. And then we need our 8:14 fields. So, we'll have uh let's 8:20 see, we 8:22 need ID and then we'll have 8:25 email and 8:27 then first 8:30 name and last name. Okay. And then we're 8:33 going to say values. So, you can see 8:36 pretty much the same thing as SQL values 8:40 for the ID. Now, this is going to be a 8:42 UU ID. So, it's going to be this long 8:44 ugly looking string of characters. And 8:48 we don't want to do this manually. So, 8:50 we there's a few functions that we can 8:52 use. Uh, one of them is the now 8:54 function. So, we're going to say now and 8:56 then some parenthesis. And that's going 8:58 to generate a type one UU ID for us. All 9:02 right. Next, we have the email. So, 9:04 we're just going to have to think of an 9:06 email. just say user one 9:11 atgmail.com and next is the first name 9:15 we'll say 9:16 John. Okay, last name we'll say do. 9:20 Okay, so that should work. All right, so 9:22 if we get no errors back, that's usually 9:24 a good thing. But now we can say select 9:28 all 9:30 from 9:32 subscribers. And there we go. phone. You 9:34 can see the ID is this is a UYU ID and 9:37 it was generated by that now function. 9:40 And then you can see we have our email, 9:42 first name and last name. All 9:45 right. Now I want to go ahead and insert 9:48 some more 9:49 um subscribers here. And I want to show 9:53 you how you can do a batch. Okay. So we 9:55 can do more than one query at one time. 9:58 So we can say 10:00 begin batch and we don't want a 10:02 semicolon. Just click enter because 10:04 that'll bring us to a new line. And 10:07 we'll say 10:12 insert into 10:16 subscribers and we need 10:23 ID first 10:25 name last name and then we want our 10:29 values. Okay, values will be um again 10:33 we're going to use the now function and 10:35 then we'll say for email user 2 at we'll 10:40 say 10:42 yahoo.com. Make sure that you use quotes 10:44 if you're using 10:46 strings. Um let's see what else do we 10:48 got. First name uh we'll say 10:52 Bob and then last name we'll say 10:57 Johnson. Okay, so that's one. Now, 11:00 without a semicolon, we're going to 11:01 click enter again and we can do our next 11:04 one. So, insert 11:08 into 11:12 subscribers. I apologize. I'm not the 11:14 fastest typer in the 11:17 world. Uh, let's see. ID, email, first 11:25 name, last name, and I forgot a quote 11:34 here. Uh, actually, what am I doing? We 11:37 don't need quotes here. We need quotes 11:38 for the values, not 11:41 these. All right. So, we're going to 11:43 have ID, email, first name, last 11:46 name. Uh, let's see what else. uh 11:52 values. So for values, we're going to 11:55 use the now for the ID. We're going to 11:58 use let's say user 3 12:03 at 12:04 [Music] 12:06 yahoo.com. We'll say William 12:11 uh smith. All right. And that's good. So 12:14 without the semicolon, I'm going to 12:16 click enter again. And then what we want 12:18 to do is apply batch 12:22 semicolon. All right. So that should 12:24 should have ran. So now if we go in and 12:26 we 12:26 select all from subscribers. Now we have 12:30 our three users. All right. Now we can 12:35 um we can add a wear clause. So let's 12:38 try to do select 12:40 uh let's say 12:42 select first 12:45 name last name 12:49 from 12:51 subscribers where email is equal 12:56 to user 12:58 2 at what do we have 13:01 yahoo yahoo.com 13:08 Uh let's see involve data filtering blah 13:10 blah blah. Okay. So what we need to do 13:13 here is just tack on uh allow filtering 13:16 at the end 13:21 here. Okay. And now it gives us the 13:24 first name and last name of the person 13:26 with the email that we chose. Now, one 13:29 thing that I should mention is that I 13:34 don't I believe that if you want to add 13:36 a wear clause, I think that this field 13:38 has to be a key, a primary key. Um, but 13:42 I'm not positive. So, let me just try 13:44 something. 13:46 Um actually I think it's only inserts 13:48 that that's true for but let's try 13:51 to we'll say 13:55 where first name is equal 14:01 to 14:06 Bob. Okay. So no index column present in 14:10 by columns clause with equal operator. 14:12 All right. So basically um you really 14:15 have to plan out your database and your 14:17 schema uh according to the queries that 14:20 you're you you're going to want to run. 14:22 And I think it's a it's kind of a 14:24 fallback um for Cassandra because it 14:28 does add in some extra work and some 14:31 frustration if you for instance didn't 14:35 make a field a primary key that you 14:37 should have. Um but it makes up for that 14:40 in other ways. um extremely scalable uh 14:44 really fast, can handle huge amounts of 14:47 data, things like 14:48 that. So, we have a couple uh 14:51 subscribers in our database. Our 14:54 keyspace has been created. So, uh I 14:57 think I'm going to stop here because the 14:58 video is getting kind of long. Um and in 15:02 the next video, what we'll do is 15:06 uh we'll get node set up. We'll install 15:09 uh Express which is going to be the the 15:11 framework that we'll be using. It'll 15:14 generate all our fi initial files and 15:16 folders for us. Um so yeah, we'll be 15:19 doing that in the next video.