1、代码模块
因为solrj没有提供MySQL的索引支持,所以只能基于http请求实现索引MySQL
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
public class SolrDataImportClient {
private CloseableHttpClient httpclient = null;
private CookieStore cookieStore = null;
private final String baseSolrUrl;
/**
* 全量导入
*/
private static final String FULL_IMPORT = "full-import";
/**
* 增量导入
*/
private static final String DELTA_IMPORT = "delta-import";
public SolrDataImportClient(String baseSolrUrl) {
if(!baseSolrUrl.endsWith("/")){
baseSolrUrl += "/";
}
this.baseSolrUrl = baseSolrUrl;
}
private synchronized void init(){
if(httpclient == null){
cookieStore = new BasicCookieStore();
httpclient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
}
}
public void fullImport(String coreName) throws IOException {
init();
HttpPost post = getDataImportPost(coreName, FULL_IMPORT);
httpclient.execute(post);
}
public void deltaImport(String coreName) throws IOException {
init();
HttpPost post = getDataImportPost(coreName, DELTA_IMPORT);
httpclient.execute(post);
}
private HttpPost getDataImportPost(String coreName,String command) throws UnsupportedEncodingException{
HttpPost post = new HttpPost(baseSolrUrl + coreName + "/dataimport?indent=on&wt=json&_=" + new Date().getTime());
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("command", command));
params.add(new BasicNameValuePair("verbose", "false"));
if(command.equals(FULL_IMPORT)){
params.add(new BasicNameValuePair("clean", "true"));
}
params.add(new BasicNameValuePair("commit", "true"));
params.add(new BasicNameValuePair("optimize", "false"));
params.add(new BasicNameValuePair("core", coreName));
params.add(new BasicNameValuePair("name", "dataimport"));
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
return post;
}
public void close() throws IOException{
httpclient.close();
httpclient = null;
cookieStore = null;
}
}
2、使用示例
/**
* 全量更新
*/
@Test
public void fullImport() throws Exception{
SolrDataImportClient dataClient = new SolrDataImportClient("http://192.168.100.27:8983/solr/");
dataClient.fullImport("testcore");
dataClient.close();
}
/**
* 增量更新
*/
@Test
public void deltaImport() throws Exception{
SolrDataImportClient dataClient = new SolrDataImportClient("http://192.168.100.27:8983/solr/");
dataClient.deltaImport("testcore");
dataClient.close();
}