Created
July 25, 2012 17:04
-
-
Save mk270/3177278 to your computer and use it in GitHub Desktop.
Shortcut to dump SQL query dictionaries to AMQP
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
#!/usr/bin/env python | |
import oursql | |
import pika | |
import json | |
import sys | |
db_config = {"db":"dbname", "user":"myuser", "passwd":"mypasswd"} | |
def dispatch(queue_name, query): | |
connection = pika.BlockingConnection( | |
pika.ConnectionParameters(host='localhost')) | |
channel = connection.channel() | |
channel.queue_declare(queue=queue_name, durable=True) | |
db = oursql.connect(**db_config) | |
c = db.cursor(oursql.DictCursor) | |
c.execute(query) | |
for row in c.fetchall(): | |
payload = json.dumps(row) | |
channel.basic_publish(exchange='', | |
routing_key=queue_name, | |
body=payload, | |
properties=pika.BasicProperties(delivery_mode=2)) | |
connection.close() | |
if __name__ == '__main__': | |
(_, queue_name, query) = sys.argv | |
dispatch(queue_name, query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment