python3 で、Google datastore のデータを読む

  • 0
    Like
  • 0
    Comment

    Google datastore の次のデータを python3 で読みます。
    datastore_jun13.png

    datastore_read.py
    #! /usr/bin/python3
    # -*- coding: utf-8 -*-
    #
    #   datastore_read.py
    #
    #                       Jun/13/2017
    # ------------------------------------------------------------------
    import sys
    import json
    import datetime
    from gcloud import datastore
    #
    sys.stderr.write("*** 開始 ***\n")
    client = datastore.Client("my-project-aug-29-2016")
    query = client.query(namespace='test',kind='test')
    #
    results = query.fetch()
    #
    for unit in results:
    #   print(unit)
    #   print()
    #
        str_out = unit['name'] + '\t'
        str_out += str(unit['population']) + '\t'
        str_out += unit['date_mod'].strftime("%Y/%m/%d %H:%M:%S")
        print(str_out)
    #
    sys.stderr.write("*** 終了 ***\n")
    # ------------------------------------------------------------------
    

    実行スクリプト

    export GOOGLE_APPLICATION_CREDENTIALS=my-project-aug-29-2016.json
    ./datastore_read.py
    

    実行結果

    *** 開始 ***
    宇都宮   41295   2017/06/13 00:01:12
    小山  38756   2017/06/13 00:02:18
    佐野  71294   2017/06/13 00:03:48
    足利  27138   2017/06/13 00:04:49
    日光  74682   2017/06/13 00:05:47
    下野  82951   2017/06/13 07:39:28
    *** 終了 ***