如何将数据从在线GAE数据存储区加载到本地开发服务器?

时间:2012-11-13 19:15:14

标签: google-app-engine google-cloud-datastore app-engine-ndb database-backups bulkloader

我以前使用GAE docs中描述的方法在实时数据存储上下载我的实体的备份。

目前,我有一个每个实体类型的csv文件,我通过编写bulkloader.yaml并使用此命令获得:

appcfg.py download_data --config_file=bulkloader.yaml --filename=users.csv --kind=Permission --url=http://your_app_id.appspot.com/_ah/remote_api

我还有一个使用命令获得的sql3转储文件:

appcfg.py download_data --kind=<kind> --url=http://your_app_id.appspot.com/_ah/remote_api --filename=<data-filename>

现在,如果我尝试这个命令:

appcfg.py upload_data --url=http://your_app_id.appspot.com/_ah/remote_api --kind=<kind> --filename=<data-filename>

通过localhost:8080替换URL,它要求我输入用户名/密码。现在即使在http://localhost:8080/_ah/remote_api中提供模拟用户名(test@example.com)并选中“admin”复选框,它也总是给我一个身份验证错误。

文档中提到的另一个替代方法是使用它:

appcfg.py upload_data --config_file=album_loader.py --filename=album_data.csv --kind=Album --url=http://localhost:8080/_ah/remote_api <app-directory>

我写了一个加载器,并尝试了它,它还要求输入用户名和密码,但它在这里接受任何东西。输出如下:

/usr/local/google_appengine/google/appengine/api/search/search.py:232: UserWarning: DocumentOperationResult._code is deprecated. Use OperationResult._code instead.
  'Use OperationResult.%s instead.' % (name, name))
/usr/local/google_appengine/google/appengine/api/search/search.py:232: UserWarning: DocumentOperationResult._CODES is deprecated. Use OperationResult._CODES instead.
  'Use OperationResult.%s instead.' % (name, name))
Application: knowledgetestgame
Uploading data records.
[INFO    ] Logging to bulkloader-log-20121113.210613
[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-20121113.210613.sql3
Please enter login credentials for localhost
Email: test@example.com
Password for test@example.com: 
[INFO    ] Connecting to localhost:8080/_ah/remote_api
[INFO    ] Starting import; maximum 10 entities per post
[ERROR   ] [WorkerThread-4] WorkerThread:
Traceback (most recent call last):
  File "/usr/local/google_appengine/google/appengine/tools/adaptive_thread_pool.py", line 176, in WorkOnItems
    status, instruction = item.PerformWork(self.__thread_pool)
  File "/usr/local/google_appengine/google/appengine/tools/bulkloader.py", line 764, in PerformWork
    transfer_time = self._TransferItem(thread_pool)
  File "/usr/local/google_appengine/google/appengine/tools/bulkloader.py", line 933, in _TransferItem
    self.content = self.request_manager.EncodeContent(self.rows)
  File "/usr/local/google_appengine/google/appengine/tools/bulkloader.py", line 1394, in EncodeContent
    entity = loader.create_entity(values, key_name=key, parent=parent)
  File "/usr/local/google_appengine/google/appengine/tools/bulkloader.py", line 2728, in create_entity
    (len(self.__properties), len(values)))
AssertionError: Expected 17 columns, found 18.
[INFO    ] [WorkerThread-5] Backing off due to errors: 1.0 seconds
[INFO    ] Unexpected thread death: WorkerThread-4
[INFO    ] An error occurred. Shutting down...
[ERROR   ] Error in WorkerThread-4: Expected 17 columns, found 18.

[INFO    ] 980 entities total, 0 previously transferred
[INFO    ] 0 entities (278 bytes) transferred in 5.9 seconds
[INFO    ] Some entities not successfully transferred

我总共有大约4000个实体,它在这里说980是转移的,但实际上我检查了本地数据存储区,但我找不到它们。

下面是我使用的加载器(我使用NDB作为Guess实体)

import datetime
from google.appengine.ext import db
from google.appengine.tools import bulkloader
from google.appengine.ext.ndb import key


class Guess(db.Model):
    pass

class GuessLoader(bulkloader.Loader):
    def __init__(self):
        bulkloader.Loader.__init__(self, 'Guess',
                                   [('selectedAssociation', lambda x: x.decode('utf-8')),
                                    ('suggestionsList', lambda x: x.decode('utf-8')),
                                    ('associationIndexInList', int),                                    
                                    ('timeEntered',
                                     lambda x: datetime.datetime.strptime(x, '%m/%d/%Y').date()),
                                    ('rank', int),
                                    ('topicName', lambda x: x.decode('utf-8')),
                                    ('topic', int),
                                    ('player', int),
                                    ('game', int),
                                    ('guessString', lambda x: x.decode('utf-8')),
                                    ('guessTime',
                                     lambda x: datetime.datetime.strptime(x, '%m/%d/%Y').date()),
                                    ('accountType', lambda x: x.decode('utf-8')),
                                    ('nthGuess', int),
                                    ('score', float),
                                    ('cutByRoundEnd', bool),
                                    ('suggestionsListDelay', int),
                                    ('occurrences', float)
                                   ])

loaders = [GuessLoader]

编辑:我刚刚在错误消息[ERROR ] Error in WorkerThread-0: Expected 17 columns, found 18.中注意到这一部分,而实际上我只是浏览了整个csv文件,并确保每行有18列。我检查了加载程序,发现我错过了key列,我给它一个类型int,但这不起作用。

2 个答案:

答案 0 :(得分:3)

如果您的身份验证有问题,请在appengine_config.py中添加以下内容:

if os.environ.get('SERVER_SOFTWARE','').startswith('Development'):
    remoteapi_CUSTOM_ENVIRONMENT_AUTHENTICATION = (
    'REMOTE_ADDR', ['127.0.0.1'])

然后运行

appcfg.py download_data --url=http://APPNAME.appspot.com/_ah/remote_api --filename=dump --kind=EntityName
appcfg.py upload_data --url=http://localhost:8080/_ah/remote_api --filename=dump --application=dev~APPNAME

答案 1 :(得分:1)

尝试按Enter(无用户名/密码)。这似乎对我有用。我的命令(用bash脚本包装,以防止我偶尔收到的导入错误)是:

#!/bin/bash

# Modify path
export PYTHONPATH=$PYTHONPATH:.

# Load data
python /path/to/app/config/appcfg.py upload_data \
--config_file=<my_loader.py> \
--filename=<output.csv> \
--kind=<kind> \
--application=dev~<application_id> \
--url=http://localhost:8088/_ah/remote_api ./

当提示输入Email时,我按Enter键,所有内容都上传到开发服务器。在这种情况下,我没有使用NDB,尽管我认为不应该有所作为。