一次针对批量查询处理的优化

 客户调用批量查询接口对Solr核进行查询时以为查询响应时间有些慢,接口的内部实现目前是顺序执行每一个查询,再把结果汇总起来返回给调用方。所以,考虑引入线程池对查询接口的内部实现进行重构优化。java

       先声明一个大小可随之增加的线程池,
 
  
  
  
  
  1. private ExecutorService executor = Executors.newCachedThreadPool();//查询请求处理线程池 

而后是主线程方法的代码: public Listsql

 

  
  
  
  
  1. List<Map<String, String>> finalResult = null
  2.        if (idList == null || idList.size() == 0 || StringUtil.isBlank(entityCode)) {//参数合法性校验 
  3.            return finalResult; 
  4.        } 
  5.        finalResult = new ArrayList<Map<String, String>>(); 
  6.         
  7.        List<Future<Map<String, String>>> futureList = new ArrayList<Future<Map<String, String>>>(); 
  8.        int threadNum = idList.size();//查询子线程数目 
  9.        for (int i = 0; i < threadNum; i++) { 
  10.            Long itemId = idList.get(i); 
  11.            Future<Map<String, String>> future = executor.submit(new QueryCallable (entityCode, itemId)); 
  12.            futureList.add(future); 
  13.        } 
  14.        for(Future<Map<String, String>> future : futureList) { 
  15.            Map<String, String> threadResult = null
  16.            try { 
  17.                threadResult = future.get(); 
  18.            } catch (Exception e) {  
  19.                threadResult = null
  20.            } 
  21.            if (null != threadResult && threadResult.size() > 0) {//结果集不为空 
  22.                finalResult.add(threadResult); 
  23.            } 
  24.        } 
  25.        return finalResult; 
  26.    } 

最后是具体负责处理每一个查询请求的Callableapache

 

  
  
  
  
  1. public class QueryCallable implements Callable<Map<String, String>> { 
  2.         private String entityCode = ""
  3.         private Long itemId = 0L; 
  4.          
  5.         public GetEntityListCallable(String entityCode, Long itemId) { 
  6.             this. entityCode = entityCode; 
  7.             this.itemId = itemId; 
  8.         } 
  9.         public Map<String, String> call() throws Exception { 
  10.             Map<String, String> entityMap = null
  11.             try { 
  12.                 entityMap = QueryServiceImpl.this.getEntity(entityCode, itemId);//先去hbase查基本信息 
  13.  
  14.             } catch (Exception e) { 
  15.                 entityMap = null
  16.             } 
  17.             return entityMap; 
  18.         } 
  19.     } 

经过线程池的使用,能够减小建立,销毁进程所带来的系统开销,并且线程池中的工做线程能够重复使用,极大地利用现有系统资源,增长了系统吞吐量。app

另外,今天也尝试了另外一种合并Solr索引的方法,直接经过底层的Lucene的API进行,而不是提交Http请求,具体方法以下:ide

java -cp lucene-core-3.4.0.jar:lucene-misc-3.4.0.jar org/apache/lucene/misc/IndexMergeTool ./newindex ./app1/solr/data/index ./app2/solr/data/index 优化

相关文章
相关标签/搜索