Contents
Introduction to Google Cloud Firestore
Picking the right database solution on Google Cloud Platform can be tricky. There are so many options to choose from, and sometimes the differences between them are not very obvious. In this course, you will learn about Firestore, one of Google’s NoSQL databases. We will cover what it can do, how it differs from some other databases, and how to get started using it.
Learning Objectives
- Understand what Google Cloud Firestore is
- Learn how to use it to store and retrieve data
- Know how to create indexes
- Know how to install the emulators for local development
Intended Audience
- Database administrators
- Cloud Architects
- GCP Developers
- Anyone preparing for a Google Cloud certification
Prerequisites
- Some experience with databases and writing queries
Now you should be aware that you can only import data if it is in a special format. You cannot import common formats like CSV. (That stands for comma-separated values). If you need to import a CSV file, then you are going to have to write some custom code. I can demonstrate how to import data that I previously exported. These files need to be stored in a Cloud Storage bucket. You can choose to limit the imported records to a particular namespace and/or kind. Alright, so that’s pretty straight forward.
While I wait for this import to finish, I am going to demonstrate it for Native mode as well. It is a very similar process. Here you pick your file. And there are no namespaces or kinds, so you don’t have to worry about that. And after a short amount of time, it will complete.
Ok, so here are the ten books I imported in Datastore mode. Exporting this data is almost the same process. You can filter which records to export by specifying a namespace and/or kind. And then pick the location to save the file to.
Here are the same records in Native mode. Notice I have to click on the document IDs to see the fields. Exporting here is very similar. You can export all records, or you can filter by collection. And then pick the bucket and location to save the file to. Pretty simple stuff.
Ok, I have a few more things that I want to run through. Next, we are going to look at building queries. Now Datastore mode provides a nice and easy way to do that in the console. You can click on this tab here called “Query by GQL”. GQL or the “Google Query Language” is very similar to Structured Query Language or SQL. So if you already are familiar with writing SQL queries, then this will be pretty intuitive. To view all my “book” entities I can use this query. Notice that the results are shown at the bottom here. Now I can also search for a particular author using this method. Or I can search for a specific ISBN number like this. Now all these queries work because I am using indexed fields. If I try to do a query using a non-indexed field, I am not going to get any results. So even though I do have paperback books, this query is going to be empty because the “paperback” field is not indexed. So remember, if you are not getting any results, you might need to verify that your fields are indexed. Now, this is great for testing or debugging. But of course, most of your queries are going to be written in Java, Python, Node.js or Go by using the appropriate Google client library.
In Native mode, you do not have a way to write or run queries using the web console. There is no support for Google Query Language. So in this mode, all queries need to be written in your web or mobile app. Now, I am not going to demonstrate that since it is going to depend upon your platform and your programming language. Obviously, Android code is going to look a lot different than code for iOS. Instead, you should refer to the appropriate documentation for the Google Cloud Client library that you are going to be using.
There is just one last thing I want to cover. We already covered indexing single fields. But you can also create composite indexes as well. In Datastore mode, those need to be defined in your application. And then you can view any defined composite indexes here.
In Native mode, you can review your single field indexes here. And notice that you can exempt certain fields from indexing if you wish. But you can define composite indexes by clicking on this tab. Now if you know which composite indexes you need, then you can click on “Create Index”. But instead of creating them yourself, Google will automatically detect any required composite indexes you need. And then in your code, it is going to return a link that will generate it for you.
Alright, so that pretty much covers everything you need to get started. Now you know how to set up a database in both modes. You know how to add, edit, delete, and query records. And finally, you should understand what indexes are and know how to create them.
Daniel began his career as a Software Engineer, focusing mostly on web and mobile development. After twenty years of dealing with insufficient training and fragmented documentation, he decided to use his extensive experience to help the next generation of engineers.
Daniel has spent his most recent years designing and running technical classes for both Amazon and Microsoft. Today at Cloud Academy, he is working on building out an extensive Google Cloud training library.
When he isn’t working or tinkering in his home lab, Daniel enjoys BBQing, target shooting, and watching classic movies.