Java兩表數(shù)據(jù)大數(shù)據(jù)量比對

使用FutureTask進行多線程數(shù)據(jù)讀取,減少讀取時間

public void checkCompany(){

?? ??? ?//獲取數(shù)據(jù)

?? ??? ?List<CompanyVo> omsList = getOmsList();

?? ??? ?//將要比對數(shù)據(jù)放入Map

?? ??? ?Map<Long, CompanyVo> omsMap = new HashMap<Long, CompanyVo>();

?? ??? ?for(CompanyVo vo:omsList){

?? ??? ??? ?omsMap.put(vo.getIdUuid(), vo);

?? ??? ?}

?? ??? ?//比較數(shù)據(jù)

?? ??? ?compareCompany(omsMap);

??? }

每2000條數(shù)據(jù)比對一次,MySQL數(shù)據(jù)庫

public void compareCompany(Map<Long, CompanyVo> omsMap){

//獲取總數(shù)據(jù)量

? ? int count = jdbcTemplate.queryForObject("select count(1) from oms_company", Integer.class);

? ? //分頁查詢

? ? ? int pages = count%2000= 0?(count/2000):(count/2000 + 1);

? ? ? ExecutorService executor = Executors.newFixedThreadPool(10);

? ? ? for(int i = 0; i < pages; i++){

? ? ? int startIndex = i*2000;

? ? ? ProTask task = new ProTask(startIndex, omsMap);

? ? ? FutureTask future = new FutureTask(task,null);

? ? ? executor.execute(future);

? ? ? }

? ? ? executor.shutdown();

? ? ? //判斷是否比對完成

? ? ? while(true){

? ? ? if(executor.isTerminated()){

? ? ? break;

? ? ? }

? ? ? try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

? ? ? }

? ? }

獲取要比較的總數(shù)據(jù),Oracle數(shù)據(jù)庫

public List<CompanyVo> getOmsList(){

? ? List<CompanyVo> list = new ArrayList<>();

? ? int count = jdbcTemplate.queryForObject("select count(1) from TB_PUBHA", Integer.class);

? ? int pages = count%2000 == 0?(count/2000):(count/2000 + 1);

? ? ExecutorService executor = Executors.newFixedThreadPool(10);

? ? List<FutureTask<List<CompanyVo>>> futureList = new ArrayList<>();

? ? for(int i = 0; i < pages; i++){

? ? int start = i*2000 + 1;

? ? int end = (i+1)*2000;

? ? if(end > count) end = count;

? ? OmsTask task = new OmsTask(start, end);

? ? FutureTask<List<CompanyVo>> future = new FutureTask<>(task);

? ? executor.submit(future);

? ? futureList.add(future);

? ? }

? ? executor.shutdown();

? ? for(FutureTask<List<CompanyVo>> future:futureList){

? ? try {

List<CompanyVo> listq = future.get();

if(null != listq && !listq.isEmpty())

list.addAll(listq);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ExecutionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

? ? }

? ? return list;

? ? }

OmsTask實現(xiàn)Callable接口,因為需要返回值

class OmsTask implements Callable<List<CompanyVo>>{

? ? private int start;


? ? private int end;


? ? public OmsTask(int start,int end){

? ? this.start = start;

? ? this.end = end;

? ? }

@Override

public List<CompanyVo> call() throws Exception {

String sql = "select * from (select rownum rm, PUBHA_ID id_uuid,UUID company_number,PUBHA003 company_name,PUBHA005 company_address " +

? ? ? ? ? ? ? ? ? ? ",PUBHA006 post_code,PUBHA008 corporation,PUBHA009 corporation_tel,PUBHA010 manager,PUBHA011 manager_tel,PUBHA012 contract,PUBHA013 contract_tel" +

? ? ? ? ? ? ? ? ? ? ",PUBHA015 web_url from TB_PUBHA ) where rm between "+start+" and "+end;

List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);

return CompanyVo.getCompany(mapList);

}


? ? }

ProTask實現(xiàn)Runnable接口,不需要返回結果,只進行比較

class ProTask implements Runnable{

? ? private int startIndex;

? ? private Map<Long, CompanyVo> omsMap;


? ? public ProTask(int startIndex,Map<Long, CompanyVo> omsMap){

? ? this.startIndex = startIndex;

? ? this.omsMap = omsMap;

? ? }

@Override

public void run() {

List<Map<String, Object>> listMap = jdbcTemplate.queryForList("select id_uuid,company_number,company_name,province_id,city_id,company_address,"

+ "post_code,corporation,corporation_tel,manager,manager_tel,contract,contract_tel,web_url "

+ "from oms_company limit "+startIndex+","+ 2000);

List<CompanyVo> list = CompanyVo.getCompany(listMap);

if(null != list && !list.isEmpty()){

List<Object[]> obs = new ArrayList<>();

for(CompanyVo vo:list){

CompanyVo oms = omsMap.get(vo.getIdUuid());

if(null == oms){

continue;

}

if((null == vo.getCompanyNumber() && null != oms.getCompanyNumber())

|| (null != vo.getCompanyNumber() && !vo.getCompanyNumber().equals(oms.getCompanyNumber()))){

//結果不同時進行處理

}

//對已比對數(shù)據(jù)進行移除

omsMap.remove(vo.getIdUuid());

}

}

}


? ? }

實體類,主要進行查詢結果轉化

package com.digital.domain;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import com.digital.util.StringUtil;

public class CompanyVo {

private Long idUuid;

private String companyNumber;

public Long getIdUuid() {

return idUuid;

}

public void setIdUuid(Long idUuid) {

this.idUuid = idUuid;

}

public String getCompanyNumber() {

return companyNumber;

}

public void setCompanyNumber(String companyNumber) {

this.companyNumber = companyNumber;

}

public static CompanyVo getCompany(Map<String, Object> map){

if(null == map) return null;

CompanyVo vo = new CompanyVo();

String idUuid = StringUtil.ObjectToString(map.get("ID_UUID"));

if(null != idUuid)

vo.setIdUuid(Long.parseLong(idUuid));

? ? ? ? vo.setCompanyNumber(StringUtil.ObjectToString(map.get("COMPANY_NUMBER")));


return vo;

}

public static List<CompanyVo> getCompany(List<Map<String, Object>> mapList){

if(null == mapList) return null;

List<CompanyVo> list = new ArrayList<>();

for(Map<String, Object> map:mapList){

CompanyVo vo = getCompany(map);

if(null != vo)

list.add(vo);

}

return list;

}

}

————————————————

版權聲明:本文為CSDN博主「yun0000000」的原創(chuàng)文章,遵循CC 4.0 by-sa版權協(xié)議,轉載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/yun0000000/article/details/52758098

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容