python指令碼 mongodb到postgresql,mongodbpostgresql

來源:互聯網
上載者:User

python指令碼 mongodb到postgresql,mongodbpostgresql

安裝 mongo模組

pip install pymongo

安裝postgresql 驅動

yum install python-psycopg2 

 

 1 # -*- coding: utf-8 -*- 2

 3 from pymongo import MongoClient 4 import datetime 5 import psycopg2 6 7 def get_db(): 8 print('connect mongo...') 9 client=MongoClient('localhost',27017)10 db=client.webdb11 db.authenticate('jackical','****')12 return db13 14 def get_collection():15 print('start read table..')16 db=get_db()17 coll=db['table']18 print(db.collection_names())19 return coll.find({'pid':12,'did':45})20 21 def get_pg():22 print('read pg db')23 conn=psycopg2.connect(database='jackical',user='postgres',password='******',host='localhost',port='5432')24 cur=conn.cursor()25 colles=get_collection()26 pernumb=027 for i in colles:
28 searchKey=i['searchKey'] if 'searchKey' in i else '' 29 websiteType=i['websiteType'] if 'websiteType' in i else ''30 31 searchKey=searchKey.replace('\'','*_*')32 websiteType=websiteType.replace('\'','*_*')33 34 persql='insert into public.myapp_res2(searchkey,websitetype) values(\'%s\',\'%s\')' % (searchKey,websiteType)35 print(persql)36 cur.execute(persql)37 38 pernumb=pernumb+139 40 if pernumb>1000:41 conn.commit()42 pernumb=043 44 if pernumb>0:45 conn.commit()46 cur.close()47 conn.close()48 print('insert end')49 50 51 if __name__=='__main__':52 get_pg()53 54 55 56 57 58 59 60

 

代碼中

i['searchKey'] if 'searchKey' in i else '' 

為mongo中有沒有 "searchKey" 這列,有的話就顯示,沒有的話就為 ''

為保證入庫正常,所以直接將 ‘ 單引號替換成 *_* ,取資料時,再換回來。

 

 

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.