Mar-23-2018, 12:13 PM
(This post was last modified: Mar-23-2018, 12:18 PM by chevanton1988.)
Hi gurus,
I need to take data from JSON API rest and put it into MySQL database table. I try this with for loop through json list which contains dictionaries for data for transaction but I want it to do width map and lambda function. Loop take some time and this job will run every minutes and I need to take data and load quickly. also I want to put this in asynchronise mode and run the same process for different api rest in the same time. Because that I want to put this logic in map and lambda sytnax, not through for loop syntax.
I need to take data from JSON API rest and put it into MySQL database table. I try this with for loop through json list which contains dictionaries for data for transaction but I want it to do width map and lambda function. Loop take some time and this job will run every minutes and I need to take data and load quickly. also I want to put this in asynchronise mode and run the same process for different api rest in the same time. Because that I want to put this logic in map and lambda sytnax, not through for loop syntax.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import mysql.connector as mysql json_list_of_transactions = [{ 'amount' : '0.05900000' , 'date' : '1521806276' , 'tid' : '59790535' , 'type' : '0' , 'price' : '8509.96' }, { 'amount' : '0.04400000' , 'date' : '1521806270' , 'tid' : '59790528' , 'type' : '1' , 'price' : '8512.08' }, { 'amount' : '0.03730108' , 'date' : '1521806268' , 'tid' : '59790527' , 'type' : '1' , 'price' : '8514.21' }] db = mysql.connect(user = 'root' , password = 'mysql#1234' , host = 'localhost' , database = 'DB_DEV' ) cursor = db.cursor() cursor.execute( "TRUNCATE TABLE BTCUSD_TXN_H;" ) db.commit() transaction_sql = ( "insert into BTCUSD_TXN_H" "(DATE_TIMESTAMP, DATE_ID, TID, TYPE, VOLUMEN, PRICE)" "values (%(date)s, from_unixtime(%(date)s), %(tid)s, %(type)s, %(amount)s, %(price)s)" ) for transaction in json_list_of_transactions: cursor.execute(transaction_sql, transaction) #this for loop I want change and try with lambda and map expression like this ## (it works without errors but not load data. Why? What is wrong? #map(lambda transaction: cursor.execute(transaction_sql, transaction), json_list_of_transactions ) db.commit() cursor.close() db.close() |