在solrcloud出来之前,如果通过solrj连接solrserver,需要程序自己实现一致性hash.
新版本的solr支持cloud的部署方式,可以自动实现lb和sharding的功能(通过CloudSolrServer类连接cloud),可以用下面代码做测试
需要的jar包如下:
1
2
3
4
5
6
7
8
9
|
apache-solr-solrj.jar
apache-solr-core.jar
zookeeper.jar
commons-logging.jar
apache-logging-log4j.jar
httpclient.jar
httpcore.jar
slf4j-api.jar
slf4j-nop.jar
|
sample code:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
import
java.io.IOException;
import
java.util.ArrayList;
import
java.util.Collection;
import
org.apache.solr.client.solrj.SolrQuery;
import
org.apache.solr.client.solrj.SolrServer;
import
org.apache.solr.client.solrj.SolrServerException;
import
org.apache.solr.client.solrj.impl.CloudSolrServer;
import
org.apache.solr.client.solrj.response.QueryResponse;
import
org.apache.solr.common.SolrDocument;
import
org.apache.solr.common.SolrDocumentList;
import
org.apache.solr.common.SolrInputDocument;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.apache.solr.common.cloud.ClusterState;
import
org.apache.solr.common.cloud.ZkStateReader;
public
class
SolrCloudTest {
public
static
final
Log LOG = LogFactory.getLog(SolrCloudTest.
class
);
private
static
CloudSolrServer cloudSolrServer;
private
static
synchronized
CloudSolrServer getCloudSolrServer(
final
String zkHost) {
LOG.info(
"connect to :"
+zkHost+
"\n"
);
if
(cloudSolrServer ==
null
) {
try
{
cloudSolrServer =
new
CloudSolrServer(zkHost);
}
catch
(Exception e) {
e.printStackTrace();
}
}
return
cloudSolrServer ;
}
private
void
addIndex(SolrServer solrServer) {
try
{
Collection<SolrInputDocument> docs =
new
ArrayList<SolrInputDocument>();
for
(
int
i =
0
;i<=
200
;i++){
SolrInputDocument doc =
new
SolrInputDocument();
String key =
""
;
key = String. valueOf(i);
doc.addField(
"id"
, key);
doc.addField(
"test_s"
, key+
"value"
);
docs.add(doc);
}
LOG.info(
"docs info:"
+docs+
"\n"
);
System. out.println(
"docs length "
+ docs.size());
solrServer.add(docs);
solrServer.commit();
}
catch
(SolrServerException e) {
System. out.println(
"Add docs Exception !!!"
);
e.printStackTrace();
}
catch
(IOException e){
e.printStackTrace();
}
catch
(Exception e) {
System. out.println(
"Unknowned Exception!!!!!"
);
e.printStackTrace();
}
}
public
void
search(SolrServer solrServer, String Str) {
SolrQuery query =
new
SolrQuery();
query.setRows(
20
);
//默认row是10,只返回10条,如果设置query.setRows(Integer. MAX_VALUE)会报错,默认最大为2147483391
query.setQuery(Str);
try
{
LOG.info(
"query string: "
+ Str);
System. out.println(
"query string: "
+ Str);
QueryResponse response = solrServer.query(query);
SolrDocumentList docs = response.getResults();
System. out.println(docs);
System. out.println(docs.size());
System. out.println(
"doc num:"
+ docs.getNumFound());
System. out.println(
"elapse time:"
+ response.getQTime());
for
(SolrDocument doc : docs) {
String area = (String) doc.getFieldValue(
"test_s"
);
String id = (String) doc.getFieldValue(
"id"
);
System. out.println(
"id: "
+ id);
System. out.println(
"tt_s: "
+ area);
System. out.println();
}
}
catch
(SolrServerException e) {
e.printStackTrace();
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
void
deleteAllIndex(SolrServer solrServer) {
try
{
solrServer.deleteByQuery(
"*:*"
);
solrServer.commit();
}
catch
(SolrServerException e){
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
void
main(String[] args) {
final
String zkHost =
"xxxxx:8080"
;
final
String defaultCollection =
"test"
;
final
int
zkClientTimeout =
10000
;
final
int
zkConnectTimeout =
10000
;
CloudSolrServer cloudSolrServer = getCloudSolrServer(zkHost);
cloudSolrServer.setDefaultCollection(defaultCollection);
cloudSolrServer.setZkClientTimeout(zkClientTimeout);
cloudSolrServer.setZkConnectTimeout(zkConnectTimeout);
try
{
cloudSolrServer.connect();
System. out.println(
"connect solr cloud zk sucess"
);
}
catch
(Exception e){
LOG.error(
"connect to collection "
+defaultCollection+
" error\n"
);
System. out.println(
"error message is:"
+e);
e.printStackTrace();
System. exit(
1
);
}
//ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader();
//ClusterState cloudState = zkStateReader.getClusterState();
//System.out.println(cloudState);
SolrCloudTest solrt =
new
SolrCloudTest();
System. out.println(
"====="
);
try
{
solrt.addIndex(cloudSolrServer);
}
catch
(Exception e){
e.printStackTrace();
}
solrt.search(cloudSolrServer,
"id:*"
);
//solrt.deleteAllIndex(cloudSolrServer);
System. out.println(
"hashCode"
+solrt.hashCode());
cloudSolrServer.shutdown();
}
}
|
本文转自菜菜光 51CTO博客,原文链接:http://blog.51cto.com/caiguangguang/1433748,如需转载请自行联系原作者