Python SQL with NoSQL
From DreamFactory
Tracyosborn (Talk | contribs) m (Added syntax highlighting) |
|||
Line 1: | Line 1: | ||
<source lang=python> | <source lang=python> | ||
# script db._table.contact.get.post_process | # script db._table.contact.get.post_process | ||
− | + | ||
# for each record in the MySQL db service, query the MongoDB service to get the Twitter handle for that contact | # for each record in the MySQL db service, query the MongoDB service to get the Twitter handle for that contact | ||
− | + | content = event.response.content; | |
− | + | ||
− | content | + | if content.resource != "": |
− | + | records = content.resource; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | records = content | + | |
for (i, record) in enumerate(records): | for (i, record) in enumerate(records): | ||
# filter by email | # filter by email | ||
− | filter = "filter=" + "email=" + record | + | filter = "filter=" + "email=" + record.email; |
− | + | ||
# get matching record from MongoDB service | # get matching record from MongoDB service | ||
− | + | result = platform.api.get('/mongodb/_table/contact?'+filter); | |
− | + | # convert json string -> dict -> bunch | |
− | + | data = bunchify(json.loads(result.read())); | |
# from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be | # from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be | ||
− | record | + | record.from_mongo_twitter = data.resource[0].twitter.encode('utf-8'); |
records[i] = record; | records[i] = record; | ||
− | |||
+ | event.response.content = records; | ||
# set this flag if you change the response content | # set this flag if you change the response content | ||
− | # you can also set event | + | # you can also set event.response.status_code and event.response.content_type |
− | event | + | event.response.content_changed = True; |
</source> | </source> |
Revision as of 06:28, 30 May 2016
# script db._table.contact.get.post_process # for each record in the MySQL db service, query the MongoDB service to get the Twitter handle for that contact content = event.response.content; if content.resource != "": records = content.resource; for (i, record) in enumerate(records): # filter by email filter = "filter=" + "email=" + record.email; # get matching record from MongoDB service result = platform.api.get('/mongodb/_table/contact?'+filter); # convert json string -> dict -> bunch data = bunchify(json.loads(result.read())); # from_mongo_twitter can be a field in MySQL schema, but it doesn't have to be record.from_mongo_twitter = data.resource[0].twitter.encode('utf-8'); records[i] = record; event.response.content = records; # set this flag if you change the response content # you can also set event.response.status_code and event.response.content_type event.response.content_changed = True;