You want to update, again you can update just the batting average of a player, and
you can update this in a transactional way.
So Datastore supports transactions, it allows you to read and
write structured data.
It is a pretty good replacement for use cases in your application,
where you may be using a relational database to persist your data.
However, this replacement is something that you would have to do explicitly.
Unlike the things that we've talked about in the previous chapter you can't just,
for example in the previous chapter we said you have a spark
program that you are running on a Hadoop cluster On-Premise.
You want to run it on GCP just run it on Dataproc,
pretty much all of your code just migrates unchanged.
If you have a MySQL code base,
well whatever you're doing to your MySQL On-Premise you can do MySQL on
Google Cloud using Cloud SQL, those are easy migrations, right?
Take what you have, take those use cases that you have,
just move them to the cloud, but when we talk about something like Datastore,
now it's not that easy a migration.
You have to change the code that you're doing, where the way you interact with
Datastore is different from the way you'd interact with a relational database.
So how do you interact with Datastore?
Well, the way you work with Datastore is that it's like a persistent Hashmap.
So for example let's say we want to persist objects that are author objects.
You'd say I have an author class, it's an Entity, that's the identity.
It's an annotation that you add and I'm showing you Java here, but
it works with a variety of object oriented languages.
And you say that the author is distinguished by their email address,
the email address is an Id column, so you say add Id.
We want to search for authors by name, so
we'd like the name property to be indexed, and just to show you that you can have
hazard relationships, an author has a bunch of different interests.
Same thing about guestbook entries, you store guestbook entries, each entry has
an id that makes it unique, it basically has a Parent Key and Author.
These are the people who wrote the GuestbookEntry and that's a relationship.
You have messages,
we're never going to search apparently because it's not indexed.
We're not going to search for
guestbook entries based on the text of the message and we have dates, right?
And that's something that we might want the search based on.
So once you have an Entity, you have an Author, there's an Entity you have,
an Author has an email which is the id, and a name which is the index.
So you want to create an Author, you basically call the constructor,
just as you would do for any plain old job or object.
A new Author xjin@bu.edu, name is xjin,
you have your Author object, but at this point the Author object is only in memory.
You want to save it, you basically call save passing in the entity.
ofy here is the objective file library,
it's one of several Java libraries that help you deal with Datastore.
So in this case this code is showing you objective file, we save the entity and
at this point the xjin object has been persisted.
If you want to read it, if you want to search for it,
what you can do is say load all authors and filter them by name Ha Jin,
and because name is an indexed field we can do this.
We can filter by name Ha Jin, and we will get back an iterable of authors.