java連線solr並建立索引
阿新 • • 發佈:2019-01-25
1、在tomcat上配置solr:(以後補充)
2、新建一個實體類@Field下面的key要與solr對應索引目錄的配置檔案schema中的key相對應,程式碼demo如下,其中建構函式只是方便整體賦值:
package com.vo; import org.apache.solr.client.solrj.beans.Field; public class ClassName { @Field private String key1; @Field private String key2; @Field private String key3; public ClassName(String key1) { super(); this.key1 = key1; } public String getkey1() { return key1; } public void setkey1(String key1) { this.key1 = key1; } public String getkey2() { return key2; } public void setkey2(String key2) { this.key2 = key2; } public String getkey3() { return key3; } public void setkey3(String key3) { this.key3 = key3; }
3、連線solr:
import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.impl.HttpSolrServer; public class SolrContext { private static String url_video; private static String url_picture; private static String url_videoUpload; private static HttpSolrServer server_video; private static HttpSolrServer server_picture; private static HttpSolrServer server_videoUpload; static { url_video = PropertiesUtil.getValueByKey("storage.solr.url.video"); url_picture = PropertiesUtil.getValueByKey("storage.solr.url.picture"); url_videoUpload = PropertiesUtil.getValueByKey("storage.solr.url.videoupload"); } private SolrContext() { } public static SolrServer getVideoServer() throws Exception{ if(server_video==null) { synchronized(SolrContext.class){ if(server_video==null) { server_video=new HttpSolrServer(url_video); server_video.setConnectionTimeout(5000); //solr連線超時時間 server_video.setSoTimeout(5000); //solr查詢超時時間 server_video.setMaxTotalConnections(100); //solr所有最大連線數 server_video.setDefaultMaxConnectionsPerHost(100);//solr最大連線數 server_video.setAllowCompression(false); //solr是否允許壓縮 server_video.setMaxRetries(1); //solr最大重試次數 server_video.setFollowRedirects(true); } } } return server_video; } public static SolrServer getPictureServer() throws Exception{ if(server_picture==null) { synchronized(SolrContext.class){ if(server_picture==null) { server_picture=new HttpSolrServer(url_picture); server_picture.setConnectionTimeout(5000); //solr連線超時時間 server_picture.setSoTimeout(5000); //solr查詢超時時間 server_picture.setMaxTotalConnections(100); //solr所有最大連線數 server_picture.setDefaultMaxConnectionsPerHost(100);//solr最大連線數 server_picture.setAllowCompression(false); //solr是否允許壓縮 server_picture.setMaxRetries(1); //solr最大重試次數 server_picture.setFollowRedirects(true); } } } return server_picture; } public static SolrServer getVideoUploadServer() throws Exception{ if(server_videoUpload==null) { synchronized(SolrContext.class){ if(server_videoUpload==null) { server_videoUpload=new HttpSolrServer(url_videoUpload); server_videoUpload.setConnectionTimeout(5000); //solr連線超時時間 server_videoUpload.setSoTimeout(5000); //solr查詢超時時間 server_videoUpload.setMaxTotalConnections(100); //solr所有最大連線數 server_videoUpload.setDefaultMaxConnectionsPerHost(100);//solr最大連線數 server_videoUpload.setAllowCompression(false); //solr是否允許壓縮 server_videoUpload.setMaxRetries(1); //solr最大重試次數 server_videoUpload.setFollowRedirects(true); } } } return server_videoUpload; } }
4、建立索引,其中UUID碼是起到獨特的標識作用,不用也行:
import java.util.UUID; import org.apache.log4j.Logger; import com.eshore.storage.utils.SolrContext; import com.eshore.storage.vo.VideoUpload; public class VideoUploadServiceImpl { private static final Logger log = Logger.getLogger(VideoUploadServiceImpl.class); public boolean generateIndex() { try { VideoUpload field = new VideoUpload(UUID.randomUUID().toString().replaceAll("-", "")); field.setVideo_id("1"); field.setVideo_unique("1"); field.setVideo_name("1"); field.setStatus("1"); field.setVideo_desc("1"); field.setTag("1"); field.setIs_pay("1"); field.setImg("1"); field.setVideo_duration("1"); field.setInitial_size("1"); field.setError_desc("1"); field.setAdd_time("1"); field.setComplete_time("1"); SolrContext.getVideoUploadServer().addBean(field); SolrContext.getVideoUploadServer().commit(); System.out.println("success!"); return true; } catch (Exception e) { log.error("生成索引失敗>>>>>>>>>>>", e); return false; } } }