private void setDefaultCollection(SolrClient solrClient) { String defaultCollection = null; if (solrClient instanceof CloudSolrClient) { defaultCollection = ((CloudSolrClient) solrClient).getDefaultCollection(); } this.collection = defaultCollection; }
protected static SolrClient getSolrClient() { String zkHostString = "127.0.0.1:9983"; // zkHostString for Solr gettingstarted example return new CloudSolrClient(zkHostString); }
protected void prepare() { if (solrConfig.isKerberosEnabled()) { HttpClientUtil.setConfigurer(new Krb5HttpClientConfigurer()); } solrMapper.configure(); solrClient = new CloudSolrClient(solrConfig.getZkHostString()); }
/** * Initialize any state for this DB. Called once per DB instance; there is one DB instance per * client thread. */ @Override public void init() throws DBException { Properties props = getProperties(); commitTime = Integer .parseInt(props.getProperty("solr.commit.within.time", DEFAULT_COMMIT_WITHIN_TIME)); batchMode = Boolean.parseBoolean(props.getProperty("solr.batch.mode", DEFAULT_BATCH_MODE)); String jaasConfPath = props.getProperty("solr.jaas.conf.path"); if(jaasConfPath != null) { System.setProperty("java.security.auth.login.config", jaasConfPath); HttpClientUtil.setConfigurer(new Krb5HttpClientConfigurer()); } // Check if Solr cluster is running in SolrCloud or Stand-alone mode Boolean cloudMode = Boolean.parseBoolean(props.getProperty("solr.cloud", DEFAULT_CLOUD_MODE)); System.err.println("Solr Cloud Mode = " + cloudMode); if (cloudMode) { System.err.println("Solr Zookeeper Remote Hosts = " + props.getProperty("solr.zookeeper.hosts", DEFAULT_ZOOKEEPER_HOSTS)); client = new CloudSolrClient.Builder().withZkHost( Arrays.asList(props.getProperty("solr.zookeeper.hosts", DEFAULT_ZOOKEEPER_HOSTS).split(","))).build(); } else { client = new HttpSolrClient.Builder(props.getProperty("solr.base.url", DEFAULT_SOLR_BASE_URL)).build(); } }
HttpClientUtil.setConfigurer(new KerberosHttpClientConfigurer()); final HttpClient httpClient = HttpClientUtil.createClient(params); return new HttpSolrClient(solrLocation, httpClient); } else { final String collection = context.getProperty(COLLECTION).evaluateAttributeExpressions().getValue(); final Integer zkConnectionTimeout = context.getProperty(ZK_CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue(); CloudSolrClient cloudSolrClient = new CloudSolrClient(solrLocation, httpClient); cloudSolrClient.setDefaultCollection(collection); cloudSolrClient.setZkClientTimeout(zkClientTimeout); cloudSolrClient.setZkConnectTimeout(zkConnectionTimeout); return cloudSolrClient;
/** * Initialize any state for this DB. Called once per DB instance; there is one DB instance per * client thread. */ @Override public void init() throws DBException { Properties props = getProperties(); commitTime = Integer .parseInt(props.getProperty("solr.commit.within.time", DEFAULT_COMMIT_WITHIN_TIME)); batchMode = Boolean.parseBoolean(props.getProperty("solr.batch.mode", DEFAULT_BATCH_MODE)); String jaasConfPath = props.getProperty("solr.jaas.conf.path"); if(jaasConfPath != null) { System.setProperty("java.security.auth.login.config", jaasConfPath); HttpClientUtil.setConfigurer(new Krb5HttpClientConfigurer()); } // Check if Solr cluster is running in SolrCloud or Stand-alone mode Boolean cloudMode = Boolean.parseBoolean(props.getProperty("solr.cloud", DEFAULT_CLOUD_MODE)); System.err.println("Solr Cloud Mode = " + cloudMode); if (cloudMode) { System.err.println("Solr Zookeeper Remote Hosts = " + props.getProperty("solr.zookeeper.hosts", DEFAULT_ZOOKEEPER_HOSTS)); client = new CloudSolrClient( props.getProperty("solr.zookeeper.hosts", DEFAULT_ZOOKEEPER_HOSTS)); } else { client = new HttpSolrClient(props.getProperty("solr.base.url", DEFAULT_SOLR_BASE_URL)); } }
public SolrIndex(final Configuration config) throws BackendException { Preconditions.checkArgument(config!=null); configuration = config; mode = Mode.parse(config.get(SOLR_MODE)); dynFields = config.get(DYNAMIC_FIELDS); keyFieldIds = parseKeyFieldsForCollections(config); maxResults = config.get(INDEX_MAX_RESULT_SET_SIZE); ttlField = config.get(TTL_FIELD); waitSearcher = config.get(WAIT_SEARCHER); if (mode==Mode.CLOUD) { String zookeeperUrl = config.get(SolrIndex.ZOOKEEPER_URL); CloudSolrClient cloudServer = new CloudSolrClient(zookeeperUrl, true); cloudServer.connect(); solrClient = cloudServer; } else if (mode==Mode.HTTP) { HttpClient clientParams = HttpClientUtil.createClient(new ModifiableSolrParams() {{ add(HttpClientUtil.PROP_ALLOW_COMPRESSION, config.get(HTTP_ALLOW_COMPRESSION).toString()); add(HttpClientUtil.PROP_CONNECTION_TIMEOUT, config.get(HTTP_CONNECTION_TIMEOUT).toString()); add(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, config.get(HTTP_MAX_CONNECTIONS_PER_HOST).toString()); add(HttpClientUtil.PROP_MAX_CONNECTIONS, config.get(HTTP_GLOBAL_MAX_CONNECTIONS).toString()); }}); solrClient = new LBHttpSolrClient(clientParams, config.get(HTTP_URLS)); } else { throw new IllegalArgumentException("Unsupported Solr operation mode: " + mode); } }
/** * Checks if the collection has already been created in Solr. */ private static boolean checkIfCollectionExists(CloudSolrClient server, String collection) throws KeeperException, InterruptedException { ZkStateReader zkStateReader = server.getZkStateReader(); zkStateReader.updateClusterState(true); ClusterState clusterState = zkStateReader.getClusterState(); return clusterState.getCollectionOrNull(collection) != null; }
public SolrClient getSiteServer() { BroadleafRequestContext ctx = BroadleafRequestContext.getBroadleafRequestContext(); Site site = ctx.getNonPersistentSite(); CloudSolrClient client = (CloudSolrClient) primaryServer; client.connect(); String aliasName = getSiteAliasName(site); if (aliasName != null) { String collectionName = getSiteCollectionName(site); createCollectionIfNotExist(client, collectionName); createAliasIfNotExist(client, collectionName, collectionName); } return client; }
protected String determineCoreName(HttpSolrClient httpSolrClient) { String url = httpSolrClient.getBaseURL(); String coreName = url.substring(url.lastIndexOf('/') + 1); return coreName; }
public void configure(DefaultHttpClient httpClient, SolrParams config) { super.configure(httpClient, config); logger.info("Setting up SPNego auth..."); //Enable only SPNEGO authentication scheme. final AuthSchemeRegistry registry = new AuthSchemeRegistry(); registry.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false)); httpClient.setAuthSchemes(registry); // Get the credentials from the JAAS configuration rather than here final Credentials useJaasCreds = new Credentials() { public String getPassword() { return null; } public Principal getUserPrincipal() { return null; } }; final SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory(); httpClient.getCookieSpecs().register(cookieFactory.POLICY_NAME, cookieFactory); httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, cookieFactory.POLICY_NAME); httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, useJaasCreds); httpClient.addRequestInterceptor(bufferedEntityInterceptor); }
@Override public void prepare(Map<String, Object> topoConf, TopologyContext context, OutputCollector collector) { this.collector = collector; if (solrConfig.isKerberosEnabled()) { HttpClientUtil.setConfigurer(new Krb5HttpClientConfigurer()); } solrMapper.configure(); this.solrClient = new CloudSolrClient(solrConfig.getZkHostString()); this.toCommitTuples = new ArrayList<>(capacity()); }
@Override public void buildSchema() throws IOException { SolrClient solrClient = null; try { solrClient = new CloudSolrClient(solrConfig.getZkHostString()); SchemaRequest schemaRequest = new SchemaRequest(); logger.debug("Downloading schema for collection: {}", collection); SchemaResponse schemaResponse = schemaRequest.process(solrClient, collection); logger.debug("SchemaResponse Schema: {}", schemaResponse); SchemaRepresentation schemaRepresentation = schemaResponse.getSchemaRepresentation(); schema.setName(schemaRepresentation.getName()); schema.setVersion(Float.toString(schemaRepresentation.getVersion())); schema.setUniqueKey(schemaRepresentation.getUniqueKey()); schema.setFieldTypes(getFieldTypes(schemaRepresentation)); schema.setFields(getFields(schemaRepresentation.getFields())); schema.setDynamicFields(getFields(schemaRepresentation.getDynamicFields())); schema.setCopyFields(getCopyFields(schemaRepresentation)); } catch (SolrServerException e) { logger.error("Error while getting schema for collection: {}", collection, e); throw new IOException("Error while getting schema for collection :" + collection, e); } finally { if (solrClient != null) { solrClient.close(); } } }
ZkStateReader zkStateReader = server.getZkStateReader(); try { boolean cont = true;
public SolrClient getSiteReindexServer() { BroadleafRequestContext ctx = BroadleafRequestContext.getBroadleafRequestContext(); Site site = ctx.getNonPersistentSite(); CloudSolrClient client = (CloudSolrClient) primaryServer; client.connect(); String aliasName = getSiteReindexAliasName(site); if (aliasName != null) { String collectionName = getSiteReindexCollectionName(site); createCollectionIfNotExist(client, collectionName); createAliasIfNotExist(client, collectionName, collectionName); } return client; }
/** * {@link SolrJsonMapper} builder class. * @param solrClient {@link SolrClient} implementation from where to extract the default Solr collection, if any defined. * @param jsonTupleField Name of the tuple field that contains the JSON object used to update the Solr index */ public Builder(SolrClient solrClient, String jsonTupleField) { String defaultCollection = null; if (solrClient instanceof CloudSolrClient) { defaultCollection = ((CloudSolrClient) solrClient).getDefaultCollection(); } this.collection = defaultCollection; this.jsonTupleField = jsonTupleField; }
@Override public void clearStorage() throws BackendException { try { if (mode!=Mode.CLOUD) throw new UnsupportedOperationException("Operation only supported for SolrCloud"); logger.debug("Clearing storage from Solr: {}", solrClient); ZkStateReader zkStateReader = ((CloudSolrClient) solrClient).getZkStateReader(); zkStateReader.updateClusterState(true); ClusterState clusterState = zkStateReader.getClusterState(); for (String collection : clusterState.getCollections()) { logger.debug("Clearing collection [{}] in Solr",collection); UpdateRequest deleteAll = newUpdateRequest(); deleteAll.deleteByQuery("*:*"); solrClient.request(deleteAll, collection); } } catch (SolrServerException e) { logger.error("Unable to clear storage from index due to server error on Solr.", e); throw new PermanentBackendException(e); } catch (IOException e) { logger.error("Unable to clear storage from index due to low-level I/O error.", e); throw new PermanentBackendException(e); } catch (Exception e) { logger.error("Unable to clear storage from index due to general error.", e); throw new PermanentBackendException(e); } }
@Override public String getDefaultCollection() { if (solrConfig != null && solrConfig.isSiteCollections()) { Site site = BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite(); return (reindexClient) ? solrConfig.getSiteReindexAliasName(site) : solrConfig.getSiteAliasName(site); } return super.getDefaultCollection(); }
protected void createAliasIfNotExist(CloudSolrClient client, String collectionName, String aliasName) { Aliases aliases = client.getZkStateReader().getAliases(); Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap(); if (!aliasCollectionMap.containsKey(aliasName)) { try { new CollectionAdminRequest.CreateAlias().setAliasName(aliasName) .setAliasedCollections(collectionName).process(client); } catch (SolrServerException e) { throw ExceptionHelper.refineException(e); } catch (IOException e) { throw ExceptionHelper.refineException(e); } } }
protected void createCollectionIfNotExist(CloudSolrClient client, String collectionName) { Set<String> collectionNames = client.getZkStateReader().getClusterState().getCollections(); if (!collectionNames.contains(collectionName)) { try { new CollectionAdminRequest.Create().setCollectionName(collectionName).setNumShards(getSolrCloudNumShards()) .setMaxShardsPerNode(getSolrCloudNumShards()).setConfigName(getSolrCloudConfigName()).process(client); } catch (SolrServerException e) { throw ExceptionHelper.refineException(e); } catch (IOException e) { throw ExceptionHelper.refineException(e); } } }