- Goal
タブ区切りのテキストファイルをローカルマシン(OR AWSのEC2でも同じ)GCPのDatastoreにインポートする。pythonのプログラムで実施。
前のPOSTのKeyを自分で設定しないバージョン - Case
以下のようなテキストファイルを、Datastoreにインポートする。
想定するのは、DatastoreをGAEのマスタのように利用していて、バッチで更新をするようなケース。keyは、GCPで自動設定されるKeyを使う。
list.txtの中身(タブ区切り)
id shimei score
1234 山田 89
2341 田中 90
1238 佐藤 81 - How
1.GCPのコンソールで、Service Account Keyを作成する
形式は、JSON
権限は、DatastoreのOwner(これは、Import&Exportでも良いのかも?)
作成された、JSONはローカルマシンのどこかに設置する。
2.Google cloud datastore のクライアントライブラリをインストールする。
~$ pip install google-cloud-datastore
3.pythonのプログラムでインポートする。
##utf-8に変換⇒Unicodeに変換して、インポートすること!
あとで、Pythonから呼び出しをする事を想定している。This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersfrom google.cloud import datastore infile = 'list.txt' datastore_client = datastore.Client.from_service_account_json('gcp_ds.json') kind = 'testscoreNokey' with open(infile, 'r') as inputfile: inputfile.readline() for line in inputfile.readlines(): i = line.split('\t') id = i[0].decode('utf-8') shimei = i[1].decode('utf-8') score = i[2].decode('utf-8') item_key = datastore_client.key(kind) item = datastore.Entity(key=item_key) item['id'] = unicode(id) item['shimei'] = unicode(shimei) item['score'] = unicode(score) datastore_client.put(item) - Result
Oracle Application Express Notes | Apps development Notes | Google Cloud Platform | Python | apps test | Cool Beans | English | Books
2018/09/02
GCP: GCP外のサーバーからDatastoreへデータをインポートする(GCPのKeyを使う場合)
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿