Getting started with Google Cloud Datastore and Python/Protobuf
Before running through the steps below, make sure that:
- You have enabled Google Cloud Datastore API.
-
You have your
<dataset-id>
(same identifier as your Google Cloud Project ID ). -
You are
connected
to a Compute Engine instance with both the
datastore
anduserinfo.email
scopes or have a<service-account>
and the<path-to-private-key-file>
. - You have a working Python environment with pip and virtualenv .
In order to make API calls to the Datastore, you first need to install the googledatastore package by running the following commands:
virtualenv gcd
gcd/bin/pip install googledatastore
Then,
download
and unzip the latest version of the
google-cloud-datastore
samples:
unzip google-cloud-datastore-1beta2-rev1-2.1.0.zip
If you are not connected to a Compute Engine instance, make sure to run the following commands (in a bash-like shell):
# configure your credentials
export DATASTORE_SERVICE_ACCOUNT=<service-account>
export DATASTORE_PRIVATE_KEY_FILE=<path-to-private-key-file>
# install the Python package requirements
gcd/bin/pip install -r google-cloud-datastore-1beta2-rev1-2.1.0/python/requirements.txt
Finally, run the
adams.py
demo with your
<dataset-id>
as a parameter.
It will prompt for
the question
and validate your answer.
gcd/bin/python google-cloud-datastore-1beta2-rev1-2.1.0/python/demos/trivial/adams.py <dataset-id>
Meaning of life?
> 11
Don't Panic!
The comments in the sample's source explain its behavior in detail:
import logging
import sys
import googledatastore as datastore
def main():
# Set dataset id from command line argument.
if len(sys.argv) < 2:
print 'Usage: adams.py <DATASET_ID>'
sys.exit(1)
# Set the dataset from the command line parameters.
datastore.set_options(dataset=sys.argv[1])
try:
# Create a RPC request to begin a new transaction.
req = datastore.BeginTransactionRequest()
# Execute the RPC synchronously.
resp = datastore.begin_transaction(req)
# Get the transaction handle from the response.
tx = resp.transaction
# Create a RPC request to get entities by key.
req = datastore.LookupRequest()
# Create a new entity key.
key = datastore.Key()
# Set the entity key with only one `path_element`: no parent.
path = key.path_element.add()
path.kind = 'Trivia'
path.name = 'hgtg'
# Add one key to the lookup request.
req.key.extend([key])
# Set the transaction, so we get a consistent snapshot of the
# entity at the time the transaction started.
req.read_options.transaction = tx
# Execute the RPC and get the response.
resp = datastore.lookup(req)
# Create a RPC request to commit the transaction.
req = datastore.CommitRequest()
# Set the transaction to commit.
req.transaction = tx
if resp.found:
# Get the entity from the response if found.
entity = resp.found[0].entity
else:
# If no entity was found, insert a new one in the commit request mutation.
entity = req.mutation.insert.add()
# Copy the entity key.
entity.key.CopyFrom(key)
# Add two entity properties:
# - a utf-8 string: `question`
prop = entity.property.add()
prop.name = 'question'
prop.value.string_value = 'Meaning of life?'
# - a 64bit integer: `answer`
prop = entity.property.add()
prop.name = 'answer'
prop.value.integer_value = 42
# Execute the Commit RPC synchronously and ignore the response:
# Apply the insert mutation if the entity was not found and close
# the transaction.
datastore.commit(req)
# Get question property value.
question = entity.property[0].value.string_value
# Get answer property value.
answer = entity.property[1].value.integer_value
# Print the question and read one line from stdin.
print question
result = raw_input('> ')
if result == str(answer):
print ('fascinating, extraordinary and, '
'when you think hard about it, completely obvious.')
else:
print "Don't Panic!"
except datastore.RPCError as e:
# RPCError is raised if any error happened during a RPC.
# It includes the `method` called and the `reason` of the
# failure as well as the original `HTTPResponse` object.
logging.error('Error while doing datastore operation')
logging.error('RPCError: %(method)s %(reason)s',
{'method': e.method,
'reason': e.reason})
logging.error('HTTPError: %(status)s %(reason)s',
{'status': e.response.status,
'reason': e.response.reason})
return
if __name__ == '__main__':
main()
With this example, you learned how to use the:
-
googledatastore
package to connect to the Datastore API. -
begin_transaction
method to start a transaction. -
lookup
method to retrieve entities by key from your dataset. -
commit
method to send mutations to entities in your dataset and commit the transaction.
Now, you are ready to learn more about the Key Datastore Concepts and look at the Python API reference and the Protocol Buffers service definition .