GAE/Pyでbulkloaderを使ってデータをアップロードする - すぎゃーんメモ
GAE/Pyでbulkloaderを使ってデータをダウンロード/削除する - すぎゃーんメモ
ã®ç¶ãã
ããããDatastoreããfetchãã¦ããæç¹ã§(ãã®å¦çã¯ãã«ãã¹ã¬ããã§è¡ããã¦ããã£ã½ã)åé¤å¦çãããã¦ãããã°ããã®ã ãã©ãæ®å¿µãªãããã¡ãã§ã«ã¹ã¿ãã¤ãºã§ããã®ã¯Exporterã¯ã©ã¹ã«ã¤ãã¦ã ãã®ããã§ãæ®å¿µãªããããã¤ã¯ãã¹ã¦ã®ãã¼ã¿ããã¦ã³ãã¼ãããå¾ã«çæãããgeneratorããæ±ããã¨ãã§ããªãã
ã¨ããåé¡ããã£ã¦ãbulkloaderã使ç¨ãã¦æå®ããkindã®ã¨ã³ãã£ãã£ãå¹çããå
¨åé¤ãããã¨ã¯åºæ¥ãªãã£ãã
ãããªãã¨bulkloaderã使ããã«èªåã§Threadä½ã£ãããã¦ããããã§remote_apiãå©ãã¦ããããã«ãããã¨ãããã¨ããæãã¤ããªãã£ãã®ã ãã©ããã£ããbulkloaderãå¹çããremote_apiããã¨ã³ãã£ãã£ãå¼ã£å¼µã£ã¦ãããã¨ãã§ããã®ã ããããããå©ç¨ããªãæã¯ãªããã¨æã軽ãhackãã¦ã¿ãã
#!/usr/bin/env python # -*- coding: utf-8 -*- import getopt import sys sys.path.extend([ '/usr/local/google_appengine/', '/usr/local/google_appengine/lib/antlr3/', ]) from google.appengine.ext import db from google.appengine.tools import appcfg from google.appengine.tools import bulkloader from google.appengine.tools.bulkloader import ( BulkExporterThread, CheckOutputFile, Exporter) # CheckOutputFile ç¡å¹å CheckOutputFile.func_code = (lambda x: x).func_code # Exporterã¯ä¸è¦ãªã®ã§Dummyã®ã¤ã³ã¹ã¿ã³ã¹ãè¿ãããã«ãã def DummyExporter(kind): class dummy: def initialize(self, filename, exporter_opts): pass def finalize(self): pass def output_entities(self, entity_generator): pass return dummy() Exporter.RegisteredExporter.func_code = DummyExporter.func_code # åé¤å¦ç(BulkExporterThreadã®TransferItemã¡ã½ããã横åããã) def delete(self, item): retval = self.request_manager.GetEntities(item) # åå¾ãã¦ããentitiesãå ¨åé¤ db.delete(retval.keys) retval.entities = [] retval.keys = [] return retval BulkExporterThread.TransferItem.im_func.func_code = delete.func_code if __name__ == '__main__': # å¼æ°, ãªãã·ã§ã³ã追å sys.argv.insert(1, 'download_data') sys.argv.insert(2, '--filename=') sys.argv.insert(3, '--config_file=' + sys.argv[0]) # å¼æ°ã§æå®ããkindåã§Modelã¯ã©ã¹ãå®ç¾© opts, unused_args = getopt.getopt(sys.argv[2:], None, bulkloader.FLAG_SPEC) kind = [x[1] for x in opts if x[0] == '--kind'][0] exec "class %s(db.Model): pass" % kind appcfg.main(sys.argv)
ãããã対象ã¨ããkindåã¨ã¢ããªã±ã¼ã·ã§ã³ã®ãã£ã¬ã¯ããªãæå®ãã¦å®è¡ããã ãã
$ ./bulkdeleter.py --kind=HogeFugaPiyo ../application/
bulkloaderã§ä½¿ãä»ã®ãªãã·ã§ã³ãæå®ãããã¨ãã§ãããã¯ãã
$ ./bulkdeleter.py --kind=HogeFugaPiyo --num_threads=50 --batch_size=50 ../application/
åºæ¬çã«"appcfg.py download_data"ã¨åãæä½ãããããã®ä¸ã§Datastoreããfetchãã¦ããentitiesãExporterã§æ¸ãåºã代ããã«å
¨åé¤ãã¦ããã ãã
å¤æ´ç®æãæä½éã«æããããã«ãã³ãã¤ã³ãã§ã¡ã½ããã®å®è£
ãå
¥ãæ¿ãã¦ããã
関数の呼び出し時の処理を入れ替える - すぎゃーんメモ
ããã使ããã¨ã§ãã¢ãããã¼ãã¨ããå ´åã¨ã»ã¼åãé度ã§å
¨åé¤ãã§ããã
ãã ãããã«ãã¹ã¬ãããªä¸¦åå¦çãè¡ãããã«ã¯å¯¾è±¡ã¨ãªãkindã®__key__ã¤ã³ããã¯ã¹ãä½æããã¦ããå¿
è¦ãããã
ããããªãã¦ãå
¨åé¤ã¯ã§ãããã並åã§ã¯ãªãé 次å¦çã§ããåé¤ã§ããªãã
å¾ã£ã¦ã並åå¦çããããããã«ã¯ãåé¤ãè¡ãåã«index.yamlã§å¯¾è±¡ã¨ãªãkindã«å¯¾ãã¦ä»¥ä¸ã®ããã«indexãæå®ãã¦ãã¤ã³ããã¯ã¹ãä½æãã¦ãããããã¯Exporterã使ããã¦ã³ãã¼ãå¦çã§ãåããã¨ãè¨ããã
- kind: HogeFugaPiyo properties: - name: __key__ direction: desc