Created
May 4, 2013 01:48
-
-
Save FugiTech/5515688 to your computer and use it in GitHub Desktop.
Moves data from a SQL table to MongoDB
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 characters
from twisted.enterprise import adbapi | |
from twisted.internet import reactor, defer | |
import txmongo, sys, random | |
bottom = int(sys.argv[1]) if len(sys.argv) > 1 else 0 | |
mongo = txmongo.lazyMongoConnectionPool().DATABASE.COLLECTION | |
sql = adbapi.ConnectionPool(LIBRARY, host=HOST, port=PORT, db=DATABASE, user=USER, passwd=PASS, cp_reconnect=True) | |
@defer.inlineCallbacks | |
def main(): | |
global bottom, mongo, sql | |
result = yield sql.runQuery("SELECT MAX(id) FROM parts") | |
if not result: | |
print "Could not find max id" | |
return | |
top = result[0][0] | |
for start in range(bottom, top+1, 100): | |
rows = yield sql.runQuery("SELECT name, seed, answer, added FROM parts ORDER BY id ASC LIMIT %s,100", start) | |
documents = [] | |
for row in rows: | |
try: | |
row[1].decode("utf8") | |
row[2].decode("utf8") | |
except: | |
continue # If it isn't valid utf-8, who cares? | |
documents.append({ | |
"name": row[0], | |
"seed": row[1], | |
"answer": row[2], | |
"added": row[3], | |
"random": random.random() | |
}) | |
yield mongo.insert(documents, safe=True) | |
print "Completed {:d} of {:d} rows".format(start, top) | |
main() | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment