documentDumplicate.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. #coding:UTF8
  2. from odps.udf import annotate
  3. from odps.udf import BaseUDTF
  4. from odps.udf import BaseUDAF
  5. @annotate('string,string -> string,bigint,bigint,string,string,string,string,string,string,string,string,string,string,string,string,string,string,bigint')
  6. class f_decode_extract(BaseUDTF):
  7. def __init__(self):
  8. import logging
  9. import json
  10. import time,re
  11. global json,logging,time,re
  12. self.time_pattern = "\d{4}\-\d{2}\-\d{2}.*"
  13. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  14. def process(self, extractjson,otherjson):
  15. if extractjson is not None:
  16. _extract = json.loads(extractjson)
  17. else:
  18. _extract = {}
  19. if otherjson is not None:
  20. _other = json.loads(otherjson)
  21. else:
  22. _other = {}
  23. project_code = ""
  24. project_name = ""
  25. tenderee = ""
  26. agency = ""
  27. win_tenderer = ""
  28. bidding_budget = ""
  29. win_bid_price = ""
  30. page_time_stamp = 0
  31. docchannel = 0
  32. extract_count = 0
  33. page_time = _other.get("pageTime",time.strftime('%Y-%m-%d',time.localtime()))
  34. doctitle = _other.get("doctitle","")
  35. doctitle_refine = re.sub(r'工程|服务|询价|比价|谈判|竞争性|磋商|结果|中标|招标|采购|的|公示|公开|成交|公告|评标|候选人|交易|通知|废标|流标|终止|中止|一笔|预告|单一来源|询价|竞价', '', doctitle)
  36. area = _other.get("area","")
  37. province = _other.get("province","")
  38. city = _other.get("city","")
  39. district = _other.get("district","")
  40. web_source_no = _other.get("webSourceNo","")
  41. docchannel = _other.get("docchannel",0)
  42. if re.search(self.time_pattern,page_time) is not None:
  43. timeArray = time.strptime(page_time[:11], "%Y-%m-%d")
  44. page_time_stamp = int(time.mktime(timeArray))
  45. list_code = _extract.get("code",[])
  46. if len(list_code)>0:
  47. project_code = list_code[0]
  48. project_name = _extract.get("name","")
  49. dict_pack = _extract.get("prem",{})
  50. logging.info(dict_pack)
  51. for _key in dict_pack.keys():
  52. if dict_pack[_key]["tendereeMoney"]!='' and float(dict_pack[_key]["tendereeMoney"])>0:
  53. extract_count += 1
  54. if bidding_budget=="":
  55. bidding_budget = str(dict_pack[_key]["tendereeMoney"])
  56. for _role in dict_pack[_key]["roleList"]:
  57. extract_count += 1
  58. if _role[2]!='' and float(_role[2])>0:
  59. extract_count += 1
  60. if _role[0]=="tenderee":
  61. tenderee = _role[1]
  62. if _role[0]=="win_tenderer":
  63. if win_tenderer=="":
  64. win_tenderer = _role[1]
  65. if _role[2]!='' and float(_role[2])>0:
  66. if win_bid_price=="":
  67. win_bid_price = str(_role[2])
  68. if _role[0]=="agency":
  69. agency = _role[1]
  70. if project_code!="":
  71. extract_count += 1
  72. if project_name!="":
  73. extract_count += 1
  74. logging.info(page_time+doctitle+doctitle_refine+area+province+city+
  75. district+web_source_no+project_code+project_name+tenderee+agency+win_tenderer+bidding_budget+win_bid_price)
  76. self.forward(page_time,page_time_stamp,docchannel,doctitle,doctitle_refine,area,province,city,
  77. district,web_source_no,project_code,project_name,tenderee,agency,win_tenderer,bidding_budget,win_bid_price,extract_count)
  78. @annotate('string,string,string,string,string -> string,string,string,bigint')
  79. class f_decode_sub_docs_json(BaseUDTF):
  80. def __init__(self):
  81. import logging
  82. import json
  83. global json,logging
  84. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  85. def process(self, project_code,project_name,tenderee,agency,sub_docs_json):
  86. columns = {"win_tenderer":"","bidding_budget":"","win_bid_price":""}
  87. extract_count = 0
  88. if project_code!="":
  89. extract_count += 1
  90. if project_name!="":
  91. extract_count += 1
  92. if tenderee!="":
  93. extract_count += 1
  94. if agency!="":
  95. extract_count += 1
  96. if sub_docs_json is not None:
  97. for sub_docs in json.loads(sub_docs_json):
  98. for _key_sub_docs in sub_docs.keys():
  99. extract_count += 1
  100. if _key_sub_docs in columns:
  101. if columns[_key_sub_docs]=="" and str(sub_docs[_key_sub_docs]) not in ["","0"]:
  102. if _key_sub_docs in ["bidding_budget","win_bid_price"]:
  103. if float(sub_docs[_key_sub_docs])>0:
  104. columns[_key_sub_docs] = str(sub_docs[_key_sub_docs])
  105. else:
  106. columns[_key_sub_docs] = str(sub_docs[_key_sub_docs])
  107. self.forward(columns["win_tenderer"],columns["bidding_budget"],columns["win_bid_price"],extract_count)
  108. @annotate("string->bigint")
  109. class totimestamp(object):
  110. def __init__(self):
  111. import time
  112. global time
  113. import logging
  114. import json
  115. import re
  116. global json,logging,re
  117. self.time_pattern = "\d{4}\-\d{2}\-\d{2}.*"
  118. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  119. def evaluate(self, str_time):
  120. try:
  121. logging.info(str_time)
  122. if str_time is not None and re.search(self.time_pattern,str_time) is not None:
  123. timeArray = time.strptime(str_time[:10], "%Y-%m-%d")
  124. timeStamp = int(time.mktime(timeArray))
  125. return timeStamp
  126. else:
  127. return 0
  128. except Exception as e:
  129. return 0
  130. @annotate("string->string")
  131. class refind_name(object):
  132. def __init__(self):
  133. import logging
  134. import re
  135. global logging,re
  136. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  137. def evaluate(self, title):
  138. if title is not None:
  139. return re.sub(r'工程|服务|询价|比价|谈判|竞争性|磋商|结果|中标|招标|采购|的|公示|公开|成交|公告|评标|候选人|交易|通知|废标|流标|终止|中止|一笔|预告|单一来源|询价|竞价', '', title)
  140. return ""
  141. @annotate('bigint,bigint,bigint,string,bigint,string->string')
  142. class f_set_docid(BaseUDAF):
  143. '''
  144. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  145. '''
  146. def __init__(self):
  147. import json
  148. global json
  149. def new_buffer(self):
  150. return [[]]
  151. def iterate(self, buffer,docid, page_time_stamp,extract_count,defind_column,defind_count,tenderee):
  152. buffer[0].append({"docid":docid,"page_time_stamp":page_time_stamp,"extract_count":extract_count,
  153. "defind_column":defind_column,"defind_count":defind_count,"tenderee":tenderee})
  154. def merge(self, buffer, pbuffer):
  155. buffer[0].extend(pbuffer[0])
  156. def terminate(self, buffer):
  157. list_docs = buffer[0]
  158. list_docs.sort(key=lambda x:x["page_time_stamp"])
  159. list_group = []
  160. _begin = 0
  161. defind_count = 0
  162. if len(list_docs)>0:
  163. defind_count = list_docs[0]["defind_count"]
  164. for i in range(len(list_docs)-1):
  165. if abs(list_docs[i]["page_time_stamp"]-list_docs[i+1]["page_time_stamp"])<=86400*2:
  166. continue
  167. else:
  168. _group = []
  169. _set_column = set()
  170. _set_tenderee = set()
  171. for j in range(_begin,i+1):
  172. _set_tenderee.add(list_docs[j]["tenderee"])
  173. _set_column.add(list_docs[j]["defind_column"])
  174. _group.append({"docid":list_docs[j]["docid"],"extract_count":list_docs[j]["extract_count"]})
  175. if len(_group)>=3 and len(_set_tenderee)>1:
  176. pass
  177. else:
  178. if len(_group)>1:
  179. if defind_count==2:
  180. if len(_set_column)>=2:
  181. list_group.append(_group)
  182. elif defind_count==1:
  183. if len(_set_column)==1:
  184. list_group.append(_group)
  185. elif defind_count==0:
  186. list_group.append(_group)
  187. _begin = i+1
  188. if len(list_docs)>1:
  189. _set_column = set()
  190. _set_tenderee = set()
  191. _group = []
  192. for j in range(_begin,len(list_docs)):
  193. if list_docs[j]["tenderee"] is not None and list_docs[j]["tenderee"]!="":
  194. _set_tenderee.add(list_docs[j]["tenderee"])
  195. _set_column.add(list_docs[j]["defind_column"])
  196. _group.append({"docid":list_docs[j]["docid"],"extract_count":list_docs[j]["extract_count"]})
  197. if len(_group)>=3 and len(_set_tenderee)>1:
  198. pass
  199. else:
  200. if len(_group)>1:
  201. if defind_count==2:
  202. if len(_set_column)>=2:
  203. list_group.append(_group)
  204. elif defind_count==1:
  205. if len(_set_column)==1:
  206. list_group.append(_group)
  207. elif defind_count==0:
  208. list_group.append(_group)
  209. return json.dumps(list_group)
  210. @annotate('bigint->string')
  211. class f_stamp_squence(BaseUDAF):
  212. '''
  213. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  214. '''
  215. def __init__(self):
  216. import json
  217. global json
  218. import logging
  219. global logging
  220. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  221. def new_buffer(self):
  222. return [set()]
  223. def iterate(self, buffer,page_time_stamp):
  224. buffer[0].add(page_time_stamp)
  225. def merge(self, buffer, pbuffer):
  226. buffer[0] |= pbuffer[0]
  227. def terminate(self, buffer):
  228. if 0 in buffer[0]:
  229. buffer[0].remove(0)
  230. list_stamp = list(buffer[0])
  231. list_stamp.sort(key=lambda x:x)
  232. list_stamp_final = []
  233. _begin = 0
  234. _time_decase = 86400*2
  235. logging.info(str(list_stamp))
  236. for _index in range(len(list_stamp)-1):
  237. if list_stamp[_index+1]-list_stamp[_index]<_time_decase:
  238. continue
  239. else:
  240. list_stamp_final.append([list_stamp[_begin]-_time_decase,list_stamp[_index]+_time_decase])
  241. _begin = _index+1
  242. if len(list_stamp)>0:
  243. list_stamp_final.append([list_stamp[_begin]-_time_decase,list_stamp[-1]+_time_decase])
  244. return json.dumps(list_stamp_final)
  245. @annotate("bigint,string->bigint")
  246. class in_stamp(object):
  247. def __init__(self):
  248. import logging
  249. import re
  250. import json
  251. global logging,re,json
  252. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  253. def evaluate(self, page_time_stamp,json_stamp):
  254. list_stamp = json.loads(json_stamp)
  255. int_flag = 0
  256. for item in list_stamp:
  257. if page_time_stamp <item[0]:
  258. break
  259. if page_time_stamp>item[0] and page_time_stamp<item[1]:
  260. int_flag = 1
  261. break
  262. return int_flag
  263. @annotate('string -> bigint,bigint,bigint,bigint')
  264. class f_split_group_single(BaseUDTF):
  265. '''
  266. 将多个组拆解成多条记录
  267. '''
  268. def __init__(self):
  269. import logging
  270. import json
  271. global json,logging
  272. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  273. def process(self, json_set_docid):
  274. list_group = json.loads(json_set_docid)
  275. for item in list_group:
  276. for index_i in range(len(item)):
  277. for index_j in range(len(item)):
  278. if index_i!=index_j and item[index_i]["docid"]!=item[index_j]["docid"]:
  279. self.forward(item[index_i]["docid"],item[index_j]["docid"],item[index_i]["extract_count"],item[index_j]["extract_count"])
  280. @annotate('bigint,string->string')
  281. class group_document(BaseUDAF):
  282. '''
  283. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  284. '''
  285. def __init__(self):
  286. import json
  287. global json
  288. def new_buffer(self):
  289. return [[]]
  290. def iterate(self, buffer,id,json_set_docid):
  291. buffer[0].append({"id":id,"json_set_docid":json.loads(json_set_docid)})
  292. def merge(self, buffer, pbuffer):
  293. buffer[0].extend(pbuffer[0])
  294. def terminate(self, buffer):
  295. return json.dumps(buffer[0])
  296. @annotate('bigint,string,bigint,string -> bigint,bigint,string')
  297. class decare_document(BaseUDTF):
  298. '''
  299. 将多个组拆解成多条记录
  300. '''
  301. def __init__(self):
  302. import logging
  303. import json
  304. global json,logging
  305. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  306. def process(self,group_id1, json_list_doc1,group_id2,json_list_doc2):
  307. #y=x,少掉近一半的数据
  308. if group_id1>=group_id2:
  309. list_doc1 = json.loads(json_list_doc1)
  310. list_doc2 = json.loads(json_list_doc2)
  311. for _doc1 in list_doc1:
  312. for _doc2 in list_doc2:
  313. #同一个重复group不做判断
  314. if _doc1["id"]!=_doc2["id"]:
  315. #判断两个group是否有重复
  316. _set1 = set()
  317. for _item1 in _doc1["json_set_docid"]:
  318. _set1.add(_item1["docid"])
  319. _set2 = set()
  320. for _item2 in _doc2["json_set_docid"]:
  321. _set2.add(_item2["docid"])
  322. if len(_set1&_set2)>0:
  323. new_json_set_docid = _doc1["json_set_docid"]
  324. for _item2 in _doc2["json_set_docid"]:
  325. if _item2["docid"] not in _set1:
  326. new_json_set_docid.append(_item2)
  327. self.forward(_doc1["id"],_doc2["id"],json.dumps(new_json_set_docid))
  328. def getBestDocid(list_pair):
  329. list_pair.sort(key=lambda x:x[3],reverse=True)
  330. _max_count = max(list_pair[0][3],list_pair[0][1])
  331. set_candidate = set()
  332. if list_pair[0][1]==_max_count:
  333. set_candidate.add(list_pair[0][0])
  334. for item in list_pair:
  335. if item[3]==_max_count:
  336. set_candidate.add(item[2])
  337. else:
  338. break
  339. list_candidate = list(set_candidate)
  340. list_candidate.sort(key=lambda x:x)
  341. return list_candidate[0]
  342. @annotate('bigint,bigint,bigint,bigint->string')
  343. class choose_document(BaseUDAF):
  344. '''
  345. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  346. '''
  347. def __init__(self):
  348. import json
  349. global json
  350. def new_buffer(self):
  351. return [[]]
  352. def iterate(self, buffer,docid1,extract_count1,docid2,extract_count2):
  353. buffer[0].append([docid1,extract_count1,docid2,extract_count2])
  354. def merge(self, buffer, pbuffer):
  355. buffer[0].extend(pbuffer[0])
  356. def terminate(self, buffer):
  357. list_pair = buffer[0]
  358. _set = set()
  359. for item in buffer[0]:
  360. _set.add(str(item[2]))
  361. list_dumplicate = list(_set)
  362. best_docid = getBestDocid(list_pair)
  363. if best_docid==list_pair[0][0]:
  364. save_flag = 1
  365. else:
  366. save_flag = 0
  367. return json.dumps({"save_flag":save_flag,"dumplicates":list_dumplicate})
  368. @annotate('string -> bigint,string')
  369. class f_get_choose_document(BaseUDTF):
  370. '''
  371. 将多个组拆解成多条记录
  372. '''
  373. def __init__(self):
  374. import logging
  375. import json
  376. global json,logging
  377. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  378. def process(self,json_choose):
  379. if json_choose is None:
  380. self.forward(1,None)
  381. else:
  382. _choose = json.loads(json_choose)
  383. self.forward(_choose["save_flag"],",".join(_choose["dumplicates"]))
  384. @annotate('bigint,bigint,bigint,bigint->string')
  385. class group_document_bestFirst(BaseUDAF):
  386. '''
  387. 将组里面最优的放在前面
  388. '''
  389. def __init__(self):
  390. import json
  391. global json
  392. def new_buffer(self):
  393. return [[]]
  394. def iterate(self, buffer,docid1,extract_count1,docid2,extract_count2):
  395. buffer[0].append([docid1,extract_count1,docid2,extract_count2])
  396. def merge(self, buffer, pbuffer):
  397. buffer[0].extend(pbuffer[0])
  398. def terminate(self, buffer):
  399. list_pair = buffer[0]
  400. _set = set()
  401. for item in buffer[0]:
  402. _set.add(item[2])
  403. _set.add(list_pair[0][0])
  404. best_docid = getBestDocid(list_pair)
  405. _set.remove(best_docid)
  406. list_dumplicate = list(_set)
  407. list_dumplicate.sort(key=lambda x:x)
  408. list_dumplicate.insert(0,best_docid)
  409. list_dumplicate_str = []
  410. for item in list_dumplicate:
  411. list_dumplicate_str.append(str(item))
  412. return ",".join(list_dumplicate_str)
  413. @annotate('string -> bigint,string')
  414. class f_get_best_dumplicates(BaseUDTF):
  415. '''
  416. 得到每个分组中最优的那一条及其重复记录
  417. '''
  418. def __init__(self):
  419. import logging
  420. import json
  421. global json,logging
  422. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  423. def process(self,list_dumplicate_str):
  424. if list_dumplicate_str is None:
  425. pass
  426. else:
  427. list_dumplicate = list_dumplicate_str.split(",")
  428. if len(list_dumplicate)>0:
  429. self.forward(int(list_dumplicate[0]),",".join(list_dumplicate[1:]))
  430. else:
  431. pass
  432. @annotate('bigint,bigint->string')
  433. class bridge2group(BaseUDAF):
  434. '''
  435. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  436. '''
  437. def __init__(self):
  438. import json
  439. global json
  440. def new_buffer(self):
  441. return [set()]
  442. def iterate(self, buffer,docid1,docid2):
  443. buffer[0].add(docid1)
  444. buffer[0].add(docid2)
  445. def merge(self, buffer, pbuffer):
  446. buffer[0] |= pbuffer[0]
  447. def terminate(self, buffer):
  448. list_pair = list(buffer[0])
  449. list_pair.sort(key=lambda x:x,reverse=True)
  450. return json.dumps(list_pair)
  451. @annotate('string -> bigint,bigint')
  452. class group2bridge(BaseUDTF):
  453. '''
  454. 将多个组拆解成多条记录
  455. '''
  456. def __init__(self):
  457. import logging
  458. import json
  459. global json,logging
  460. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  461. def process(self,json_list_docid):
  462. list_docid = json.loads(json_list_docid)
  463. for _docid in list_docid:
  464. self.forward(list_docid[-1],_docid)
  465. @annotate('bigint,bigint,string -> bigint')
  466. class f_get_dump_docid(BaseUDTF):
  467. '''
  468. 将多个组拆解成多条记录
  469. '''
  470. def __init__(self):
  471. import logging
  472. import json
  473. global json,logging
  474. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  475. def process(self,docid,save_flag,dumplicates):
  476. if save_flag==0:
  477. self.forward(docid)
  478. if dumplicates is not None:
  479. list_docid = dumplicates.split(",")
  480. if len(list_docid)>0:
  481. for _docid in list_docid[1:]:
  482. self.forward(int(_docid))
  483. else:
  484. if dumplicates is not None:
  485. list_docid = dumplicates.split(",")
  486. if len(list_docid)>0:
  487. for _docid in list_docid:
  488. self.forward(int(_docid))
  489. @annotate('string -> bigint,bigint')
  490. class f_get_docid(BaseUDTF):
  491. '''
  492. 将多个组拆解成多条记录
  493. '''
  494. def __init__(self):
  495. import logging
  496. import json
  497. global json,logging
  498. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  499. def process(self,json_set_docid):
  500. team_id = 0
  501. if json_set_docid is not None:
  502. list_docses = json.loads(json_set_docid)
  503. for list_docs in list_docses:
  504. team_id += 1
  505. for item in list_docs:
  506. self.forward(team_id,item["docid"])
  507. @annotate("string->bigint")
  508. class get_count_dump(object):
  509. def __init__(self):
  510. import logging
  511. import re
  512. global logging,re
  513. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  514. def evaluate(self, title):
  515. _count = 0
  516. if title is not None:
  517. _count = len(title.split(","))
  518. return _count
  519. def getSet(list_dict,key):
  520. _set = set()
  521. for item in list_dict:
  522. if key in item:
  523. if item[key]!='' and item[key] is not None:
  524. if re.search("^\d[\d\.]*$",item[key]) is not None:
  525. _set.add(str(float(item[key])))
  526. else:
  527. _set.add(str(item[key]))
  528. return _set
  529. @annotate('bigint,string -> bigint,bigint')
  530. class f_getGroup_dumpFinal(BaseUDTF):
  531. '''
  532. 从最后的结果中获取组
  533. '''
  534. def __init__(self):
  535. import logging
  536. import json
  537. global json,logging
  538. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  539. def process(self,docid,dumplicates):
  540. self.forward(int(docid),int(docid))
  541. if dumplicates is not None:
  542. list_docids = dumplicates.split(",")
  543. for _docid in list_docids:
  544. self.forward(int(docid),int(_docid))
  545. @annotate('bigint,bigint,string,string,string,string,bigint,bigint->string')
  546. class f_redump_limit_num(BaseUDAF):
  547. '''
  548. 去重合并后重新判断,组内个数大于5时,dottitle、tenderee、win_tenderer、bidding_budget组内只能有一个取值
  549. 组内个数小于等于5时,tenderee、win_tenderer、bidding_budget组内只能有一个取值
  550. '''
  551. def __init__(self):
  552. import logging
  553. import json,re
  554. global json,logging,re
  555. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  556. def new_buffer(self):
  557. return [list()]
  558. def iterate(self, buffer,main_docid,docid,set_limit_column1,set_limit_column2,set_limit_column3,set_limit_column4,extract_count1,extract_count2):
  559. buffer[0].append({"main_docid":main_docid,"docid":docid,"set_limit_column1":set_limit_column1,"set_limit_column2":set_limit_column2,
  560. "set_limit_column3":set_limit_column3,"set_limit_column4":set_limit_column4,"extract_count1":extract_count1,"extract_count2":extract_count2})
  561. def merge(self, buffer, pbuffer):
  562. buffer[0].extend(pbuffer[0])
  563. def terminate(self, buffer):
  564. list_group = []
  565. the_group = buffer[0]
  566. if len(the_group)>5:
  567. keys = ["set_limit_column1","set_limit_column2","set_limit_column3","set_limit_column4"]
  568. else:
  569. keys = ["set_limit_column2","set_limit_column3","set_limit_column4"]
  570. stay = True
  571. for _key in keys:
  572. if len(getSet(the_group,_key))>1:
  573. stay = False
  574. break
  575. final_group = []
  576. if stay:
  577. main_docid = the_group[0]["main_docid"]
  578. for item in the_group:
  579. if item["docid"]!=main_docid:
  580. final_group.append({"docid1":main_docid,"docid2":item["docid"],"extract_count1":item["extract_count1"],"extract_count2":item["extract_count2"]})
  581. return json.dumps(final_group)
  582. @annotate('string -> bigint,bigint,bigint,bigint')
  583. class f_get_dumpFinal_checked(BaseUDTF):
  584. '''
  585. 从最后的结果中获取组
  586. '''
  587. def __init__(self):
  588. import logging
  589. import json
  590. global json,logging
  591. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  592. def process(self,list_group):
  593. if list_group is not None:
  594. final_group = json.loads(list_group)
  595. for _group in final_group:
  596. self.forward(_group["docid1"],_group["docid2"],_group["extract_count1"],_group["extract_count2"])