from google.appengine.Method using ext import db.(db instead of ndb)
GAE (Google App Engine) * I think it's the same with GCE, but I've never used it
Python2.7
# Datastore operations from Cloud Console
## Meaning of words
An entity is equivalent to a record in RDBMS. Corresponds to a row in a table.
The type is equivalent to a table in RDBMS.
The key identifier is the Primary Key in RDBMS.
The namespace is unknown.
## Creating an entity
Transition to the creation screen from "Create entity" at the top of the data store screen.
--Namespace: [default]
--Type: UserData
--Key identifier: Custom name: xxxxxxxxxxxxxxxxx (lineId)
The following is added by selecting "Add Property".
lineId example:
--Name: lineId
--Type: String
--Value: xxxxxxxxxxxxxxxxx (lineId)
List of added properties.
- lineId:xxxxxxxxxxxxxxxxx(lineId)
- dashId:shoshuriki
--message: Someone is calling you
- registrationTime:2016/01/02 00:27:12
* I'm not sure about "index registration", but it seems that the search performance will improve if you register, so check it. The trade-off is unknown. (Insert slows down ??)
## Referencing and updating entities
Select "Name / ID" to move to the update screen. You can also check the value here.
--Namespace, type, key (key identifier), these cannot be updated (changed).
--Properties can be updated. (Name, type, value, all of which can be updated.)
--Select "Save" to save to the data store.
## Delete entity
Select from the checkbox to the left of "Name / ID"
Can be deleted from "Delete" on the right side of "Create Entity".
## Search for entities
Select a type of drop-down list or
Set conditions from "Filter Something"
It is possible to narrow down the entities to be displayed.
# Datastore operations from App Engine
[2017/1/4 postscript from here]
There are two ways to operate the Datastore from App Engine.
- db (from google.appengine.ext import db)
- ndb (from google.appengine.ext import ndb)
There is no difference in being able to operate the Datastore in both cases, but since ndb is a newer module, it seems that ndb is recommended when starting from now on.
If you have to use the db module for some reason, we would appreciate it if you could refer to this article.
What is ndb
https://cloud.google.com/appengine/docs/python/ndb/?hl=ja
About migration from db to ndb
https://cloud.google.com/appengine/docs/python/ndb/db_to_ndb?hl=ja
[2017/1/4 postscript so far]
Refer to the following page.
https://cloud.google.com/appengine/docs/python/datastore/entities
## Creating an entity
Before creating the entity, create the underlying class.
from google.appengine.ext import db
class UserData(db.Model): lineId = db.StringProperty() dashId = db.StringProperty() message = db.StringProperty() registrationTime = db.StringProperty()
Create an instance based on this and put it in the Datastore to create it as an entity.
userData = UserData( key_name = 'xxxxx(lineId)', lineId = 'xxxxx(lineId)', dashId = 'shoshuriki', message = u'Someone is calling you', registrationTime = '2016/01/02 00:27:12', ) userData.put()
The following is a comparison of the items set in Cloud Platform.
--Type: UserData (The class name becomes the type (kind) as it is. If you want to specify it, add the member function kind and return the character string, and it becomes the type (unconfirmed))
--Key identifier: Character string assigned to key_name
--Namespace: Not specified (where to specify is unknown)
## Referencing and updating entities
A method that works only if you know the key identifier.
Get the entity's key object from the key identifier and get the entity from the key object.
Since the entity is a class created by yourself, you can freely refer to and write to the property.
If you write it, send the entity to the Datastore by putting it at the end.
try: key = db.Key.from_path('UserData', 'xxxxx(lineId)') userData = db.get(key) old_message = userData.message #Reference part userData.message = u'Please come slowly' #Update part userData.put() #If only reference, put is unnecessary except: print(u'Could not be located')
* A try block is required because an exception will occur if the target is not found.
## Delete entity
A method that works only if you know the key identifier.
Get the entity's key object from the key identifier and delete the entity.
try: key = db.Key.from_path('UserData', 'xxxxx(lineId)') db.delete(key) except: print(u'Could not be located')
* A try block is required because an exception will occur if the target is not found.
## Search for entities
For one type, use the property value to refine and get the result.
If there is only one target entity, it is safe to specify limit = 1.
try: q = UserData.all() q.filter('dashId = ', 'shoshuriki') for p in q.run(limit=1): lineId = p.lineId #Reverse lookup of lineId from dashId except: print(u'Could not be located')
* A try block is required because an exception will occur if the target is not found.
* How to reverse key_name from dashId?
# reference
Google Cloud Platform-Datastore
https://console.cloud.google.com/datastore
DB to NDB Client Library Migration (Easy to understand because the usage is straightforward)
https://cloud.google.com/appengine/docs/python/ndb/db_to_ndb?hl=ja
Recommended Posts