一次针对批量查询处理的优化
客户调用批量查询接口对Solr核进行查询时觉得查询响应时间有些慢,接口的内部实现目前是顺序执行每个查询,再把结果汇总起来返回给调用方。因此,考虑引入线程池对查询接口的内部实现进行重构优化。
先声明一个大小可随之增长的线程池,
然后是主线程方法的代码:
public List<Map<String, String>> queryEntityList(String entityCode, List<Long> idList) throws ServiceException {
if (idList == null || idList.size() == 0 || StringUtil.isBlank(entityCode)) {//参数合法性校验
return finalResult;
}
finalResult = new ArrayList<Map<String, String>>();
List<Future<Map<String, String>>> futureList = new ArrayList<Future<Map<String, String>>>();
int threadNum = idList.size();//查询子线程数目
for (int i = 0; i < threadNum; i++) {
Long itemId = idList.get(i);
Future<Map<String, String>> future = executor.submit(new QueryCallable (entityCode, itemId));
futureList.add(future);
}
for(Future<Map<String, String>> future : futureList) {
Map<String, String> threadResult = null;
try {
threadResult = future.get();
} catch (Exception e) {
threadResult = null;
}
if (null != threadResult && threadResult.size() > 0) {//结果集不为空
finalResult.add(threadResult);
}
}
return finalResult;
}
private String entityCode = "";
private Long itemId = 0L;
public GetEntityListCallable(String entityCode, Long itemId) {
this. entityCode = entityCode;
this.itemId = itemId;
}
public Map<String, String> call() throws Exception {
Map<String, String> entityMap = null;
try {
entityMap = QueryServiceImpl.this.getEntity(entityCode, itemId);//先去hbase查基本信息
} catch (Exception e) {
entityMap = null;
}
return entityMap;
}
}
另外,今天也尝试了另一种合并Solr索引的方法,直接通过底层的Lucene的API进行,而不是提交Http请求,具体方法如下:
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
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
posted on 2011-12-29 23:10 Phinecos(洞庭散人) 阅读(1760) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2008-12-29 COM组件开发实践(八)---多线程ActiveX控件和自动调整ActiveX控件大小(下)
2008-12-29 COM组件开发实践(七)---多线程ActiveX控件和自动调整ActiveX控件大小(上)
2007-12-29 HDU1014 Uniform Generator