LoginSignup
0
0

More than 5 years have passed since last update.

Python3 で Google Datastore のデータを更新 (Update)

Last updated at Posted at 2018-05-18

Google Datastore にある次のデータを更新する方法です。
datastore_may1801.png

datastore_update.py
#! /usr/bin/python
#
#   datastore_update.py
#                       May/18/2018
# ------------------------------------------------------------------
from gcloud import datastore
import sys
import datetime
# ------------------------------------------------------------------
def add_data_proc(client,name,name_city,population):
    sys.stderr.write("name = " + name + "\n")
    kind = 'Cities'
    key = client.key(kind,name)

    task = datastore.Entity(key)

    task.update({
        'population': population,
        'name_city': name_city,
        'date_mod': datetime.datetime.utcnow()
        })

    client.put(task)

    return task.key

# ------------------------------------------------------------------
def name_city_read_proc(client,name):
    kind = 'Cities'
    key = client.key(kind,name)
    results = client.get(key)
#
    return results['name_city']
# ------------------------------------------------------------------
sys.stderr.write("*** start ***\n")
key_in = sys.argv[1]
population_in = int(sys.argv[2])
#
print("%s\t%d" % (key_in, population_in))

try:
    project_id = "my-project-aug-29-2016"
    client = datastore.Client(project_id)
except Exception as ee:
    sys.stderr.write("*** error *** in datastore.Client ***\n")
    sys.stderr.write(str(ee) + "\n")
#
name_city = name_city_read_proc(client,key_in)
add_data_proc(client,key_in,name_city,population_in)
#
sys.stderr.write("*** end ***\n")
#
# ------------------------------------------------------------------

実行コマンド

export GOOGLE_APPLICATION_CREDENTIALS="/home/uchida/my_project_Aug-29-2016-7313bb6c3526.json"
#
./datastore_update.py t0922 98912355
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0