Build Microservices in Apache Cassandra™ , with Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so now about bootstrapping okay i told you that today is not about cassandra it's really about coding okay so those people who told me yeah it's too eye level about cassandra i don't really do and zone today is the end zone okay we will build a real app okay so you need a git okay git is optional just you can just download all the source using download as a zip from github java so gdk11 is uh recommended if you are still on gdk8 in 2byte to to buy it would work uh still you need to edit the pom because you know latest version of the maven compiler plugin added some new attributes for gdk11 guitar release released okay need to edit department drop that maven and your java id and of course again we do have a git board for you if you never use each other all right so again um we do have two streams once on youtube and once twitch as kind of a backup we are uh less we we still looking at the twitch chat but not as many people as the other streams um discord for uh the questions gitbuddin ids and as usual everybody everything is on github slide notebook and all the source code but that makes sense right okay so let's move forward so um as again as usual we are showing a lot of stuff moving from one tab to another so if you have multiple screens that will help if not i would recommend this kind of settings on the screen having me somewhere with the stream explaining stuff i will do the exercise in front of you a question if you want on discord your id and github because everything is on github and you will do some some copy paste from github but not a lot today is kodi not copy pasting okay so this link is very very important maybe i should make a pause like 30 seconds here just to be sure that you do have the time to get the link and go to the github repo and clone the repo on your local machine or access using a browser okay so github.com data stacks dash academy you'll find all the courses all the workshops there machine learning kubernetes core cassandra uh what do we do um testing with no sequel no sql bench oss fallout better bots yeah i we do have a lot of workshops now ready to go but this one okay david just post the link i think we're good oh maybe i'm in front of the line okay you got it microservice.java workshop online okay so ends on with striping so let me go to the like you to github okay so here i am that has tax academy micro service workshop online all right all right as you can see we did a little trick to connect this code to github so if you are not on our more than 3000 happy false on discord you can just join us here and today we will create the to-do app wow nice right so uh if you are in the front the front-end part of the developers or full stack you know each time you try to develop your first web web ui uh to-do app is kind of the to do to do samples yeah like for you know for us back-end developers uh it's you know the patch the pet store for for spring yeah to do is the same for the for the web ui for the sample you find everywhere and we will do that but we won't do the web ui we will do the backend using java okay so table of content so everything is on the single with me you don't have to you know to click on multiple files today everything is there and there is a nice menu as you can see and if you complete one of the section at the end of the section there would be some kind of link to go back to the table of content if you want to but today you simply have to scroll all the content from the content tables so boot strapping okay so i told you two options i'm using git pod or i'm using my local environment i think i will use my local environment just to ensure best latencies but and also avoid some copy paste and stuff like that locally i'm most confident but again you can simply even if you don't use keyboard today just give it a try click on open in github that's just amazing you do have a full working eclipse and that will create that will do a maven cleaning style in the beginning and this is what you should like but okay by default auto save is not enabled in github so if you change the file and you know run the command and nothing happen yeah don't be surprised you may have forget to to save control plus s is the way to go uh it happened to us many many times this week so you ma you may find this in the same trap oh okay so to do you will see that so it's not doing maven clean and set exactly he's doing maven clean and style dash d maven dot skip that test that's typical true why because we are developing with tdd approach because you are not you have not coded anything yet tests are red okay your job today is to make test green all right so option b working with your local environment okay all right so you clone this repo and you access the repo so i also provided some how to install java okay that's good to have everything in a single place how to install maven and this is what you should and how to install your ade okay but i will consider that you do have your own i will do yeah i will consider that you do have your laptop more or less ready to do some uh java code and if not you have a little time to make that happen because the first is run the to-do app okay so um give me a okay when you think you have imported your project in eclipse and you are ready yeah because we will start with the to-do application okay so full disclosure the slides in the in the github repo are not the latest version yet okay i will commit that after that it's always glad that we are finishing everything you know a couple of hours before the workshop all right so let's see how is this code okay and youtube okay we do have 100 viewers okay perfect and you are probably downloading okay okay solid you are downloading okay i do have some feedback telling me that github gitpod i has had enough time to start up and also some other faults using laptop is also ready okay don't browse the code yet first we can play a little bit so i told you about the to-do app okay so let's look following the readme step by step what we will do and if you are you know confident about java and spring and maven i saw some hardcore i expect that the readme is self is autonomous enough you know it's there is enough info there for you to do the full exercise on your own just reading what what you need to do okay so um all right so to do mvc okay to mvc.com um is the go-to app when you are doing a web ui and there are multiple implementation i can pick anything i like let's say view gs okay and here i can start playing with the app so i can create some tasks okay i can mark task as complete i can delete a task to do okay i can hide the completed task i can clear the completed task so up i it was only completed and or either only active or whole okay there is a filter here i can list everything select everything and remove everything okay so this is implementation it's all javascript okay this is not what we want to do today we want to create a backend for it okay so second part is to do dash backend so there is something called to do dash backend.com that noise okay to do backend.com okay so this is the rest api needed for having a working to-do app exactly the way i show you okay and as you can see there are tons and tons and tons of implementation available let's add our own will you okay let me go back to slides okay to do back end here you do have the link i don't know why the link disappeared in the readme but never mind okay you are probably already on to bycam.com playing trying just to figure out what it is okay so in todobakken.com you do have a client ready you know client ready for you and there is a spec runner so you can provide an endpoint and there is you know unit test automatic test uh suite that will be executed against your rest api validating that your behavior is okay and once you did the unit test of course you can use the client okay so everything that we are doing which is the rest api today you don't have to worry about the web ui or the specific integration test it's all provided in tulubaken.com pretty nice isn't it all right okay so let's go to exercise and maybe i should stay in the readme just follow the different steps and you know what i will just move many meter to the other side okay all right um tons of people have already implemented the api so you can pick any of these endpoints and try it and as you can see i do have blah blah blah ready to go so we are about 100 watching and doing the exercise so we are you know 30 per endpoint working on that so those up are hosted in hiroku and you know hiroku may could make could take 30 seconds to start the app it's on demand but i'm pretty sure that now if i eat any end point yeah it's there because everybody every single of you have try it okay so what i want you to do is run the spec runner you know like that this is a spec runner and take one of the end points so i will pick the second one okay i copy and i paste and i run the test so i am the i'm executing the test suite against an endpoint available on internet just for you to realize that we need our rest api to be green like that okay and here is the url this is what it should look like okay we could also use now the client okay i could use the same endpoint so each time i put the endpoint here and guess what it will simply put the end point in the end super super sump simple way so let's all create some nice task yeah it's working baby alex all right i can remove okay so i think you got it both the web ui and the run spec are there for you but what about the the back end now okay so let's see expected output start the back end now this is your time okay so you go to the let me okay i will put my okay clear let's make that bigger okay [Music] bigger okay so where i am i don't know why keep having this old second folder but anyway so you do have the two then to do in cassandra so let's go in there oh by the way you can first check that this is a meaty maven project uh so the first is to do back end cassandra but you know there will be follow-up in this workshop and you might tell that we do have the same for multiple languages and we also have the same for reactive so today's you know basics cred let's move it's already um something it won't be that simple don't worry okay so i'm going into tulu backend and the first thing to do is uh start the app so um even oh i can build so maven now let's see clean spring boot run okay you have the command somewhere on the readme and it will you know compile everything and start okay okay and you do have these uh logs telling you that it's now available on locals 8080 so guess what let's go to locals 8080. and look at that we do have uh we do have rest api already there okay already providing some stuff okay so as you can see if i so let's see what what about the endpoints okay you can list all the tasks add a new delete a new or for dedicated tasks you can get the details delete or update okay um and you know wow that's nice it's swagger so maybe i think most of you who already use swagger used swagger 2 providing by spring fox so maybe you also know that the swagger has been given to the community and now there is something called open api like swagger three ish and so i did not use the spring fox to generate this documentation from the api i've used spring dock so i would recommend to check out because it's really well done okay i'll let you get use the latest version of spring boot and have the latest version of the specification to get the dock okay all right okay so same with uh git pod if you run the same command it's not localized but something like 8080 dash and the dynamic url of your workspace but we have you know tall gitpod to expose the 8080 port so yeah it should be fine you should be in the same things as me i'm using localhost and everything is on http if you are using gitpod everything on https and so the created url should be there and you know what i'm using swagger but what about so seems like to get the list i'm eating localhost 8080 api v1 to do slash you can do the same okay and of course you will eat and get the results okay and it's kind of a-tos-ish meaning that it provide the url of sub resources so if i click there i get to the detail of a task and i can go back working both for git pod and local no problem about that and you can play a little bit with these nice clients okay and if you are doing race dpi i would recommend you to just implement this spring dock layer it's so easy now to it's also easy now to to to get this ui generated for you all right um okay you know what i will try to find this guy okay and i will do the same but now i will open now i will go in my id and i will start the app you know the way you shall start the app in an ide like that okay so uh nothing changed no no worry about that i'm just now in my ide and i feel more comfortable in my id all right okay so you might tell but i don't we don't do anything and yet i do have a back-end api and it seems to answer and uh you guess what we will now execute the test runner again this endpoint and see if the api is okay and we will also do the web ui and see if this endpoint is okay so let me go to [Music] let's see local api local api documentation local api spec runner okay so as you can see my endpoint is localhost 80 blah blah blah i'm working locally my first reaction when i saw that oh how does this guy is able to interact with something on my laptop you know back-end reaction it's javascript so it's executed on your laptop on client side and this is why localhost resolve and it works so you don't have the api and it's ready to go so what the hell okay i could also do the same with the client and the client is there and i can do some stuff and it's working as well okay so guess what on your laptop now you do have a spring boot app with spring dock for the documentation layer you'd have spring web to expose endpoints you can browse the code uh it's not it's quite clean i would say you know if you find dirty i would accept any pull request of course happy to do that and we do have an interface called to-do list repository and no mystery what i did is i provided an implementation of to-do list repository to work in memory in memory in run okay and if i go to my id okay let's close all the stuff it's too much and i go to repository you can see that at on the top i do have to do list repository okay this is my interface find by id upstair delete find all delete all okay easy and i do have in memory implementation and guess what this is simply a hash map key value key value and i work with the hash map okay fair enough and this is why you get task one one two three here because i explicitly created those tasks and i also explicitly defined a uid why because in the documentation now you can simply try out and this id is working okay try to make things simple for you okay so now what now we want uh okay run the to-do app so i think i'm moving but okay okay okay let's go back to gita i'm simply following those steps you know not reading but i'm doing exactly what it what it's listing there i can tell because i write document all right okay nevermind okay so we started the backend api this is what it looked like we are happy we navigated through uh through the web ui like that and i was able to test the backend and now if i'm online light up i do the spec run and if i'm github i do the spectrum same and i can use the ui and working both local and at git pod and here you do have you know task to do uh don't hesitate to subscribe to the channel i don't know if you noticed but we are 2.75k it would be not difficult to reach the 3k come on if you like the content please subscribe it help us or give a thumbs up in the video or even in the chat okay well done so it was all about part number two starting the back backend wherever you like and test with to do backend.com spec runner and client that the api is ready to go but it's working in memory let's do better we want that api to connect to cassandra okay all right yeah so we will change the to-do list repository to connect to cassandra but there is a little issue here we don't have cassandra yet okay and yeah we got a few cover of course with data stacks astra castle has a service in the cloud again don't have to install anything if you run one of our previous workshop you probably already have an instance if you don't we will create an instance all together if you have a existing instance you can reuse it of course easy so what is astra astra is a service offered in the cloud okay software as a service providing cassandra and a datastax studio it's a notebook oriented tool and sql console the secure and also some dashboarding around cassandra just for you as a developer only to interact with cassandra and don't have to be concerned about monitoring and all the administration operation okay in there you can do sql sql console there is a studio we will go into the studio in a minute and we can use the drivers and today we use java drivers full disclosure next week same time we are using python and node.js drivers also to create api on top of uh customer but it's not the same use case okay we like variety just for you don't to be too bored you know doing the same app again each week it will be something about events streaming stuff like that okay okay so now i would like you to create the astra instance okay so how to do how to do it okay read the readme okay well then create the astra instance so we need to go to astrada.com or you know as strata.com register if you don't have an account so let's go oh but i do have when i can remove the register okay so you can sign in using uh you know email of course but also github account or google account i feel it pretty neat you know pretty easy to go and you sign in nice okay so next i do have already my database created you can change it i could terminate it and recreate it but yeah it's already there so what i if you don't have any database in astra yet this is the screen you do have you will provide some parameters please use free tier okay it's it's a real free tier free forever you create a database it will stick there it's a small size database single node but hey you can use it anytime anywhere as i told you if you already created an instance named killer video totally fine we will reuse that if you don't you can use def workshop database as the database name which is pretty relevant because each time we go back here to not having to install cassandra we can also provide docker image and docker compose if you like but then you need a laptop bit more powerful to run the three nodes and yeah it's easier for a hands-on to have everybody using software as a service in the cloud okay select the region so select the region the closest to you to reduce latencies i'm based in paris and so i'm using the europe west but a folks based in the us the time zone fit more the usa i think so pick one of the us you know us east and west west i don't know which one are available today yeah so java version is dependency as in palm not resolving released only if you are using only if you are using gdk uh eight yes you're right yeah because uh until before gdk9 you were only one attribute it was target i think target and you can say target 8 1.8 and one we switch to 11 there is now target release and it's not 1.11 but it's 11. all right okay so keep doing that fill the key space name to do app but again you can use whatever you like to do user and to do password please remember this username and password you can pick whatever you like but you will need it in the next exercise so you know copy paste is not that bad for this one okay and when you have filled everything this launch database button is blue and you can go and click launch database and you get a pop-up launching your database and next you will see this screen for a couple of minutes uh depending on how many people trying to create the instance at the same time it can take to two three up to ten minutes but i don't think so um and when it's done this is what it looks like you don't have the green uh panel anymore it's just telling you that your astra instance is ready to go okay all right i will maybe make a pause to have everybody starting creating the database but for folks who already have created the database you can go on with the exercise i think it's detailed enough for you to succeed the more java you can do the better i guess okay go all right all right all right all right so now it's about opening data stack studio okay so i'm going to astra i'm going to my instance okay and i will open data stack studio developer studio okay so locate developer studio launch now i did a nice screenshot for you and you will launch now the first time you are launching the studio it will ask you login password uh yes it's another service running in the same platform so you need to provide again to do user to do password if you use the parameter defined in the readme but simply reuse the one you entered previously and so let me move my knit little okay so this is my github this is my github materials i have downloaded i have clone okay in z materials i do have okay tons of image this is how many screenshots i did for you today okay okay but in the notebooks you do have something you can simply drag and drop okay and let's do it it will create a new one okay no big deal all right so now i have to and it will open the notebook like that okay but you know for now there is maybe nothing to to show okay if you describe key space to do oh you know what i will just to be sure i will you don't have to i will drop table it's live i will drop table if exist and i will drop my to-do app you know key spaces uh what is the name again to do underscore task took okay and now if i describe the key space yeah nothing in there okay i've created a astra instance cassandra single node okay and i do the key space name to-do app and nothing in there i can simply say that i'm i do have a single node and replication factor is one okay don't execute the inserting two because guess what the table is not there we need to define which table we need and we also need to connect our java to this guy okay so what is said in the readme okay i have imported okay and now i do have stuff and i you know execute a command in front of you just to give you time to do so okay now connectivity to astra okay now is the big time we will connect the back-end api you are running to this astra guy and to do so you need a zip file okay so to download the d-file you will go to download secure connection bundle so here it is in the middle of your screen so refresh the page before just to be sure because as it's written when you download that stuff you know the link to download is available for five minutes so let's say you were doing stuff in data stack studio go back and click download yeah you won't have the file so you know be sure and now you click on download and you get the zip and i will put that in downloads and as you can see the name is secure dash connect dash the name of the instance of the database okay so this is my so i think it's a readme i i put something at dev dash workshop dash db.zip and this is the reason why it's already like that but you know i can play and re re save my new file now i do a file to file in my downloads side by side oh how cute okay go back here now this file you just downloaded locally you need to put it in git pod okay because we was not able of course to have this zip available for you you needed to create the instance first so to do so uh you go to file upload file menu but this menu will appear only if you select one folder so i would recommend you to select the root folder micro service does java does workshop online here and then do file upload file and uh you will get this uh file available just at the bottom of the of the of the files you know files three and if you do ls in the terminal you will see that the file is available for you in slash workspace microservice java workshop online name of the repo okay and name of the zip okay same if you downloaded this file somewhere click make sure to have the path you know pwd or you know anything working with windows if there is a command to know which part we are in windows just to have the full path for your file okay and if you are using windows maybe you have to use double slash because it's empty i didn't try that but you know be careful in the path it needs to be a valid path and it's it's windows it's anti-slash and in java anti-slash are escaped so you would have probably need to double them to slash i didn't try myself but no i feel that that would be required okay so now my zip is on my local folder and i can try uh to edit the first g unit which is connectivity to astra okay so it's it's written you know friendly for people running on on [Music] git pod because i don't i didn't find any right click run as junit uh but you know until it g eclipse of course easy to do so i will use mostly clips but if you are using git pod you can just make them test and do the same okay so let's do fourth c fix the g unit connectivity to astra explicit eclipse okay i go to src test java okay and it's the first file man the guy who did the workshop did a good job they are the proper order the guy was kind of imaginative in the name i guess all right uh so you need to provide the path of the zip okay so i guess this one is okay okay no big deal it's the correct path file exists okay username to-do user password to-do password key space to-do app and this is where you can put killer video key space user kv user and password kv password all right okay so should connect to astra okay so first we are testing that the file you provided exists now that's a nice things to do and then to initiate the connection to casserole we are using an object called cql session okay and to connect to local cassandra we would use the ip of one other node called the contact point but here we are working with cloud and we have we simply have to define the path of the secure connect bundle and username password and the key space at night i've provided everything like constant just before and if everything's okay i should have connection established to astra with key space okay let me shut down my running back end i don't need that right now and i will run as unit test okay green bar not bad seems like the unit is working all right um so i'm connected and i do have this nice log message connect establish to astra with key space to app so if you are hardcore java regular spring boot user and everything pay attention i am using g unit 5 there is some tricks uh in the pump to make that work you need to provide uh you know to you need to exclude the you you need vintage because it's not g4 but you need five so there is a working sample this is simply the break the comment i would like to do about that you know nothing related to cassandra but that's a nice unit to execute but it's not a spring boot unit because i don't need to run the full app to execute my integration test it's still an integration test because i'm connected to astra but it's not it's not a spring boot test but let's see a bit later we will do spring boot stuff okay so i did my first exercise okay and now i did 4c and i can go to 4d and execute the same now connectivity to astra with conf test okay so i will close this guy go to the next one okay and now instead of providing is explicitly each parameter when i initialize the session i will use a config loader something called driver config loader and i simply said please use application underscore test.conf so if you go to ssc test resource you should have the same file as me because i just committed it before the workshop okay so sorry about that there is my name in the path you need to change that to match the path of your secure cloud bundle file and also uh username password and key space okay those are the three the four parameters to change and the idea is here instead of injecting everything as constant there is a dedicated file called file and now the only thing you have to change is the conf file okay if tomorrow you want to move from cloud to local you go to this file you only change this file it will still working okay so secure connect okay it's a proper url to do user to do password to do app i think i'm good so now uh well i would get a file not find exception with here if the file was not existing so i didn't use any assertion anymore also it's not assert is assertion dot tell me if you get it do you need five i change life as well all right okay and it's also you can also have a display name anyway it's not do you need 5 stuff but nice to work a bit with that and now i can execute this guy run as unit and cross finger i should be connected to astra again okay sims is happy with that okay so as you can see first you need to connect providing all the settings second is do you need to connect but using a configuration file okay and now until starting now we will use the configuration file okay i keep sliding in this in the wrong order i don't know which one is sliding left or right for tinder but you know i'm doing the wrong one all right uh so now okay we did 4d okay and well done we are very happy with uh like connectivity to somewhere because now indeed our app can connect to cassandra we still have to change the ripple because it's still in memory but at least we do have the conf file and some code working connected to castleman okay good point so let's back a little bit to slide okay sorry it's about an hour let's go back to a bit of slide mode not a lot okay but still okay so to connect to cassandra datastax provide a set of drivers available of a lot of languages today we are working java one the java one has something special it used to be two set of drivers drivers for other open source customers drivers for dscs of data stacks enterprise enterprise distribution of cassandra with extra features like spark embedded like search and also the graph graph database embedded so now there is only a single set of drivers two data stacks java drivers providing tons of features like connectivity um of course connectivity for a driver oof but also you know there is retry policy reconnection policy load balancing so the driver is very clever you know your data is probably replicated on multiple nodes this is how somehow works and the drivers is token aware that at the at the driver level we know which data is on which node and that mean the driver can do load balancing among the node holding the the data you need and if one node goes down the drivers detect it because it's doing else check under the hood and won't uh invoke this node anymore okay very clever a lot of not balancing okay it's doing the query yeah i can use the driver to execute queries against my database wow what a surprise so we can create some tables exactly what we will do in the next exercise you can do synchronous asynchronous and reactive calls so i have the reactive part and you see the folder active uh just before but you know what you know i i take the time and in two hours there is no time to do so so there is a query builder to help you building the queries there is paging and also of course you can use spring and you can map from objects to tables you know object mapper are there to you know ease the use of java peugeot with tables okay it's not a rational mapping because there is nothing relational in cassandra but it's still object mapping why i told you that because most of us know ofm acronym the object relational mapping there is no heart because there is no relation no joining and if you want to know why please attend one of the previous workshop all about data modeling okay so how to connect i told you when we seen the code that you would provide one contain point only one is necessary two are better because if the first call failed the driver will load balance to the second one just to initiate the connection but you know what if the first call is successful then now at the driver level we know all the topology of the cluster and then we can start load balancing among all the nodes and you know even if the content pause is down after a few minutes it's not a big deal for the at the client side because this guy has everything he needs to do the failover for you okay that's cassandra on premise close to you but hey we are using a cloud service okay so how does it works well you know what it's work the same there is simply a trick you are connected to a cloud service so it's it needs to be http okay and it needs to be really secure so it's a two ways encrypted connection using strong authentication your certificate and this is why you're downloading a zip inside the zip there is the certificate required to open the two-way tls connection between you and astra and then there is a load balancer on the astra side and some proxies to route your request to the proper node and using a protocol name sni for service name in indication uh the client can say i want to send my data to this node sni and then the load balancer knows what's happening and route the instruction to the proper node it could be read requests write requests or read requests write requests or anything like else check and look at that we are opening a two-way tls connection and this is the reason why when you execute the unit test to connect to astra it took a few seconds not milliseconds but seconds on my laptop it's about two seconds and the reason is yeah you need to really you know inch and check ssl in two ways validate the the certificate and everything so yeah took a little bit to open the connection but once the connection is open you know keep alive it's uh the pipe is there you can start sending the request very very fast all right open a session we did that in the exercise before so i can go fast but you can either use explicit parameter or delegate everything in a file we did that and what is this file application.conf it's a default name as you can see using a driver loader config you can use any file you like we are i think it's what is now we do have a spring boot starter so these keys can also be put in application.gml or very soon it will be released very soon instead of that conf keep in everything in application yaml like we love to do uh okay yeah nothing more to say about it uh i had a couple of slides about load balancing and everything but as we don't change that today let's move forward important to know about sql session this sequel station is a stateful object i told you the driver is clever and keep open tcp connection between you and any single node that's a lot of tcp connection and so if you're working uh on castle of course if you're working with castra there is a tls um but then it's doing else check and stack aztec okay uh so when you shut down your app please do sequestration.close because you don't want to keep the tcp socket open you want to be clean and so you need to close the secure station okay how to close it in the java world you can so you are using what we call a shutdown hook and i know at least two ways to do it you can go to my classes dot get runtime dot get shandang ook and then you implement a lambda or you can use the spring pretty pre-destroy annotation and on the cleanup you will simply close the session okay i think this is what we are doing with the unit test you need test with spring but you need tests maybe i i use a try closable i will show you that but you know starting from java 7 hardcore knows that try try close so the the sql station is autoclausable all right let's move forward connected yeah i did the exercise before the slide maybe it's it's it's better no it's you know seeing what's happening and then get the theory okay how rest requests are under in multi-node cluster in customer uh so it's using sni for sure so it will contact the load balancer with multiple nodes there is an alias of course it's not a spuf and then using sni tips it be routed to the proper cassandra node yeah if you want more deep dive like i think there is a driver guys hidden somewhere either on youtube chat on or on discord and i can ask those guys okay okay now let's do the real fun stuff and do credit repositories and you know what we are moving fast or maybe i am moving fast okay uh so we want to create the repository okay create read update and install guess what we need a table to do that and as far as it is now there is no table in the key space okay i did describe tables nothing there so how to define the proper data model with casa let's do it with i don't do the theory today it's practice practice practice so i let's apply the methodology to our sample in cassandra you start with the app what you need to do and then you will work on the model and finally the data in the relational world it's not the same you start with data entities and relationships and then try to go to the normal form like third normal form and you ask the app you know find your your request use the nc sql do some joins whatever you like my data is there i don't touch it cassandra is not working the same at all if you try to think like relational world in cassandra you fail okay in cassandra there is no joints and so if you need many too many relationships between entities you will have to duplicate the data okay i won't go too much in the theory i do we did previous workshops explaining that into the details let's focus to our use case so my app yeah so of course my app is to-do app okay so let's apply the methodology there is a process described on academy.com courses ds220 it's a free courses uh mooc free courses this is the courses you need to take to pass the casanova certification because yeah sorry with only the on hands-on workshop it's not enough but you know stay with us until the end i do have a nice surprise for you in the last slide yeah seriously all right and if you're watching the rest the the live after the stream you can go there and go back i'm waiting for you no just kidding okay to run the process you need first entities in relationship this one is unchanged uh from relational world but you also need your queries because you will create one table per queries basically okay if it's not 100 cred you will need one query per table all right okay then pause your brain it's all about applying rules and rules and rules to go to the physical data model so let's apply that for today entities okay uh so if i look at my ui i think that i need an entity called task okay with a title yeah completed flag true false and holder in some javascript web ui you can move and change the order of the of the task it's not the case in the ui provided by to-do back-end but you know i told you we need an order because it's part of the spec of the rest api okay so we need an order okay that's my entity what about my queries now okay first i need to list all the tasks now when i show up get all the tasks find all okay then i need to create a new task providing the title and the status you know true false and the ui seems like by default it's false you know if you created a to-do it's not done yet it's still to do so yeah create a new task and you can go back to the web client and test it if you double click on an existing task you can edit so you can edit the title you can either read the title either the status true false so of course we need some update we can delete you know it's not only mark as complete you can simply go to the right part of the of the task and delete a task and not part of the ui but again i told you because this is the specification you need to delete all there is a rest resources to delete all okay we do other queries we do other entities what the tables or tables look like okay title completed offset it's not enough to ensure unicity you know a primary key needs to ensure units the full primary key needs to ensure unicity of the record okay so i've created a uid uh and it's a unique identifier and there is no real advantage to use a partition i mean if i would have a family or a tag for the to-do maybe i could have to do tasks by category to do tasks by you know by day something like that and have a partition key named you know if it's to do by family i would have family as a partition key here there is no need about that now it's a full plain list nothing special so uid is totally fine to be the partition uid is a full primary key no clustering column simple uid so you will get a unique record per partition totally fine with customer it will be evenly distributing with the hashing function as usual title completed offset not part of the primary key i don't need something anything special about it um yeah i would probably order the result based on the offset this is why order slash offset here i didn't use order because order is a key word of the language so avoid to use the keyword of the language as much as you can there is no real reason to use order simply use offset okay and so this is simply my table my logical data model for the table if i move that into cql i will first define the type scalar type uid is a uid title is a text completed in the boolean and offset is an integer and i can move to the sql custom required language statement to create a table okay create table if not exist uid title completed offset you got it and the primary key uid remember i use double set of bracket just to told you i know what i'm doing and i know that duid is the primary key but also the partition okay enough with data modeling and if you already take the courses before i think that's a good sample to go through and say oh so this is how you actually do the methodology it's a simple one okay but it's always the same method okay so we do have the we do have the table or we will have the table very soon by executing a new a new unit test but now it's a crud repository so we want to do the operation create read update delete on the tables so and to do that we will use the sql session we've just created and execute some queries yes it is okay so each string is under the hood uh martial as the statement okay and so first you know so baby step if you are doing that yeah you're very totally new with cassandra and maybe even with databases what about secret injection with the skull okay so first step is to do explicitly a simple statement you know what we are doing we are creating an object called system statement and in the statement there they are uh metadata you know paging size timeout consistency level all the metadata of the query will be part of the simple statement to improve the performance okay so i can do a new instance and still provide the full query and not bad first baby step to improve externalize the value using question mark and use either positional value because it's question mark or label you know semicolon label and i will add name view and i will execute my statement okay so in the next exercise you will first create a table and then you have to implement a simple statement okay but simple statement is not the best because each time you execute a simple statement cassandra needs to validate the syntax of the query validating that the table is okay validating that the column exists that the column of the proper type that the the the type you the parameter you provided match the type and so forth and so on and to improve the speed of your queries you will prepare your statement okay so we'll take your simple statement or here the string and you will at the beginning of your app at the load of your app prepare the statement you will get an object called the prepare statement okay and now each time you want to invoke these this query you will bind the parameter and execute the bound statement no validation anymore of the syntax of the grammar of your query much much faster so if you don't do that in your custom graph it starts it's a good start to improve it the performance all right okay uh so we execute the query okay create read update delete and now we get a result set so the result is part of the result set same as the relational gdbc world so two parts in the result set first is metadata what i call the plan berry okay execution info query tries how much time did the query last if you're using lw2 lightweight transaction was it applied no yes okay everything related to metadata prime and the data themselves and as you can see the result set does not contain a list of results it's not a list of record it's an iterable of record okay an integrable of all and the reason is when you execute a query all queries are paged okay cassandra is there to store billions and billions and billions of records in the single table this is for big data and so you don't want to do select stuff from the table in cassandra so first you will get huge number of results and also uh it will take some time you will need to do a full scan of your cluster multiple node to get all the results and then imagine that all this information come back to the node the coordinator node executing the query best case scenario you know the coordinator node has more memory than you so the node is okay give you all that info and then you do out of memory worst case scenario according to the node is out of node so this is now how it works you provide a page size and default page size is 5000 and so when you execute the query you get only the first page you get an iterator of the record and you get only the first pane has been fetched and this is why we could get available without fetching let's pretend you do have a page of 5000 and you get item first call 300 you know that you got everything in the single fetch totally fine but now if if you know in the table there is more than 5k well this is when you will iterate on the interrupter and when you will reach one of the latest item of the of the integrator when the interactor will be exhausted the driver will do fetching of paged the next page under the hood for you okay it's done for you okay all right so in the result set you can do dot all so please don't do that immediately without thinking because dot all will exhaust the iterator and do all the fetch for all the pages and so if you do again select stuff from the table you can still blow on your your your app but now it's not the coordinator not coordinator is happy and doing page after page uh because a driver will ask page per page but maybe you get here billions of record and you blow up your your memory okay that's it's it's possible okay uh iterator and you can of course use lambda and was applied okay it's just a metadata of the query okay moving forward passing the row okay you get the row one record in the record you can same as gdbc get column by column name column type or using cql identifier which is a small trick to escape all the not valid characters in the string for column name for key space name that guy will encode the characters what you need so you might find securing the identifier here and there in the code don't worry you can still do string to securely anti-fire identifier to string no problem all right so now it's your turn and this one will be a bit longer okay and i will let you do it uh i will do it in front of you as usual or people like watching maybe you have gone and starting this exercise before me if you would totally develop a geek like me this is a good problem this is the thing i would probably do if i was watching my own workshop i want to all right okay uh okay and there will be three repo one is using the driver and basic driver then he's using advanced feature of the driver i will discuss right after this one and then spring data but spring data you know it was not that easy to write the textbook so i will do it in front of you you do have the code but i will have to update the readme after the workshop because at the minute you just put spring data instance uh i know spring data cassandra dependency in your spring boot app okay you're downloading half of internet and this guy tried to do a lot of stuff under the hood for you and you don't want that so it will work but i will explain how to do so okay so credit repository so it's time to define the data model this is a table all right and so we will create the table in the astra instance using this g unit so let's see what it looks like i will close everything i have and i will open the unit number three so now uh don't worry about connectivity it's done for you it's there okay and so what you want to do is to execute the statement and so we will create a simple statement so because we are working on the schema there is no real some schema validation right because it does not exist so there is no need to prepare a statement based on schema yeah okay got it so what i did um i can either you know write down the full query string so this one and ask you know secure session that execute or you can use some schema builder so we love the builders in uh that driver world where you will define uh create tables with partition key with column with column with column.build and here there is a trick and i like to think about the trick you know to have all the constants from the schema in your in your you know it's working for any database to be honest you should create an interface providing all the constants in your schema you know all the table names color names and everything and it's really easy to implement this interface wherever you need it and then you will only have to work with you know with constants and if tomorrow you want to change the name of the column you go there and then change the name of the column and the full app is updated if tomorrow you want to delete one column you delete the attributes in the interface and boom you got some compilation error wherever it's used pretty easy for you know to see where you need to change the code so this is what i do i'm creating interface and interface anytime i need it i will simply implement my interface even if if even if it's a unit test i'm implementing my interface and i do have all the constant i need to work with my schema okay and then i execute and if i'm happy i do have now my table okay so okay let's go run as unit test in the readme again you do have the maven test d and the class name okay so blah blah blah table to do has been created well don't try don't uh no don't trust the log let's go back to astra maybe the data stack studio is still there for you okay refresh you know timeout we know what it is and now i will describe my key space okay oh i do have my two tables okay pretty nice right now i do have something to work with and create stuff okay [Music] let's create some data we'll use so let's say uh some task one okay [Music] i love casama [Music] and let's see uh java is live you can thumb up the video now because you like what i say and i execute and uh oh no this is not what i want because they will be all completed let's put one with false oh what did i do i just execute the same statement twice and i provided uid each time so guess what now i do have six record anyway not a big deal select star from blah blah blah tables okay so each time you do that think about how many records are in the table how many nodes you will scan it's a single node cluster okay and there is only six record in the table i'm safe okay and i do have my blah blah blah okay okay one two three one two three okay so okay let's get some more okay we will do that with the okay i just create a bunch of data to have something in the ui okay life is live the same okay okay okay okay all right that's it we have created the table and i can execute the describe space and now i can see oh there is also a sql console you know for those who love so in astra ui there is also the else to know how your cluster behave how many nodes is it okay how many compaction how many read writes you can do that and there is secure console okay if you love secure sh here it is and i can do so i think it was to do user to do password and so what i would love to do is first use uh to do app which is my key space okay and oh i there is no need to do that i think i could describe key space the name of my key space and oh wow and i can do also well let's do it select from uh i don't have to use a prefix but anyway yeah so happy life is life i love customers twice okay uh done moving forward i told you it would be practice practice practice okay moving forward so now let's go to crud with cassandra driver oh okay it's starting getting interesting after 90 minutes not bad now i will do some stuff and i will insert a new task using java okay your job so i'm going to proper unique desktop so this one is crud blah blah blah cred with driver driver blah blah blah okay this guy uh so this one okay first time uh first time i show you that how to combine spring and unit five and so so a run riff you only that you only need this annotation to run unit test in eclipse because this eclipse is just sometimes a bit stupid but maven run without this annotation working but spring's unit is pretty neat because providing a test configuration you can define the bin you need and so i need a secure session to connect to astral and i i need uh my class to do repository cassandra diver implementation just to execute some create read instance operation with this class i don't need to start the full app i could i also could use some context configuration annotation of course providing the list of the class but hey i simply want to do my own bin and don't be you know don't have any adherence any really any dependency with existing code outside my unit test which is a good practice i guess right all right okay so you need secure station and init repo so now i can auto wire my sql session because it's there and i can auto wire on my to-do list repo because it's there but before using those two okay i will ask you to write the proper statement to insert this task per task into uh into a strap okay and with that i will we use a pre-implemented class just to see if you did it well and say that it's not empty that mean defined by id return the result okay so just to tell you uh you will need to create a simple statement okay and then you will need to execute the statement okay and now remember in the slide we had something statement builder new instance ah it's a new oh jesus ah i will simply go just new simple statement and provide the query string now i cannot do that i cannot do that can i alright you know what the solution is at the bottom of the page or just in the readme it's simple statement dot builder okay i got it but this is what i just did oh i did simple statement window dot something okay so far so good now let's see what we do so um insert into to do task okay you got it up here it is inside into to-do task okay and then i need values okay and you can either use positional value or name value i will simply use a positional value one two three four okay and so i will add positional value okay and build okay so first is uid okay i've generated a random uid do i have a logo okay anyways this out that's it's life life is life it's an enzyme okay anyways i will add position value two three four i do have four parameter okay second is title then is offset okay all right and then i can run the test insert so it was weird before to have this uh issue not finding test inserts because it's there maybe it's a typo in the command but anyway i will test insert cross finger that i don't have fat or i do have fat [Music] see it's not pre-recorded insert next time i copy paste the solution okay and now this guy has been created yes he's out remember so if i go to astra uh data stack studio uh i can go at the bottom and maybe see do i have a new task of select from [Music] this guy no quote because uid is an object not a string and true a task has been inserted in astra yes we should use uh to do app schema yeah that's a good point that's a really good point so if i go back here simple statement builder insert into that you know what uh you we can not only using uh we can not only using uh the to-do app but also a query builder there is something called query builder and you can you know insert into only providing the name of the table okay and you know just then using value name value name value name value and column id of course now we i would use to do to do apps so this guy is called something okay so implements to do app schema and i will do the query builder value la la la la you got the id okay i'm moving forward because you probably already have written all that and i'm just like behind so now it's really time to connect finally the web ui to what we just did if you go back to the repo so it's to do list repository customer implementation so here you got the the answer so i i did i did the same answer but now for all the other operation it's pre-implemented and as you can see i'm using the query builder select from so it's import static query select form table so i want all the columns where collin id is equal to build and i will prepare the statement at the launch of the app and when i need for instance to do select from now if i go to find my id i will simply use the prepared statement and bind the statement with only the parameter and that's quite nice code you know elegant single line meaning that you like what you do all right always assert not new okay every java null pointer exception in your code is your fault no excuse okay you didn't test your parameter don't you yeah that's a bit rough but at the same time yeah gnp most of the time is your fault test your parameter anytime okay moving forward so if i go back to the exercise catch up a little bit attack injection dependencies i will now change from in-memory to cassandra driver okay so let's go here go to my controller okay find my qualifier okay change with the java driver but we still need a secure session right because for now it was only using sql session in the test in application.test so in the exercise there is a command asking you to copy everything from application underscore test to application.conf okay and as you can see it's the same file for me okay and so i'm going to go to cassandra driver config and i will uncomment configuration so now when i will start my app spring will recognize this guy as something to load in the in the spring context i will use sql builder.build delegated everything in a file go to the file connect to astra and now cassandra driver implementation do have a secure session to work with and can do stuff by the way no see i also need to uncomment this line to be sure that this guy is part of the of the spring of the spring context and then i should start my app so let's go back here it takes a bit more time because i initiate my connection to astra now and if i go back to the to-do back-end clients and instead of using these i'm using my localhost okay boom guess what now i do have the task in astra life is live okay to time uh this one was also two time simple task okay and this guy would just insert it with a unit test and you know by clicking here don't believe me go back to the studio execute select star from the table only you know it's it's live so let's see close the proper tab where is my client okay the demo is live live no the demo is alive okay and i go back to my studio how cool okay so i promised you did it there is a sample a rest api connected to cassandra and serving web ui and if you're using git but you didn't have installed anything so more quickly so there is something called an object mapper in the driver allowing so new dependency in the code but i did that for you already the purpose is you define some entities providing annotation and some interface no implementation only interface okay providing queries only you know insert save delete query provider like that okay you then define a mapper and that's it all the crud has been done for you no code no statement nothing the mapper is pretty clever and doing it for you okay so yeah maybe before the mentee to to work with the mapper you will simply shut down okay this guy i will go to the mapper and comment okay so this repo is there object mapper okay it's so as you can see everything related to the mapper is in dedicated package so the entity i open no the entity first okay the dao no code and the mapper just in my mapper i can have multiple deo single mapper for the app and here i do have a single diode factory like that okay and so my perimeter is there and i can go back to my controller let's see and i remove my cassandra driver switch to object mapper and i should refresh cross finger and now you do have repository using object matter coded for you no exercise on this one simply have to map what you need okay uh okay i said okay but i didn't try it uh blue blue blue where is my ui okay let's do it again oh yeah and now uh object mapper okay go back to astra studio yes and now i do have my new lag okay so it's working the same but instead of you providing all the statements it's done by an object mapper okay so we did that i did that in front of you next is spring data okay so now it's really for folks who is using spring boot they're very good the go-to when talking with database spring data because these guys do the same as object mapping help you to map object to the database and you don't have to provide code it's all convention over configuration and so you only have to have interface and spring data will generate that for you so spring data is there for the persistent and then to expose you can either use spring mvc and this is what i use today because i had to stick to a proper specification remember you need test to execute integration test to execute but with spring data there is some couple more annotation to directly expose a repository as a resting point i'm not sure i like this approach you know i like to validate my my parameter i like to be really cushioned about the http code i use so i you know i do use always uh webmbc but spring data rest is there and i think that spring dock is also compatible with spring data rest because spring data rs exposed some main points using a2s and that's kind of a format that spring dock understand but a food disclosure i was not able to make it bring that at rest okay but yeah spring missy is there so okay so will spring data multiple approach don't define anything you simply provide keys in application.tml and this guy will generate the sql bin for you and inject the secret bean in crud for you as well again i don't really like this because you need to know all the keys and even if there is some auto completion yeah you're struggling you know for for ages before finding what you need whereas you can also uh ex and it's a slide yeah whereas you can also x you can extend abstract cassandra configuration and then you need to override a couple of methods and you know what you need to provide and then you can use auto wired you know values spring properties configuration as usual and you also have access to the sql session with sql session builder customizer and you can do what you want with secure session once you did both you can create tasks with entities with annotations same as object mapper and do the same with select star okay all right and now let's do the creed repo so i will do it in front of you again i told you i'm not sure about the the read me here okay i've lost my eclipse uh it's the end okay this top desktop this one looks eclipse okay let's do it this way where are you okay i will close this guy okay so now i'm going to the bomb i will uncomment spring data okay where are you uh no i will uncomment spring data okay the minute i'm uncommenting spring data this guy will start to create a lot of bin for me so i need to tell him what to do okay so let's see i don't need this customer configuration anymore because i want to extend the java so everything is now in the package spring data what a surprise right so first i need to change my config i will uncomment config and as you can see this spring data java config extend the abstract configuration yes i will create my sql station and i will provide key space name part of the sql station here to be honest and i will uh import what i need pub shift or command shift or or ctrl shift on windows so first i have imported spring data then i do have created my bin to connect to spring data okay what's next task switch data i will annotate my entity can i just do source toggle command okay these guys up for this guy i will simply do that as test okay so as i can as you can see i provide the type and the name okay name this one from spring data okay and now so config another repo okay so now i need to uncommand my repo okay and i need so i don't need it was just a sample to show you how to do it but i will extend the cassandra repository class and i will import one query and you know with only that now task spring data repository provide all the method i need to uh you know create read update delete they are all created by uh spring data for me i don't have to worry about that okay almost there okay so now i'm going to the repo it's it's just uncommenting everything so sql session i will inject my repo i will use injection by construction by constructor and i will and comments every single method yeah so explain that in the readme okay it's wise and comment whatever you find in in the code to make that work okay we uncommented the config the entity the repo and then the the ripple and with that ladies and gentlemen i only need to go back to the controller in resources and now instead of using object mapper i will using spring data okay so if i do it again this is my controller i'm using to do this repository connecting to an implementation from spring data okay using you know some kind of i should have called that maybe the ao class really dedicated to spring data but as i do have multiple implementation i did this little router okay let's see moment of trush okay so this guy is not happy constructor a single bin but two version was first person right please configure cassandra configuration in spring data config okay so i do have two being defined in here okay like the security session com samples spring data men they are both defined in the same class sql session customer session okay uh okay configuration abstracts okay abstract configuration uh no i'm sorry i simply have my own okay i simply have my own okay it's used to it's just about that i don't know why this guy tried to create two okay okay just ask me to create mine is primary okay mine i don't remember do that but hey this guy want a primary i will give you a primary all the more as the cassandra session by default yeah you know what cassandra default created all by default try to connect to local so let's try to help this guy inside implements secure session what was it again remember the slide took sequel session builder customization but you know what i won't probably fix it live which was strange because it worked maybe i i did uncomment what i should not anyway you got the id and i'm at the top of the hour secure session and this guy didn't want to find him anyway yes yeah not sure why it's not happy builder customers secure session window customizer so secure session builder customizer you know what i will go to i will go to my explicit test remember i do have secured session builder you can do you can do that and it should work but yeah never mind let's move forward you're still watching me debug not probably the funnest fun stuff to do okay so uh first resources for you the data stacks developer youtube channel okay this one please subscribe if you can um like the video if you like the content today we are doing live every wednesday same time okay more sample code okay i do provide the slide to you so i this is a sample with rest but i do have some perfect graphql grpc kafka web flux and serverless those are all the links so it will be in the slide deck okay in the github repo but also go to my github github.com c-l-u-n you find them here they are and this is a survey we will do in the end but before that uh if you like the session and want to know more about cassandra go to academy.com you'll find all the training to be able to pass the certification if you do have questions go to community.datastacks.com it's like stack overflow for cassandra okay again i told you 30 times to subscribe to the channel okay we told you you would get a free von show to be certified here is the link i think we share the link 20 times already but there are two certifications developer associate administrator associate to pass the developer associates uh two courses are recommended ts201 ds220 and to have the administrator certification you add 210 which is all about monitoring with customer okay in upcoming events so next week i told you build the rest api for cassandra with node and python and starting july the first we are rebooting the workshop series with something called cassandra workshop theories eight weeks eight workshops starting from core cassandra data modeling application development advanced application development testing deploying monitoring and administrating so each week two hours workshop with me and that has tax advocate crew then exercise to do on your own at home uh and in the end you would be you have all the knowledge required to pass the certification with us and we will give some swag to anyone that dare attend the full week with us it's a full courses and i hope we will see you there so to register there for now simply go to eventbrite.com search for data stacks we will share the link in the next days i mean before next week's we will have all the link and all the arguments
Info
Channel: DataStax Developers
Views: 6,351
Rating: 4.8987341 out of 5
Keywords:
Id: KAcZg6l9QTw
Channel Id: undefined
Length: 106min 37sec (6397 seconds)
Published: Wed Jun 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.