List of usage examples for org.apache.solr.client.solrj.request CollectionAdminRequest createCollection
public static Create createCollection(String collection, String config, int numShards, int numReplicas)
From source file:com.shaie.solr.solrj.CollectionAdminHelper.java
License:Apache License
/** Creates a collection. */ public CreateCollectionResponse createCollection(String collectionName, int numShards, int numReplicas, String configName) {//from w w w . j av a 2s. c om if (collectionExists(collectionName)) { return null; } try { final CollectionAdminRequest.Create createCollectionRequest = CollectionAdminRequest .createCollection(collectionName, configName, numShards, numReplicas); final CollectionAdminResponse response = createCollectionRequest.process(solrClient); return new CreateCollectionResponse(response); } catch (IOException | SolrServerException e) { throw new RuntimeException(e); } }
From source file:io.redlink.solrlib.cloud.SolrCloudConnector.java
License:Apache License
@Override
@SuppressWarnings({ "squid:S1141", "squid:S3776" })
protected void init(ExecutorService executorService) throws IOException, SolrServerException {
final Path sharedLibs = Files.createTempDirectory("solrSharedLibs");
try (CloudSolrClient client = createSolrClient()) {
/* NOTE: do not use as this breaks compatibility with lower Solr Versions
* <code>final List<String> existingCollections = CollectionAdminRequest.listCollections(client);</code>
*//*from w w w. java 2 s . com*/
@SuppressWarnings("unchecked")
final List<String> existingCollections = (List<String>) new CollectionAdminRequest.List()
.process(client).getResponse().get("collections");
for (SolrCoreDescriptor coreDescriptor : coreDescriptors) {
final String coreName = coreDescriptor.getCoreName();
final String remoteName = createRemoteName(coreName);
if (availableCores.containsKey(coreName)) {
log.warn("CoreName-Clash: {} already initialized. Skipping {}", coreName,
coreDescriptor.getClass());
continue;
} else {
log.info("Initializing Core {} (remote: {})", coreName, remoteName);
}
if (config.isDeployCores()) {
final Path tmp = Files.createTempDirectory(coreName);
try {
coreDescriptor.initCoreDirectory(tmp, sharedLibs);
uploadConfig(remoteName, tmp);
if (!existingCollections.contains(remoteName)) {
// TODO: Check and log the response
final NamedList<Object> response = client.request(CollectionAdminRequest
.createCollection(remoteName, remoteName,
Math.max(1, coreDescriptor.getNumShards()),
Math.max(2, coreDescriptor.getReplicationFactor()))
.setMaxShardsPerNode(config.getMaxShardsPerNode()));
log.debug("Created Collection {}, CoreAdminResponse: {}", coreName, response);
scheduleCoreInit(executorService, coreDescriptor, true);
} else {
log.debug("Collection {} already exists in SolrCloud '{}' as {}", coreName,
config.getZkConnection(), remoteName);
// TODO: Check and log the response
final NamedList<Object> response = client
.request(CollectionAdminRequest.reloadCollection(remoteName));
log.debug("Reloaded Collection {}, CoreAdminResponse: {}", coreName, response);
scheduleCoreInit(executorService, coreDescriptor, false);
}
availableCores.put(coreName, coreDescriptor);
} catch (SolrServerException e) {
log.debug("Initializing core {} ({}) failed: {}", coreName, remoteName, e.getMessage());
throw new IOException(
String.format("Initializing collection %s (%s) failed", coreName, remoteName), e);
} finally {
PathUtils.deleteRecursive(tmp);
}
} else {
if (existingCollections.contains(remoteName)) {
log.debug("Collection {} exists in SolrCloud '{}' as {}", coreName,
config.getZkConnection(), remoteName);
scheduleCoreInit(executorService, coreDescriptor, false);
availableCores.put(coreName, coreDescriptor);
} else {
log.warn(
"Collection {} (remote: {}) not available in SolrCloud '{}' "
+ "but deployCores is set to false",
coreName, remoteName, config.getZkConnection());
}
}
}
log.info("Initialized {} collections in Solr-Cloud {}: {}", availableCores.size(),
config.getZkConnection(), availableCores);
} catch (IOException e) {
throw e;
} catch (SolrServerException e) {
log.error("Could not list existing collections: {}", e.getMessage(), e);
throw new IOException("Could not list existing collections", e);
} catch (final Exception t) {
log.error("Unexpected {} during init(): {}", t.getClass().getSimpleName(), t.getMessage(), t);
throw t;
} finally {
PathUtils.deleteRecursive(sharedLibs);
}
}
From source file:org.apache.nifi.processors.solr.QuerySolrIT.java
License:Apache License
@BeforeClass public static void setup() throws IOException, SolrServerException { CloudSolrClient solrClient = createSolrClient(); Path currentDir = Paths.get(ZK_CONFIG_PATH); solrClient.uploadConfig(currentDir, ZK_CONFIG_NAME); solrClient.setDefaultCollection(SOLR_COLLECTION); if (!solrClient.getZkStateReader().getClusterState().hasCollection(SOLR_COLLECTION)) { CollectionAdminRequest.Create createCollection = CollectionAdminRequest .createCollection(SOLR_COLLECTION, ZK_CONFIG_NAME, 1, 1); createCollection.process(solrClient); } else {//from w w w.j a va2s . com solrClient.deleteByQuery("*:*"); } for (int i = 0; i < 10; i++) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "doc" + i); Date date = new Date(); doc.addField("created", DATE_FORMAT.format(date)); doc.addField("string_single", "single" + i + ".1"); doc.addField("string_multi", "multi" + i + ".1"); doc.addField("string_multi", "multi" + i + ".2"); doc.addField("integer_single", i); doc.addField("integer_multi", 1); doc.addField("integer_multi", 2); doc.addField("integer_multi", 3); doc.addField("double_single", 0.5 + i); solrClient.add(doc); } solrClient.commit(); }
From source file:org.apache.sentry.tests.e2e.solr.AbstractSolrSentryTestCase.java
License:Apache License
protected void createCollection(String userName, String collectionName, String configName, int numShards, int numReplicas) throws SolrServerException, IOException { String tmp = getAuthenticatedUser(); try {//from w w w .j av a 2 s . c o m setAuthenticationUser(userName); // Create collection. CollectionAdminRequest.Create createCmd = CollectionAdminRequest.createCollection(collectionName, configName, numShards, numReplicas); assertEquals(0, createCmd.process(cluster.getSolrClient()).getStatus()); } finally { setAuthenticationUser(tmp); } }
From source file:org.apache.sentry.tests.e2e.solr.AbstractSolrSentryTestCase.java
License:Apache License
protected void adminUpdateActionFailure(String userName, String collectionName) throws SolrServerException, IOException { String tmp = getAuthenticatedUser(); try {/*from w w w .j ava2 s .co m*/ setAuthenticationUser(userName); // This user doesn't have admin permissions // Create collection. CollectionAdminRequest.Create createCmd = CollectionAdminRequest.createCollection(collectionName, "cloud-minimal", 1, NUM_SERVERS); createCmd.process(cluster.getSolrClient()); fail("This admin request should have failed with authorization error."); } catch (RemoteSolrException ex) { assertEquals(HttpServletResponse.SC_FORBIDDEN, ex.code()); } finally { setAuthenticationUser(tmp); } }
From source file:org.apache.sentry.tests.e2e.solr.TestSolrAdminOperations.java
License:Apache License
protected void adminUpdateActionSuccess(String userName, String collectionName) throws SolrServerException, IOException { // Success./*from w ww.j a va2 s . com*/ String tmp = getAuthenticatedUser(); try { // Create collection. setAuthenticationUser(userName); CollectionAdminRequest.Create createCmd = CollectionAdminRequest.createCollection(collectionName, "cloud-minimal", 1, NUM_SERVERS); assertEquals(0, createCmd.process(cluster.getSolrClient()).getStatus()); // Delete collection. CollectionAdminRequest.Delete delCmd = CollectionAdminRequest.deleteCollection(collectionName); assertEquals(0, delCmd.process(cluster.getSolrClient()).getStatus()); } finally { setAuthenticationUser(tmp); } }
From source file:org.broadleafcommerce.core.search.service.solr.SolrSearchServiceImpl.java
License:Apache License
@Override public void afterPropertiesSet() throws Exception { if (SolrContext.isSolrCloudMode()) { //We want to use the Solr APIs to make sure the correct collections are set up. CloudSolrServer primary = (CloudSolrServer) SolrContext.getServer(); CloudSolrServer reindex = (CloudSolrServer) SolrContext.getReindexServer(); if (primary == null || reindex == null) { throw new IllegalStateException("The primary and reindex CloudSolrServers must not be null. Check " + "your configuration and ensure that you are passing a different instance for each to the " + "constructor of " + this.getClass().getName() + " and ensure that each has a null (empty)" + " defaultCollection property, or ensure that defaultCollection is unique between" + " the two instances. All other things, like Zookeeper addresses should be the same."); }/*from www . j a v a 2s . c om*/ if (primary == reindex) { //These are the same object instances. They should be separate instances, with generally //the same configuration, except for the defaultCollection name. throw new IllegalStateException( "The primary and reindex CloudSolrServers must be different instances " + "and their defaultCollection property must be unique or null. All other things like the " + "Zookeeper addresses should be the same."); } //Set the default collection if it's null if (StringUtils.isEmpty(primary.getDefaultCollection())) { primary.setDefaultCollection(SolrContext.PRIMARY); } //Set the default collection if it's null if (StringUtils.isEmpty(reindex.getDefaultCollection())) { reindex.setDefaultCollection(SolrContext.REINDEX); } if (primary.getDefaultCollection().equals(reindex.getDefaultCollection())) { throw new IllegalStateException( "The primary and reindex CloudSolrServers must have a null (empty) or " + "unique defaultCollection property. All other things like the " + "Zookeeper addresses should be the same."); } primary.connect(); //This is required to ensure no NPE! //Get a list of existing collections so we don't overwrite one Set<String> collectionNames = primary.getZkStateReader().getClusterState().getCollections(); if (collectionNames == null) { collectionNames = new HashSet<String>(); } Aliases aliases = primary.getZkStateReader().getAliases(); Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap(); if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(primary.getDefaultCollection())) { //Create a completely new collection String collectionName = null; for (int i = 0; i < 1000; i++) { collectionName = "blcCollection" + i; if (collectionNames.contains(collectionName)) { collectionName = null; } else { break; } } CollectionAdminRequest.createCollection(collectionName, solrCloudNumShards, solrCloudConfigName, primary); CollectionAdminRequest.createAlias(primary.getDefaultCollection(), collectionName, primary); } else { //Aliases can be mapped to collections that don't exist.... Make sure the collection exists String collectionName = aliasCollectionMap.get(primary.getDefaultCollection()); collectionName = collectionName.split(",")[0]; if (!collectionNames.contains(collectionName)) { CollectionAdminRequest.createCollection(collectionName, solrCloudNumShards, solrCloudConfigName, primary); } } //Reload the collection names collectionNames = primary.getZkStateReader().getClusterState().getCollections(); if (collectionNames == null) { collectionNames = new HashSet<String>(); } //Reload these maps for the next collection. aliases = primary.getZkStateReader().getAliases(); aliasCollectionMap = aliases.getCollectionAliasMap(); if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(reindex.getDefaultCollection())) { //Create a completely new collection String collectionName = null; for (int i = 0; i < 1000; i++) { collectionName = "blcCollection" + i; if (collectionNames.contains(collectionName)) { collectionName = null; } else { break; } } CollectionAdminRequest.createCollection(collectionName, solrCloudNumShards, solrCloudConfigName, primary); CollectionAdminRequest.createAlias(reindex.getDefaultCollection(), collectionName, primary); } else { //Aliases can be mapped to collections that don't exist.... Make sure the collection exists String collectionName = aliasCollectionMap.get(reindex.getDefaultCollection()); collectionName = collectionName.split(",")[0]; if (!collectionNames.contains(collectionName)) { CollectionAdminRequest.createCollection(collectionName, solrCloudNumShards, solrCloudConfigName, primary); } } } }
From source file:org.codice.ddf.commands.solr.SolrCommandTest.java
License:Open Source License
protected static void createDefaultCollection() throws Exception { CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(DEFAULT_CORE_NAME, DEFAULT_CONFIGSET, 1, 1);/* w ww.j a va2 s . c o m*/ CollectionAdminResponse response = create.process(miniSolrCloud.getSolrClient()); if (response.getStatus() != 0 || response.getErrorMessages() != null) { fail("Could not create collection. Response: " + response.toString()); } List<String> collections = CollectionAdminRequest.listCollections(miniSolrCloud.getSolrClient()); assertThat(collections.size(), is(1)); miniSolrCloud.getSolrClient().setDefaultCollection(DEFAULT_CORE_NAME); }
From source file:org.deeplearning4j.nn.dataimport.solr.client.solrj.io.stream.TupleStreamDataSetIteratorTest.java
License:Apache License
@BeforeClass public static void setupCluster() throws Exception { final int numShards = 2; final int numReplicas = 2; final int maxShardsPerNode = 1; final int nodeCount = (numShards * numReplicas + (maxShardsPerNode - 1)) / maxShardsPerNode; // create and configure cluster configureCluster(nodeCount).addConfig("conf", configset("mini")).configure(); // create an empty collection CollectionAdminRequest.createCollection("mySolrCollection", "conf", numShards, numReplicas) .setMaxShardsPerNode(maxShardsPerNode).process(cluster.getSolrClient()); // compose an update request final UpdateRequest updateRequest = new UpdateRequest(); final List<Integer> docIds = new ArrayList<Integer>(); for (int phase = 1; phase <= 2; ++phase) { int docIdsIdx = 0; if (phase == 2) { Collections.shuffle(docIds); }/* w w w . j ava 2 s. co m*/ final int increment = 32; for (int b = 0; b <= 256; b += increment) { if (256 == b) b--; for (int g = 0; g <= 256; g += increment) { if (256 == g) g--; for (int r = 0; r <= 256; r += increment) { if (256 == r) r--; if (phase == 1) { docIds.add(docIds.size() + 1); continue; } final float luminance = (b * 0.0722f + g * 0.7152f + r * 0.2126f) / (255 * 3.0f); // https://en.wikipedia.org/wiki/Luma_(video) final SolrInputDocument doc = sdoc("id", Integer.toString(docIds.get(docIdsIdx++)), "channel_b_f", Float.toString(b / 255f), "channel_g_f", Float.toString(g / 255f), "channel_r_f", Float.toString(r / 255f), "luminance_f", Float.toString(luminance)); updateRequest.add(doc); ++numDocs; } } } } // make the update request updateRequest.commit(cluster.getSolrClient(), "mySolrCollection"); }
From source file:org.janusgraph.diskstorage.solr.SolrIndex.java
License:Apache License
private static void createCollectionIfNotExists(CloudSolrClient client, Configuration config, String collection) throws IOException, SolrServerException, KeeperException, InterruptedException { if (!checkIfCollectionExists(client, collection)) { final Integer numShards = config.get(NUM_SHARDS); final Integer maxShardsPerNode = config.get(MAX_SHARDS_PER_NODE); final Integer replicationFactor = config.get(REPLICATION_FACTOR); // Ideally this property used so a new configset is not uploaded for every single // index (collection) created in solr. // if a generic configSet is not set, make the configset name the same as the collection. // This was the default behavior before a default configSet could be specified final String genericConfigSet = config.has(SOLR_DEFAULT_CONFIG) ? config.get(SOLR_DEFAULT_CONFIG) : collection;/*from w w w .j a va 2s. co m*/ final CollectionAdminRequest.Create createRequest = CollectionAdminRequest.createCollection(collection, genericConfigSet, numShards, replicationFactor); createRequest.setMaxShardsPerNode(maxShardsPerNode); final CollectionAdminResponse createResponse = createRequest.process(client); if (createResponse.isSuccess()) { logger.trace("Collection {} successfully created.", collection); } else { throw new SolrServerException(Joiner.on("\n").join(createResponse.getErrorMessages())); } } waitForRecoveriesToFinish(client, collection); }