Google Datastore
Note
Google Cloud now recommends using Firestore in Datastore mode for new projects. It is the next generation of Datastore and is backward-compatible with the concepts and client libraries described here.
What is Google Datastore?
- Google Datastore is a NoSQL Database used to store objects
- Datastore is robust, scalable and stored in several locations making data resilient to most service failures and planned downtime.
- This is exposed as a service, relieving us from the infrastructure-related configuration and administration.
Salient Features of the Datastore:
- Datastore is an object store. An object is called an Entity.
- An Entity has a key which uniquely identifies the object.
- A Key is composed of several parts: the application ID, a Kind (similar to a table name, e.g.,
Customer), and an entity ID (either a string specified by the application or an integer auto-generated by Datastore). - Data of an entity is stored with one or more properties.
- Each property has a name and at least one value.
- A property can have multiple values and each value can be of a different type
Difference between RDBMS and Datastore
- Kind in Datastore is like a Table in an RDBMS
- Entity is like a Row in an RDBMS
- Property is like a Column in an RDBMS
- Two entities of the same kind can have different properties set or not set
- Each entity can have the same property name but values of different data types.
- In other words Datastore is schemaless!
- You can use GQL (Google Query Language), which has a SQL-like syntax, to query for entities from the GCP console. GQL is more limited than SQL and does not support
JOINoperations.
Example Python Application
Below is a sample Python application that uses datastore.
# Imports the Google Cloud client library
from google.cloud import datastore
# Instantiates a client
datastore_client = datastore.Client()
# The kind for the new entity
kind = "Task"
# The name/ID for the new entity
name = "sampletask1"
# The Cloud Datastore key for the new entity
task_key = datastore_client.key(kind, name)
# Prepares the new entity
task = datastore.Entity(key=task_key)
task["description"] = "Do Math Homework"
# Saves the entity
datastore_client.put(task)
print(f"Saved {task.key.name}: {task['description']}")
To get this example working, you first have to create a project on GCP and then open Google Cloud Shell and run the above program. This program will add an Entity to the Datastore instance within the project you created.
References: