资源描述
1. 设置pom.xml
· 找到工程的pom.xml文件,增加依赖
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
其需要依赖的JAR包包含
因一般工程里都会引用这些依赖jar包,在这里就不做pom.xml里的配置了,大家参照上面的图增加依赖和替换版本即可。
2. 设置solrcloud.properties
· 在工程中新增properties配置,内容为
solr.solrcloudServer=192.168.169.129:2181,192.168.169.12:2181,192.168.169.15:2181/bjk
solr.zkClientTimeout=2000
solr.zkConnectTimeout=2000
solr.solrcloudServer:是solrcloud的zookeeper配置信息
solr.zkClientTimeout:当前应用与zookeeper连接超时时间
solr.zkConnectTimeout:当前应用与zookeeper连接超时时间
3. 增加java文件
· 在工程中util目录下增加
4. 修改applicationContext.xml文件
· 找到spring主配置文件applicationContext.xml,增加如下配置
<!--红色位置为项目中solrcloud.properties文件的实际位置-->
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/environments/development/solrcloud.properties" />
<!--红色位置为项目中SolrCloudUtils.java文件的实际包路径-->
<bean name="solrCloudUtils" class="com.jd.sph.report.service.solr.SolrCloudUtils">
<property name="solrcloudServer" value="${solr.solrcloudServer}"></property>
<property name="zkClientTimeout" value="${solr.zkClientTimeout}"></property>
<property name="zkConnectTimeout" value="${solr.zkConnectTimeout}"></property>
</bean>
5. 操作solrj新建和查询索引
在需要操作solrj的类上增加依赖注解信息
@Resource
private SolrCloudUtils solrCloudUtils;
查询索引
/**
* 从solr查询每日入库
* @param query
* @param page
* @return
*/
public Page<InBoundPo> selectInBoundDayBySolr(PanYingDetailQueryVo query ,Page<InBoundPo> page){
//设置查询条件
SolrQuery solrQuery = new SolrQuery();
StringBuffer queryStr = new StringBuffer("io:1 AND status:4 AND upDownTime:[2014-10-01T00:00:00Z TO 2015-10-01T00:00:00Z]");
if(query.getOrgId()!=null && query.getOrgId()>0){
queryStr.append(" AND newIdCompany:"+query.getOrgId());
}
if(query.getStoreId()!=null && query.getStoreId()>0){
queryStr.append(" AND storeAttr:"+query.getStoreId());
}
System.out.println("查询参数 "+queryStr);
//设置查询条件
solrQuery.setQuery(queryStr.toString());
//设置查询分页条件
solrQuery.set("start",(page.getPageNo()-1)*page.getPageSize());
solrQuery.set("rows",page.getPageSize());
//设置查询排序
solrQuery.setSort("upDownTime",SolrQuery.ORDER.desc);
List<InBoundPo> list = new ArrayList<InBoundPo>();
CloudSolrServer cloudSolrServer = null;
try {
//根据core名称获取solrCloud连接
cloudSolrServer = solrCloudUtils.getCloundSolrServer("transferdetail");
QueryResponse res = cloudSolrServer.query(solrQuery);
SolrDocumentList docs = res.getResults();
InBoundPo inBoundPo = null;
//循环将索引文档的信息转换到实体对象中
for(SolrDocument doc:docs){
inBoundPo = new InBoundPo();
inBoundPo.setInBoundCode((Integer)doc.getFieldValue("transfer_id"));
inBoundPo.setOrgId((Integer)doc.getFieldValue("NewIdCompany"));
inBoundPo.setInType((Integer)doc.getFieldValue("type"));
inBoundPo.setStoreName((String)doc.getFieldValue("storeName"));
inBoundPo.setIdPart((String)doc.getFieldValue("idPart"));
inBoundPo.setWareName((String)doc.getFieldValue("wareName"));
inBoundPo.setWareId((Integer)doc.getFieldValue("wareId"));
inBoundPo.setCid1((Integer)doc.getFieldValue("cid1"));
inBoundPo.setCid2((Integer) doc.getFieldValue("cid2"));
inBoundPo.setCid3((Integer) doc.getFieldValue("cid3"));
inBoundPo.setSupplierID((String)doc.getFieldValue("supplierID"));
if((String)doc.getFieldValue("price")!=null){
inBoundPo.setCostPrice(new BigDecimal((String)doc.getFieldValue("price")));
}
inBoundPo.setLossType((Integer) doc.getFieldValue("losstype"));
inBoundPo.setXmlAttaches((String)doc.getFieldValue("xmlAttaches"));
inBoundPo.setSalesPin((String)doc.getFieldValue("salesPin"));
list.add(inBoundPo);
}
long numFound = docs.getNumFound();
page.setTotalCount(numFound);
System.out.println("查询总数量:"+numFound);
System.out.println("查询耗费时间:"+res.getQTime());
page.setResult(list);
} catch (SolrServerException e) {
e.printStackTrace();
} finally {
//关闭solrCloud连接
cloudSolrServer.shutdown();
}
return page;
}
新增索引
//创建solrserver连接
CloudSolrServer solrServer = solrCloudUtils.getCloundSolrServer("transferdetail");
//需要提交的索引集合
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
SolrInputDocument doc = null;
//获取transfer和defstore信息
for(BigEntity bigEntity:list){
doc = new SolrInputDocument();
//afs_transfer
doc.addField("transfer_id", bigEntity.getTransfer_id());
doc.addField("idStoreFrom", bigEntity.getIdStoreFrom());
doc.addField("idStoreTo", bigEntity.getIdStoreTo());
doc.addField("type", bigEntity.getType());
doc.addField("io", bigEntity.getIo());
doc.addField("status", bigEntity.getStatus());
doc.addField("yn", bigEntity.getYn());
doc.addField("fromPin", bigEntity.getFromPin());
doc.addField("fromName", bigEntity.getFromName());
if(bigEntity.getFromTime()!=null){
doc.addField("fromTime", solrUtil.solrTime(bigEntity.getFromTime()));
}
doc.addField("fromNotes", bigEntity.getFromNotes());
doc.addField("toPin", bigEntity.getToPin());
doc.addField("toName", bigEntity.getToName());
if(bigEntity.getToTime()!=null){
doc.addField("toTime", solrUtil.solrTime(bigEntity.getToTime()));
}
doc.addField("toNotes", bigEntity.getToNotes());
doc.addField("idCompany", bigEntity.getIdCompany());
doc.addField("kdanhao", bigEntity.getKdanhao());
doc.addField("istemp", bigEntity.getIstemp());
doc.addField("getWare", bigEntity.getGetWare());
doc.addField("newIdCompany", bigEntity.getNewIdCompany());
doc.addField("idStore", bigEntity.getIdStore());
if(bigEntity.getTimeUpdated()!=null){
doc.addField("timeUpdated", solrUtil.solrTime(bigEntity.getTimeUpdated()));
}
doc.addField("manUpdated", bigEntity.getManUpdated());
doc.addField("storeFrom", bigEntity.getStoreFrom());
doc.addField("storeTo", bigEntity.getStoreTo());
doc.addField("isApproved", bigEntity.getIsApproved());
if(bigEntity.getUpDownTime()!=null){
doc.addField("upDownTime", solrUtil.solrTime(bigEntity.getUpDownTime()));
}
doc.addField("inventory_status", bigEntity.getInventory_status());
doc.addField("inventory_name", bigEntity.getInventory_name());
doc.addField("inventory_pin", bigEntity.getInventory_pin());
doc.addField("inventory_check_notes", bigEntity.getInventory_check_notes());
if(bigEntity.getInventory_time()!=null){
doc.addField("inventory_time", solrUtil.solrTime(bigEntity.getInventory_time()));
}
doc.addField("ts", bigEntity.getTs());
doc.addField("rfidType", bigEntity.getRfidType());
docs.add(doc);
}
//提交到solrcloud
solrServer.add(docs);
solrSmit();
//关闭solrcloud连接
solrServer.shutdown();
注意:时间处理需要将时间转换为yyyy-MM-ddThh:mm:ss:Z
千万不要忘了T和Z
因为SOLR存储索引的时候,默认是以格林威治时间进行存储,当创建索引的时候,时间会减去8小时,所以为了保证时间一致性,我们在操作时间的时候,要么在创建索引时时间增加8小时,要么在查询时,时间减去8小时。
展开阅读全文