The Spring REST API, how to work with Spring and how to connect Spring to the ScyllaDB Cluster.
Transcript
so we have our data our data is modeled we already know that it will be a stock application for tracking the price of the stocks we know that it will be a time series application and the data is time series and we already did the improvements so the server knows that it’s time series data we can now go further and we can go uh with the spring spring actually makes the stuff very easy for us because you can just go to the start spring i o and the start spring IO will give you an initializer of the application if you want to use the serial database since we are Cassandra secure compatible you can just pick for the supporting of the rest the stringvap component spring lab dependency and you of course need to fix string data for Apache Cassandra dependency which uses the default Cassandra driver the spring itself honors the Moto view controller principles so if you are not like whereabout then go look them up but long story short you just basically take this data model that we have and you just flip it over to a data object in Java so when you look at this model once you have this model you just pass on this model to think that they call in Spring repository and this repository will just wrap around all the communication with the database for you so basically you just need to use this repository and screen wheel out of the box create the queries for you and in a default way you don’t really have to do anything else that’s pretty much it and you’re done you’re able to read from the database however I prepared two parts for you I prepared for you the part where it’s a default approach with the simple repository where you just need to create the model and I prepared for you the more advanced part where you can override the queries and why uh the reason why is basically I want to show you like how you can customize the queries and make them tailored to what what you want to do with your application and maybe improve them like don’t get me wrong the connector in string is already or the repository in Spring the default one is already smart enough and it’s already good enough but I would like to point out that you should really use some latest spring because the versions before spring 250 they didn’t have this that optimal they were missing prepared queries for example but the newer strings they already have the prepared queries so you get them out of the box you don’t need to do anything I’m showing them in advance application so you can you can have a look how to simulate them even in the older Springs and the other stuff which I’m showing a little bit are some custom queries where you just override something or you build the query yourself to optimize it in a way and another important part how you can customize string is for example to play with paging you can just put there some collectors of the rows or you can just limit The Collector of the rows or just make them scan around there are some default ones in Spring but I said you can customize you can customize anything that’s a cool thing once you have this repository working uh then on top of this repository we will put or you can put the rest controller and the rest controller takes care of the rest routing so you you have a URL an HTTP URL and it’s like an old slash ad and this will be the URL for example for adding stocks if you will have slash list it will with the stocks so this is the this is the central piece that basically lists all these routes and lists what uh call or what operation has to be done and what data has to be gathered for it to happen into the background and of course passes the parameters and and passes them on to the whatever call you need to do or whatever repository query you need to call once you have this then your application is kind of complete but of course you want to switch it from your either local testing instance to Silver Cloud so there is the application Properties or if you override them there is application ammo and you can set there the contact points uh people generally do a mistake that they just use one contact Point don’t be shy use more of them I suggest to use at least two of them and Sila driver or Cassandra driver can switch between those contact points if one contact Point goes down it can go into a second contact point and this way you can keep your client highly available as well there will be like no single point of failure uh another important part and I will show it a little bit is the local DC like if your cluster has multiple data centers uh so your data is spread for example you have East Coast and West Coast then you definitely don’t want your East Coast clients to go and purchase the data from West Coast so with the local DC you can tell the client this is your local data center connect to it don’t really connect elsewhere because it will be slower uh then there’s of course username password to authenticate yourself and there is a key space name very basically store your tables and the consistency consistency is very important because it mandates if you want to create like fully consistent application or eventually consistent application uh this is an example of the application yaml so it’s an example from the more advanced demo code and as I said you see that there is like contact points and they are divided by comma and yes I just noticed I have a mistake here so who noticed the mistakes or the mistake has one point from me there are like quotes the quotes don’t belong here you just need to list the contact points with just a command I I will fix this in the slides local DC is the one that I mentioned to you so I’m collecting just to the one DC I have only one BC in Frankfurt I don’t have any other DC in minichen or or in London so I’m just thinking to Frankfurt username password of course mighty space which is the spring demo uh note that this key space is basically the same which we created in the data modeling part and we are back to the consistency and I promise to explain something about it so basically the consistency here you should try to make your reads and writes ideally uh if you count their consistency together it should be bigger than the replication factor in such case your application is fully consistent and if you don’t want fully consist application that you can downgrade the consistency the driver default is a local one so the driver default is not fully consistent it’s eventually consistent but if you want to be fully consistent if you switch your reads and rice to Forum then your application will always be consistent but at the same time you have to consider that this there will be a cost on the resources of the cluster so one query will always in this case if you have three nodes one query will always query two nodes at least or wait for the two nodes to acknowledge the right