2011年1月30日日曜日

Python CSVデータ 一括登録 bulkloader編

以前に書いたブログ。
Python CSVデータ 一括登録 TaskQueue編

なんだけど今回別の方法があると、こっちのほうが効率がいいのではと。

というかこっちを使うのが一般的ではないかと。

なので検証することに。

いろいろ検索するとたくさんの方が記載していました。Google本家も参考に。
Google App Engine データのアップロードとダウンロード
bulkloader.py を使って Google App Engine の本番サーバーから開発サーバーにデータを移す
他多数。

ただ今回実際にやってみて、整理するためにも書いときます。

まず、remote_apiの設定。app.yamlにて簡単に設定できます。

次に一括登録のために必要な登録するデータ(今回はCSV)とKindのPropertyとをマッピングすることが必要。

そのために下記ファイルを使用する。
bulkloader.yaml


自動生成が簡単にできます。もちろん生成後は多少編集が必要ですが。

下記コマンド。
C:\> appcfg.py create_bulkloader_config --filename=bulkloader.yaml --url=http://example.appspot.com/remote_api
実行結果。
Creating bulkloader configuration.
[INFO    ] Logging to bulkloader-log-20110129.101716
[INFO    ] Throttling transfers:
[INFO    ] Bandwidth: 250000 bytes/second
[INFO    ] HTTP connections: 8/second
[INFO    ] Entities inserted/fetched/modified: 20/second
[INFO    ] Batch Size: 10
[INFO    ] Opening database: bulkloader-progress-20110129.101716.sql3
[INFO    ] Opening database: bulkloader-results-20110129.101716.sql3
[INFO    ] Connecting to biyotest.appspot.com/remote_api
[INFO    ] Downloading kinds: ['__Stat_PropertyType_PropertyName_Kind__']
.[INFO    ] __Stat_PropertyType_PropertyName_Kind__: No descending index on __ke
y__, performing serial download
.........................................................
[INFO    ] Have 573 entities, 0 previously transferred
[INFO    ] 573 entities (223932 bytes) transferred in 78.1 seconds


これで取得完了。


yamlの中身は下記。

# Autogenerated bulkloader.yaml file.
# You must edit this file before using it. TODO: Remove this line when done.
# At a minimum address the items marked with TODO:
#  * Fill in connector and connector_options
#  * Review the property_map.
#    - Ensure the 'external_name' matches the name of your CSV column,
#      XML tag, etc.
#    - Check that __key__ property is what you want. Its value will become
#      the key name on import, and on export the value will be the Key
#      object.  If you would like automatic key generation on import and
#      omitting the key on export, you can remove the entire __key__
#      property from the property map.
# If you have module(s) with your model classes, add them here. Also
# change the kind properties to model_class.
python_preamble:
- import: base64
- import: re
- import: google.appengine.ext.bulkload.transform
- import: google.appengine.ext.bulkload.bulkloader_wizard
- import: google.appengine.ext.db
- import: google.appengine.api.datastore
- import: google.appengine.api.users

transformers:

- kind: Kind1
  connector: # TODO: Choose a connector here: csv, simplexml, etc...
  connector_options:
    # TODO: Add connector options here--these are specific to each connector.
  property_map:
    - property: __key__
      external_name: key
      export_transform: transform.key_id_or_name_as_string

    - property: password
      external_name: password
      # Type: String Stats: 1 properties of this type in this kind.

    - property: user
      external_name: user
      # Type: String Stats: 1 properties of this type in this kind.

データストアのKindの情報をダウンロードできる。



なので更新したいKindの #TODO~ となっている箇所を編集。



今回はCSVを使用する。
connector:csv

下記は設定しなくても良い。どんな場合に使うかはまだ不明。
connector_options: ← 削除
external_nameを編集する。


今回の場合はcsvを使用するので先頭行ににて使用している該当の項目名を設定する。


reference_propertyはreference_classを設定。
- property: user
      external_name: user
      # Type: Key Stats: 1286 properties of this type in this kind.
      import_transform: transform.create_foreign_key('User') reference_classを設定
      export_transform: transform.key_id_or_name_as_string
編集終了後データをアップロード。


下記コマンド。
C:\>appcfg.py upload_data --config_file=bulkloader.yaml --filename=example_data.csv
 --kind=Kind1 --url=http://example.appspot.com/remote_api
実行結果。
Uploading data records.
[INFO    ] Logging to bulkloader-log-20110129.151144
[INFO    ] Throttling transfers:
[INFO    ] Bandwidth: 250000 bytes/second
[INFO    ] HTTP connections: 8/second
[INFO    ] Entities inserted/fetched/modified: 20/second
[INFO    ] Batch Size: 10
[INFO    ] Opening database: bulkloader-progress-20110129.151144.sql3
[INFO    ] Connecting to biyotest.appspot.com/remote_api
[INFO    ] Starting import; maximum 10 entities per post
................................................................................
.................................................
[INFO    ] 1286 entites total, 0 previously transferred
[INFO    ] 1286 entities (3600417 bytes) transferred in 65.6 seconds
[INFO    ] All entities successfully transferred
これで一括登録完了。


気づき。

・blobpropertyは使用不可。
→登録できなかった。調べてみると方法があるみたいだけど、それでもうまくいかず。要調査。


・listpropertyは登録は完了するが、不正な値で登録されてしまう。
→こちらはサポート外?要調査


・下記propertyは登録しない。
 - property:_key_
 - property:_scatter_

→_key_は自動載番される。
→_scatter_はどのように使われているかは不明。


今回使用してみて、GAEのサーバーリソース軽減になるかと試してみたが、ほとんど一件一件登録する場合と変わらず。


ただ時間はかなりはやいので時間のメリットはあるかと。


リソースの使用量が同じぐらいならこちらを使うメリットはあるかなというところですね。


まあ細かいところはまだまだ勉強不足。


わからないことが多すぎて。


日々精進。


0 件のコメント:

コメントを投稿