Project

General

Profile

« Previous | Next » 

Revision 48891

upgraded solr version to 6.6.0

View differences:

SolrIndexServerDAO.java
1 1
package eu.dnetlib.functionality.index;
2 2

  
3
import java.io.IOException;
3 4
import java.io.StringReader;
4 5
import java.util.Map;
5 6

  
6 7
import eu.dnetlib.functionality.cql.CqlValueTransformerMap;
7 8
import org.apache.commons.logging.Log;
8 9
import org.apache.commons.logging.LogFactory;
9
import org.apache.solr.client.solrj.impl.CloudSolrServer;
10

  
11
import org.apache.solr.client.solrj.impl.CloudSolrClient;
10 12
import org.dom4j.Document;
11 13
import org.dom4j.DocumentException;
12 14
import org.dom4j.io.SAXReader;
......
69 71
	 */
70 72
	@Override
71 73
	public void createIndexCollection(final MetadataReference mdref, final String fields) throws IndexServiceException {
72
		CloudSolrServer server = null;
74
		CloudSolrClient client = null;
73 75
		try {
76
			client = getClient();
77
			client.connect();
74 78

  
75
			server = getServer();
76
			server.connect();
77

  
78
			if (!solrAdministrator.indexCollectionExists(mdref.toString(), server)) {
79
			if (!solrAdministrator.indexCollectionExists(mdref.toString(), client)) {
79 80
				Map<String, String> params = Maps.newHashMap();
80 81

  
81 82
				final Map<String, String> p = getServiceProperties();
......
86 87
					params.put(param_Name.toString(), p.get(param_Name.toString()));
87 88
				}
88 89

  
89
				zkUtils.uploadZookeperConfig(server.getZkStateReader().getZkClient(), mdref.toString(), parse(fields), params, false);
90
				zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdref.toString(), parse(fields), params, false);
90 91
				solrAdministrator.createSolrIndex(p.get("host"), p.get("port"), mdref.toString(), p.get("numShards"), p.get("replicationFactor"),
91 92
						mdref.toString(), p.get("maxShardsPerNode"));
92 93
			}
93
			server.getZkStateReader().close();
94
			client.getZkStateReader().close();
94 95

  
95 96
		} catch (Exception e) {
96 97
			log.error("Error on creating IndexCollection", e);
97 98
			throw new IndexServiceException("Error on creating IndexCollection", e);
98 99
		} finally {
99
			if (server != null) {
100
				server.shutdown();
100
			if (client != null) {
101
				shutdown(mdref);
101 102
			}
102 103
		}
103 104
	}
104 105

  
105 106
	@Override
106 107
	public void updateIndexCollection(final MetadataReference mdRef, final Document fields) throws IndexServiceException {
107
		CloudSolrServer server = null;
108
		CloudSolrClient client = null;
108 109
		try {
109
			server = getServer();
110
			server.connect();
110
			client = getClient();
111
			client.connect();
111 112
			Map<String, String> params = Maps.newHashMap();
112 113

  
113 114
			params.put("numShards", getServiceProperties().get("numShards"));
......
117 118
				params.put(param_Name.toString(), getServiceProperties().get(param_Name.toString()));
118 119
			}
119 120

  
120
			zkUtils.uploadZookeperConfig(server.getZkStateReader().getZkClient(), mdRef.toString(), fields, params, true);
121
			server.getZkStateReader().close();
122
			server.shutdown();
121
			zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdRef.toString(), fields, params, true);
122
			client.getZkStateReader().close();
123
			shutdown(mdRef);
123 124
			solrAdministrator.reloadCollection(getServiceProperties().get("host"), getServiceProperties().get("port"), mdRef.toString());
124 125

  
125 126
		} catch (Exception e) {
126 127
			log.error("Error on updating IndexCollection", e);
127 128
			throw new IndexServiceException("Error on updating IndexCollection", e);
128 129
		} finally {
129
			if (server != null) {
130
				server.shutdown();
130
			if (client != null) {
131
				shutdown(mdRef);
131 132
			}
132 133
		}
133 134
	}
......
156 157
	 */
157 158
	@Override
158 159
	public IndexCollection getIndexCollection(final MetadataReference mdref) throws IndexServiceException {
159
		CloudSolrServer newServer = getServer(mdref);
160
		return new SolrIndexCollection(newServer);
160
		final CloudSolrClient client = getClient(mdref);
161
		return new SolrIndexCollection(client);
161 162
	}
162 163

  
163 164
	/**
......
167 168
	 */
168 169
	@Override
169 170
	public Map<String, ValueType> getSchema(final MetadataReference mdRef) throws IndexServiceException {
170
		CloudSolrServer server = getServer(mdRef);
171
		Map<String, ValueType> fields = solrAdministrator.getFieldNamesAndTypes(mdRef.toString(), server);
172
		server.shutdown();
171
		CloudSolrClient client = getClient(mdRef);
172
		Map<String, ValueType> fields = solrAdministrator.getFieldNamesAndTypes(mdRef.toString(), client);
173
		shutdown(mdRef);
173 174
		return fields;
174 175
	}
175 176

  
......
200 201
	 */
201 202
	@Override
202 203
	public void shutdown(final MetadataReference mdRef) throws IndexServiceException {
203
		getServer(mdRef).shutdown();
204

  
204
		try {
205
			getClient(mdRef).close();
206
		} catch (IOException e) {
207
			throw new IndexServiceException(e);
208
		}
205 209
	}
206 210

  
207 211
	/**
......
213 217
	 * @throws IndexServiceException
214 218
	 *             the index service exception
215 219
	 */
216
	private CloudSolrServer getServer(final MetadataReference mdRef) throws IndexServiceException {
217
		CloudSolrServer server = getServer();
220
	private CloudSolrClient getClient(final MetadataReference mdRef) throws IndexServiceException {
221
		CloudSolrClient server = getClient();
218 222
		server.setDefaultCollection(mdRef.toString());
219 223
		return server;
220 224
	}
......
224 228
	 * 
225 229
	 * @return a server instance
226 230
	 */
227
	private CloudSolrServer getServer() {
228
		String address = getEndpoint().get(ADDRESS);
231
	private CloudSolrClient getClient() {
232
		final String address = getEndpoint().get(ADDRESS);
229 233
		log.info("connecting to address: " + address);
230
		return new CloudSolrServer(address);
231

  
234
		return new CloudSolrClient.Builder()
235
				.withZkHost(address)
236
				.build();
232 237
	}
233 238

  
234 239
	/**

Also available in: Unified diff