Google Cloud Datastore – A Simple And Fast NoSQL Database For Your Project (with Python API examples)

Google Cloud Platform has a lot of different data storing options, but if you are looking for a simple NoSql solution with fast read-inserts – Google DataStore will be the right choice here. If you are writing some web application – Google Datastore will help you to easily handle your app’s data like data of your customers for example. It has a very simple Python API so you can implement Datastore to your app just in a few lines of code.

In order to start using Google Cloud Datastore – you should have at least one Google Cloud project in your Google Cloud Platform account. If you don’t have any – go to https://console.cloud.google.com/ and create one.
If you have it – go to “Storage” section in GCP menu, click “Datastore” and then “Entities”.

Here you should choose which kind of Datastore you want to use – Native mode (Firestore) or Datastore mode (classic Datastore).

You can find full information about differences between this modes in official documentation (https://cloud.google.com/firestore/docs/firestore-or-datastore), but let me show the differences in how the data is structured in this two different modes and examples of API for each one.
Let’s assume we want to insert this record to a Datastore:

User IdNameTransaction IDRevenuePaid
213Bob1878135.55True

Native mode

This is how our data will look like in Native mode (Firestore). As you can see it has 3 levels: collection, document and fields. In our case – “users” is the highest level of data that stores documents. Each document – is the id of individual user, and each id has fields that describes that user.

To be able to use Datastore in Native mode API – you should install Google Cloud Firestore API package with pip install google-cloud-firestore. Next – create the Service account and download it’s json key:
https://console.developers.google.com/apis/credentials
Now you can insert data to Datastore in Native mode using this code (replace key.json with the name of your Service account’s key):

from google.cloud import firestore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "key.json"
datastore_client = firestore.Client()
key = datastore_client.collection('users').document('213')
key.set({
    'name': 'Bob',
    'transaction_id': 1878,
    'revenue': 135.55,
    'paid': True
})

After adding different records to Datastore you can print all the data inside “users” collection with this code:

from google.cloud import firestore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "key.json"
datastore_client = firestore.Client()
docs = datastore_client.collection('users').stream()
for doc in docs:
    print(doc.id)
    print(doc.to_dict())

Or also you can query your records if you need only those records which match some filter. In this example we will get only those records, where “paid” field contains “True” value:

from google.cloud import firestore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "key.json"
datastore_client = firestore.Client()
docs = datastore_client.collection('users').where('paid', '==', True).stream()
for doc in docs:
    print(doc.id)
    print(doc.to_dict())

Datastore mode

This is how our data will look like in Datastore mode. As you can see it has 2 levels: kind and key-value pairs. In our case – “users” is the name of the kind (the highest level of data) and other data are stored in the key-value format, each record is called “entity”. Pay attention to “Name/ID” key – it is the special key which should be unique for each record if we don’t want to overwrite it (it is similar to document level in Native mode).

To be able to use Datastore mode API – you should install Google Cloud Datastore API package with pip install google-cloud-datastore. Next – create the Service account and download it’s json key:
https://console.developers.google.com/apis/credentials
Now you can insert data to Datastore in Datastore classic mode using this code (replace key.json with the name of your Service account’s key):

from google.cloud import datastore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "key.json"
datastore_client = datastore.Client()
key = datastore_client.key('users', '213')
task = datastore.Entity(key)
task.update({
    'name': 'Bob',
    'transaction_id': 1878,
    'revenue': 135.55,
    'paid': True
})
datastore_client.put(task)

Now you can print all the data inside “users” kind with this code:

from google.cloud import datastore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "key.json"
datastore_client = datastore.Client()
docs = datastore_client.query(kind='users').fetch()
for doc in docs:
    print(doc.key.name)
    print(doc)

And this is an example of how you can get only those entities, where “paid” field contains “True” value:

from google.cloud import datastore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "key.json"
datastore_client = datastore.Client()
docs = datastore_client.query(kind='users').add_filter('paid', '=', True).fetch()
for doc in docs:
    print(doc.key.name)
    print(doc)

So as you can see this two Datastore modes have a lot in common, but no matter which one you choose – you can see that they are both easy to understand and have a very accessible and intuitive API. All these qualities combined with a speed and scalability – make Google Datastore a perfect NoSQL solution for almost any web application or other IT project.

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments