solr(5)通过Java代码索引MySQL

1、代码模块

因为solrj没有提供MySQL的索引支持,所以只能基于http请求实现索引MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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、使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 全量更新
*/
@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();
}