LoginSignup
0
1

More than 5 years have passed since last update.

Python3 で Google Datastore のデータを作成 (Create)

Last updated at Posted at 2018-05-17

Google Datastore に次のようなデータを作成するプログラムです。
datastore_may1801.png

datastore_create.py
#! /usr/bin/python
#
#   datastore_create.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({
        'name_city': name_city,
        'population': population,
        'date_mod': datetime.datetime.utcnow()
        })

    client.put(task)

    return task.key

# ------------------------------------------------------------------
sys.stderr.write("*** start ***\n")

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")
#
sys.stderr.write("*** bbb ***\n")
add_data_proc(client,'t0921','宇都宮', 38921)
add_data_proc(client,'t0922','小山', 56413)
add_data_proc(client,'t0923','佐野', 92385)
add_data_proc(client,'t0924','足利', 39546)
add_data_proc(client,'t0925','日光', 18492)
#
#
sys.stderr.write("*** end ***\n")
#
# ------------------------------------------------------------------

実行コマンド

export GOOGLE_APPLICATION_CREDENTIALS="/home/uchida/my_project_Aug29-2016-4363ff6a1234.json"
./datastore_create.py
0
1
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
1