Week16 期末報告 - Selesfia/ComputerNetwork GitHub Wiki
NoSQL Database
Google Cloud Datastore is a NoSQL document database designed to provide automatic scaling, high performance, and ease of application development. As a fully managed service, it abstracts the complexities of database management, allowing developers to focus on building applications. Key features include support for ACID transactions, ensuring data consistency; schema-less data storage, offering flexibility in data modeling; and built-in query capabilities for efficient data retrieval
Python Code Example
Inserting Data in Native Mode (Firestore)
from google.cloud import firestore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path_to_key.json"
db = firestore.Client()
doc_ref = db.collection('users').document('213')
doc_ref.set({
'name': 'Bob',
'transaction_id': 1878,
'revenue': 135.55,
'paid': True
})
Inserting Data in Datastore Mode
from google.cloud import datastore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path_to_key.json"
client = datastore.Client()
key = client.key('users', '213')
entity = datastore.Entity(key=key)
entity.update({
'name': 'Bob',
'transaction_id': 1878,
'revenue': 135.55,
'paid': True
})
client.put(entity)
Querying Data in Native Mode (Firestore)
from google.cloud import firestore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path_to_key.json"
db = firestore.Client()
users_ref = db.collection('users')
docs = users_ref.stream()
for doc in docs:
print(f'{doc.id} => {doc.to_dict()}')
Querying Data in Datastore Mode
from google.cloud import datastore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path_to_key.json"
client = datastore.Client()
query = client.query(kind='users')
results = list(query.fetch())
for entity in results:
print(entity.key.name, entity)
Filtering Queries in Native Mode (Firestore)
from google.cloud import firestore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path_to_key.json"
db = firestore.Client()
users_ref = db.collection('users')
query = users_ref.where('paid', '==', True)
docs = query.stream()
for doc in docs:
print(f'{doc.id} => {doc.to_dict()}')
Filtering Queries in Datastore Mode
from google.cloud import datastore
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path_to_key.json"
client = datastore.Client()
query = client.query(kind='users')
query.add_filter('paid', '=', True)
results = list(query.fetch())
for entity in results:
print(entity.key.name, entity)
Reference:
https://googleapis.dev/python/python-ndb/0.2.0/ https://serhiipuzyrov.com/2020/06/google-cloud-datastore-a-simple-and-fast-nosql-database-for-your-project-with-python-api-examples/ https://medium.com/%40inehayadav28/google-cloud-datastore-2663bb3099d4 https://www.youtube.com/watch?v=DAuAGPnMeK4 https://github.com/googleapis/google-cloud-datastore https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/datastore/cloud-client/snippets.py https://stackoverflow.com/questions/39062334/google-cloud-datastore-api-in-python-code https://stackoverflow.com/questions/63791732/writing-a-python-script-to-fetch-content-of-a-datastore-entity-in-gcp
22/12/2024