panel_oracle.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. '''
  2. Created on 2016年8月18日
  3. @author: uas
  4. '''
  5. import os
  6. import time
  7. os.environ['NLS_LANG']='SIMPLIFIED CHINESE_CHINA.UTF8'
  8. from pymongo.mongo_client import MongoClient
  9. import csv
  10. import cx_Oracle
  11. from util_common import Constant
  12. class Panel_Oracle_Connector(object):
  13. def __init__(self):
  14. self.conn=cx_Oracle.connect(Constant.ORACLE_URL)
  15. self.cli=MongoClient(Constant.MONGODB_URL)
  16. self.db=self.cli.spider
  17. def _checkupdata(self):
  18. result=self.db.panel_propertyvalue_0728.find().limit(10)
  19. for r in result:
  20. for key,value in r.items():
  21. print('key:',key)
  22. print('value:',value)
  23. result=self.db.panel_propertyvalue_0728.find()
  24. print('component property number: ',result.count())
  25. # result=self.db.component_panel_0720.find().limit(1)
  26. # for r in result:
  27. # for key,value in r.items():
  28. # if key=='str_html':
  29. # continue
  30. # print('key:',key)
  31. # print('value:',value)
  32. #
  33. # result=self.db.component_panel_0720.find()
  34. # print('component number: ',result.count())
  35. #
  36. # result=self.db.component_panel_0720.find()
  37. # kind_set=set()
  38. # for index,r in enumerate(result):
  39. # kind_set.add(r['kind'])
  40. # print(index)
  41. # print(kind_set)
  42. #
  43. def _create_table(self):
  44. cursor=self.conn.cursor()
  45. # sql="create table product$panelcomponent"+"(cmp_id int primary key,cmp_attach varchar(255) ,cmp_img varchar(255),cmp_brid varchar2(222) ,cmp_code varchar(255),cmp_kiid varchar2(255),cmp_company varchar(255) ,cmp_company_url varchar(255) ,cmp_description varchar(4000),cmp_packaging varchar(255))"
  46. # cursor.execute(sql)
  47. sql="create table product$panel_propertyvalue"+"(pv_id int primary key,pv_componentid number(38,0) ,pv_propertyid varchar2(255),pv_value varchar2(255),pv_detno varchar2(255))"
  48. cursor.execute(sql)
  49. self.conn.commit()
  50. def _parse_data(self):
  51. result=self.db.panel_propertyvalue_0728.find({'property':'面板品牌'}).limit(1)
  52. component_list=list()
  53. for index,r in enumerate(result):
  54. d=dict()
  55. d['cmp_code']=r['cmp_code']
  56. d['cmp_attach']=''
  57. d['cmp_id']=r['cmp_id']+4674555
  58. d['cmp_img']=''
  59. d['cmp_brid']=r['value']
  60. d['cmp_kiid']=r['kind']
  61. d['cmp_company']=''
  62. d['cmp_company_url']=''
  63. d['cmp_description']=''
  64. d['cmp_packaging']=''
  65. component_list.append(d)
  66. print(component_list)
  67. return component_list
  68. def _insert_panel(self,panel_list):
  69. cursor=self.conn.cursor()
  70. sql="Insert into product$panelcomponent\
  71. (cmp_id,cmp_attach,cmp_img,cmp_brid,cmp_code,cmp_kiid,cmp_company,cmp_company_url,cmp_description,cmp_packaging)\
  72. values(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10)"
  73. cursor.prepare(sql)
  74. param_oracle=list()
  75. for index,component in enumerate(panel_list):
  76. try:
  77. param_oracle.append((component['cmp_id'],component['cmp_attach'],component['cmp_img'],component['cmp_brid'],component['cmp_code'],component['cmp_kiid'],component['cmp_company'],component['cmp_company_url'],component['cmp_description'],component['cmp_packaging']))
  78. except Exception as e:
  79. pass
  80. if index%20000==0:
  81. cursor.executemany(None,param_oracle)
  82. self.conn.commit()
  83. param_oracle.clear()
  84. cursor.executemany(None,param_oracle)
  85. self.conn.commit()
  86. cursor.close()
  87. def _insert_panel_parameter(self,panel_list):
  88. cursor=self.conn.cursor()
  89. sql="Insert into product$panel_propertyvalue (pv_id,pv_componentid,pv_propertyid,pv_value,pv_detno) values(:1,:2,:3,:4,:5)"
  90. cursor.prepare(sql)
  91. param_oracle=list()
  92. for index,component in enumerate(panel_list):
  93. try:
  94. param_oracle.append((component['pv_id,'],component['pv_componentid'],component['pv_propertyid'],component['pv_value'],component['pv_detno']))
  95. except Exception as e:
  96. pass
  97. if index%20000==0:
  98. cursor.executemany(None,param_oracle)
  99. self.conn.commit()
  100. param_oracle.clear()
  101. print(index)
  102. cursor.executemany(None,param_oracle)
  103. self.conn.commit()
  104. cursor.close()
  105. def _parse_parameter(self):
  106. result=self.db.panel_propertyvalue_0728.find().limit(1)
  107. component_list=list()
  108. total=51998620
  109. for index,r in enumerate(result):
  110. d=dict()
  111. print(r)
  112. d['pv_id']=total
  113. d['pv_componentid']=r['cmp_id']+4674555
  114. d['pv_propertyid']=r['property']
  115. d['pv_value']=r['value']
  116. d['pv_detno']=''
  117. component_list.append(d)
  118. total+=1
  119. print(component_list)
  120. return component_list
  121. if __name__=='__main__':
  122. connector=Panel_Oracle_Connector()
  123. connector._parse_parameter()
  124. # connector._checkupdata()
  125. # connector._create_table()
  126. # connector._parse_data()
  127. # connector._insert_panel(connector._parse_data())