Python connects Mongdb. Read. Parsing JSON data to MySQL

Source: Internet
Author: User

Tag:outer   ase   for    parsing JSON data    ble    end   exe   continue   bigdata   

#-*-Coding:utf-8-*-import sysfrom Pymongo import Mongoclientimport Pandas as Pdfrom sqlalchemy import Create_engineimpo RT mysqldbimport Jsonreload (SYS) sys.setdefaultencoding (' Utf-8 ') def find (Database,table_name): # Create Connection client = Mo                         Ngoclient (host= "127.0.0.1", port=3717, username= ' Dataro ' , password= "kZk671112", authsource= ' bigdata ', authmechanism= ' SC    Mam-she-8 ', replicaset= ' mgset-5057112 ') db = Client[database] collection = Db[table_name] # gets data Processresult = Collection.find () #定义字典, gets the content to parse Field_dict = {"id" : [], "credit_id": [], "source_id": [], "Req_url": [], "resp_time": [], "Bank_card ": []," Idcard ": []," mobile ": []," name ": []," result ": []," GUID ": []," Message ": []," sTatus ": []," credit_risk_level ": []," data_flag_id ": []," Data_flag_phone ": []} # Loop parsing f or J in Processresult:get_key = lambda x:j[x] if x in J.keys () Else "-" if Get_key ("applicant_mujin") = = " -": Continue req_param_list = Json.loads (Get_key (" Applicant_mujin "). Get (" Req_param ")) Response_li            st = Json.loads (Get_key ("Applicant_mujin"). Get ("response")) if "Req_param" in Get_key ("Applicant_mujin"): Applicant_list = response_list["Body" ["content"] ["applicant"] if "applicant" in response_list["body" ["Conte NT "]: field_dict[" id "].append (get_key (" _id ")) field_dict[" credit_id "].append (Get_key (" Credit  _id ")) field_dict[" source_id "].append (Get_key (" source_id ")) if Get_key (" applicant_mujin ") = =                "-": Continue field_dict["Req_url"].append (Get_key ("Applicant_mujin"). Get ("Req_url")) Field_dict["Resp_time"].append (Get_key ("Applicant_mujin"). Get ("Resp_time")) # Req_param_list Field_dic                t["Bank_card"].append (req_param_list["Bank_card"]) field_dict["Idcard"].append (req_param_list["IdCard"]) field_dict["mobile"].append (req_param_list["mobile"]) field_dict["name"].append (req_param_list                ["Name"]) # response_list field_dict["GUID"].append (response_list["GUID"]) field_dict["message"].appen D (response_list["message"]) field_dict["status"].append (response_list["status"]) field_dict[ "Result"].append (response_list["Body" ["Result"]) # applicant_list field_dict["credit_risk_l Evel "].append (applicant_list[" Credit_risk_level "]) field_dict[" data_flag_id "].append (applicant_list[" Data_f lag_id "]) field_dict[" Data_flag_phone "].append (applicant_list[" Data_flag_phone "]) dt = PD. DataFrame (DATA=FIELD_DICT) return dt# write Def to_mysql (dataframe): connect = Create_engine (' Mysql+mysqldb://root:[email protec                     Ted]@127.0.0.100:3306/data_outer?charset=utf8 ') pd.io.sql.to_sql (Dataframe, "APPLICANT_MJ"                     , Con=connect, schema= "Data_outer", if_exists= "append"                         ) # Delete def delete_data (): db = MySQLdb.connect (host= "127.0.0.100", user= ' root ' , passwd= ' [email protected] ', port=3306, db= ' Data_ou Ter ', charset= ' UTF8 ') cursor = Db.cursor () cursor.execute ("Delete    from DATA_OUTER.APPLICANT_MJ; ") Cursor.close () Db.commit () db.close () if __name__ = = ' __main__ ': print "Start" delete_data () print "Delete_d ATA End "Dataframe = Find (" Data_outer "," Outer_data ") To_mysql (dataframe) print" Dataframe End "priNT "Finish!!!" 

Python connects Mongdb. Read. Parsing JSON data to MySQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.