Google Cloud Datastore

2.24.0 · active · verified Thu Apr 09

The `google-cloud-datastore` library is the official Python client for Google Cloud Datastore, a highly scalable NoSQL document database service. It provides APIs to store, query, and manage entities. This client also supports Firestore in Datastore mode. The current version is 2.24.0, and it follows the rapid release cadence of the broader `google-cloud-python` monorepo.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a `Datastore` client, create an entity with a specific key, save it, and then query for entities of a given kind. Ensure your `GOOGLE_CLOUD_PROJECT` environment variable is set or replace `'your-project-id'` with your actual GCP Project ID, and that you have authentication configured (e.g., `GOOGLE_APPLICATION_CREDENTIALS`).

import os
from google.cloud import datastore

# Your Google Cloud Project ID. Set as environment variable GOOGLE_CLOUD_PROJECT or replace 'your-project-id'.
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-project-id') 

# Instantiates a client
client = datastore.Client(project=project_id)

# 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 = client.key(kind, name)

# Prepares the new entity
task = datastore.Entity(key=task_key)
task['description'] = 'Buy groceries'
task['priority'] = 5
task['done'] = False

# Saves the entity
client.put(task)
print(f"Saved {task.key.name}: {task['description']}")

# Query for entities of kind 'Task'
query = client.query(kind=kind)
results = list(query.fetch())

print('\nEntities found:')
for entity in results:
    print(f"  Key: {entity.key.name}, Description: {entity['description']}, Done: {entity['done']}")

view raw JSON →