Sumber
Saya mendapatkan ini dari http://code.google.com/appengine/articles/remote_api.html .
Buat Konsol Interaktif
Pertama, Anda perlu menentukan konsol appenginge interaktif. Jadi, buat file bernama appengine_console.py dan masukkan ini:
#!/usr/bin/python
import code
import getpass
import sys
# These are for my OSX installation. Change it to match your google_appengine paths. sys.path.append("/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine")
sys.path.append("/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/yaml/lib")
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.ext import db
def auth_func():
return raw_input('Username:'), getpass.getpass('Password:')
if len(sys.argv) < 2:
print "Usage: %s app_id [host]" % (sys.argv[0],)
app_id = sys.argv[1]
if len(sys.argv) > 2:
host = sys.argv[2]
else:
host = '%s.appspot.com' % app_id
remote_api_stub.ConfigureRemoteDatastore(app_id, '/remote_api', auth_func, host)
code.interact('App Engine interactive console for %s' % (app_id,), None, locals())
Buat kelas dasar Mapper
Setelah berada di tempatnya, buat kelas Mapper ini. Saya baru saja membuat file baru bernama utils.py dan melemparkan ini:
class Mapper(object):
# Subclasses should replace this with a model class (eg, model.Person).
KIND = None
# Subclasses can replace this with a list of (property, value) tuples to filter by.
FILTERS = []
def map(self, entity):
"""Updates a single entity.
Implementers should return a tuple containing two iterables (to_update, to_delete).
"""
return ([], [])
def get_query(self):
"""Returns a query over the specified kind, with any appropriate filters applied."""
q = self.KIND.all()
for prop, value in self.FILTERS:
q.filter("%s =" % prop, value)
q.order("__key__")
return q
def run(self, batch_size=100):
"""Executes the map procedure over all matching entities."""
q = self.get_query()
entities = q.fetch(batch_size)
while entities:
to_put = []
to_delete = []
for entity in entities:
map_updates, map_deletes = self.map(entity)
to_put.extend(map_updates)
to_delete.extend(map_deletes)
if to_put:
db.put(to_put)
if to_delete:
db.delete(to_delete)
q = self.get_query()
q.filter("__key__ >", entities[-1].key())
entities = q.fetch(batch_size)
Mapper seharusnya hanya kelas abstrak yang memungkinkan Anda untuk mengulangi setiap entitas dari jenis tertentu, baik itu untuk mengekstrak datanya, atau untuk memodifikasinya dan menyimpan entitas yang diperbarui kembali ke datastore.
Jalankan dengan itu!
Sekarang, mulai konsol interaktif appengine Anda:
$python appengine_console.py <app_id_here>
Itu harus memulai konsol interaktif. Di dalamnya buat subclass Model:
from utils import Mapper
# import your model class here
class MyModelDeleter(Mapper):
KIND = <model_name_here>
def map(self, entity):
return ([], [entity])
Dan, akhirnya, jalankan (dari konsol interaktif Anda): mapper = MyModelDeleter () mapper.run ()
Itu dia!