Example usage for org.apache.solr.client.solrj.impl CloudSolrClient connect

List of usage examples for org.apache.solr.client.solrj.impl CloudSolrClient connect

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.impl CloudSolrClient connect.

Prototype

public void connect() 

Source Link

Document

Connect to the zookeeper ensemble.

Usage

From source file:com.frank.search.solr.server.support.HttpSolrClientFactoryBean.java

License:Apache License

private void createCloudClient() {

    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, maxConnections);// 1000
    params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, maxConnectionsPerHost);// 5000
    params.set(HttpClientUtil.PROP_CONNECTION_TIMEOUT, timeout);
    params.set(HttpClientUtil.PROP_SO_TIMEOUT, timeout);
    HttpClient client = HttpClientUtil.createClient(params);
    LBHttpSolrClient lbHttpSolrClient = new LBHttpSolrClient(client);
    CloudSolrClient cloudSolrClient = new CloudSolrClient(url, lbHttpSolrClient);
    if (zkClientTimeout != null) {
        cloudSolrClient.setZkClientTimeout(zkClientTimeout.intValue());
    }/*from  ww w .  j a  v  a  2s.  c  o m*/
    if (zkConnectTimeout != null) {
        cloudSolrClient.setZkConnectTimeout(zkConnectTimeout.intValue());
    }

    if (StringUtils.isNoneBlank(collection)) {
        cloudSolrClient.setDefaultCollection(collection);
    }
    cloudSolrClient.connect();
    this.setSolrClient(cloudSolrClient);
}

From source file:com.indoqa.solr.spring.client.SolrClientFactory.java

License:Apache License

private void initializeCloudSolrServer() {
    this.logger.info("Initializing Cloud Solr client with URL: " + this.url);

    CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder()
            .withZkHost(CloudSolrServerUrlHelper.getConnectString(this.url)).build();
    cloudSolrClient.setDefaultCollection(CloudSolrServerUrlHelper.getCollection(this.url));
    cloudSolrClient.connect();

    this.solrClient = cloudSolrClient;

    this.logger.info("Created Cloud Solr client with URL: " + this.url);
}

From source file:com.thinkaurelius.titan.diskstorage.solr.Solr5Index.java

License:Apache License

public Solr5Index(final Configuration config) throws BackendException {
    Preconditions.checkArgument(config != null);
    configuration = config;/*  ww  w .j a  v  a 2s .co m*/

    mode = Mode.parse(config.get(SOLR_MODE));
    dynFields = config.get(DYNAMIC_FIELDS);
    keyFieldIds = parseKeyFieldsForCollections(config);
    maxResults = config.get(GraphDatabaseConfiguration.INDEX_MAX_RESULT_SET_SIZE);
    ttlField = config.get(TTL_FIELD);
    waitSearcher = config.get(WAIT_SEARCHER);

    if (mode == Mode.CLOUD) {
        String zookeeperUrl = config.get(Solr5Index.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);
    }
}

From source file:de.qaware.chronix.storage.solr.ChronixSolrCloudStorage.java

License:Apache License

/**
 * Returns the list of shards of the default collection.
 *
 * @param zkHost            ZooKeeper URL
 * @param chronixCollection Solr collection name for chronix time series data
 * @return the list of shards of the default collection
 *//*from   w  ww. j av a  2  s  . c  o m*/
public List<String> getShardList(String zkHost, String chronixCollection) throws IOException {

    CloudSolrClient cloudSolrClient = new CloudSolrClient(zkHost);
    List<String> shards = new ArrayList<>();

    try {
        cloudSolrClient.connect();

        ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();

        ClusterState clusterState = zkStateReader.getClusterState();

        String[] collections;
        if (clusterState.hasCollection(chronixCollection)) {
            collections = new String[] { chronixCollection };
        } else {
            // might be a collection alias?
            Aliases aliases = zkStateReader.getAliases();
            String aliasedCollections = aliases.getCollectionAlias(chronixCollection);
            if (aliasedCollections == null)
                throw new IllegalArgumentException("Collection " + chronixCollection + " not found!");
            collections = aliasedCollections.split(",");
        }

        Set<String> liveNodes = clusterState.getLiveNodes();
        Random random = new Random(5150);

        for (String coll : collections) {
            for (Slice slice : clusterState.getSlices(coll)) {
                List<String> replicas = new ArrayList<>();
                for (Replica r : slice.getReplicas()) {
                    if (r.getState().equals(Replica.State.ACTIVE)) {
                        ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(r);
                        if (liveNodes.contains(replicaCoreProps.getNodeName()))
                            replicas.add(replicaCoreProps.getCoreUrl());
                    }
                }
                int numReplicas = replicas.size();
                if (numReplicas == 0)
                    throw new IllegalStateException("Shard " + slice.getName() + " in collection " + coll
                            + " does not have any active replicas!");

                String replicaUrl = (numReplicas == 1) ? replicas.get(0)
                        : replicas.get(random.nextInt(replicas.size()));
                shards.add(replicaUrl);
            }
        }
    } finally {
        cloudSolrClient.close();
    }

    return shards;
}

From source file:org.broadleafcommerce.core.search.service.solr.SolrConfiguration.java

License:Open Source License

@Override
public void afterPropertiesSet() throws SolrServerException, IOException, IllegalStateException {
    if (CloudSolrClient.class.isAssignableFrom(getServer().getClass())) {
        //We want to use the Solr APIs to make sure the correct collections are set up.
        CloudSolrClient primary = (CloudSolrClient) primaryServer;
        CloudSolrClient reindex = (CloudSolrClient) reindexServer;
        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 w  w w .  ja va2s . c  o m*/

        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.");
        }

        //check if the default collection is null
        if (StringUtils.isEmpty(primary.getDefaultCollection())) {
            throw new IllegalStateException(
                    "The primary CloudSolrServer must have a defaultCollection property set.");
        } else {
            this.setPrimaryName(primary.getDefaultCollection());
        }

        //check if the default collection is null
        if (StringUtils.isEmpty(reindex.getDefaultCollection())) {
            throw new IllegalStateException(
                    "The reindex CloudSolrServer must have a defaultCollection property set.");
        } else {
            this.setReindexName(reindex.getDefaultCollection());
        }

        if (Objects.equals(primary.getDefaultCollection(), reindex.getDefaultCollection())) {
            throw new IllegalStateException("The primary and reindex CloudSolrServers must have "
                    + "unique defaultCollection properties.  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
        NamedList<Object> listResponse = new CollectionAdminRequest.List().process(primary).getResponse();
        List<String> collectionNames = listResponse.get("collections") == null
                ? collectionNames = new ArrayList<String>()
                : (List<String>) listResponse.get("collections");

        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;
                }
            }

            new CollectionAdminRequest.Create().setCollectionName(collectionName)
                    .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary);

            new CollectionAdminRequest.CreateAlias().setAliasName(primary.getDefaultCollection())
                    .setAliasedCollections(collectionName).process(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)) {
                new CollectionAdminRequest.Create().setCollectionName(collectionName)
                        .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary);
            }
        }

        //Reload the collection names
        listResponse = new CollectionAdminRequest.List().process(primary).getResponse();
        collectionNames = listResponse.get("collections") == null ? collectionNames = new ArrayList<String>()
                : (List<String>) listResponse.get("collections");

        //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;
                }
            }

            new CollectionAdminRequest.Create().setCollectionName(collectionName)
                    .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary);

            new CollectionAdminRequest.CreateAlias().setAliasName(reindex.getDefaultCollection())
                    .setAliasedCollections(collectionName).process(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)) {
                new CollectionAdminRequest.Create().setCollectionName(collectionName)
                        .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary);
            }
        }
    }
}

From source file:org.codice.solr.factory.impl.SolrCloudClientFactory.java

License:Open Source License

private static SolrClient createSolrCloudClient(String zookeeperHosts, String collection) {

    CloudSolrClient client = new CloudSolrClient(zookeeperHosts);
    client.connect();

    try {//from  ww w.jav  a  2  s  .com
        uploadCoreConfiguration(collection, client);
    } catch (SolrFactoryException e) {
        LOGGER.debug("Unable to upload configuration to Solr Cloud", e);
        return null;
    }

    try {
        createCollection(collection, client);
    } catch (SolrFactoryException e) {
        LOGGER.debug("Unable to create collection on Solr Cloud", e);
        return null;
    }

    client.setDefaultCollection(collection);
    return client;
}

From source file:org.janusgraph.diskstorage.solr.SolrIndex.java

License:Apache License

public SolrIndex(final Configuration config) throws BackendException {
    Preconditions.checkArgument(config != null);
    configuration = config;/* www  .  jav a  2 s.c  o  m*/
    mode = Mode.parse(config.get(SOLR_MODE));
    kerberosEnabled = config.get(KERBEROS_ENABLED);
    dynFields = config.get(DYNAMIC_FIELDS);
    keyFieldIds = parseKeyFieldsForCollections(config);
    batchSize = config.get(INDEX_MAX_RESULT_SET_SIZE);
    ttlField = config.get(TTL_FIELD);
    waitSearcher = config.get(WAIT_SEARCHER);

    if (kerberosEnabled) {
        logger.debug("Kerberos is enabled. Configuring SOLR for Kerberos.");
        configureSolrClientsForKerberos();
    } else {
        logger.debug("Kerberos is NOT enabled.");
        logger.debug("KERBEROS_ENABLED name is " + KERBEROS_ENABLED.getName() + " and it is"
                + (KERBEROS_ENABLED.isOption() ? " " : " not") + " an option.");
        logger.debug("KERBEROS_ENABLED type is " + KERBEROS_ENABLED.getType().name());
    }
    final ModifiableSolrParams clientParams = new ModifiableSolrParams();
    switch (mode) {
    case CLOUD:
        final String[] zookeeperUrl = config.get(SolrIndex.ZOOKEEPER_URL);
        // Process possible zookeeper chroot. e.g. localhost:2181/solr
        // chroot has to be the same assuming one Zookeeper ensemble.
        // Parse from the last string. If found, take it as the chroot.
        String chroot = null;
        for (int i = zookeeperUrl.length - 1; i >= 0; i--) {
            int chrootIndex = zookeeperUrl[i].indexOf("/");
            if (chrootIndex != -1) {
                String hostAndPort = zookeeperUrl[i].substring(0, chrootIndex);
                if (chroot == null) {
                    chroot = zookeeperUrl[i].substring(chrootIndex);
                }
                zookeeperUrl[i] = hostAndPort;
            }
        }
        final CloudSolrClient.Builder builder = new CloudSolrClient.Builder()
                .withLBHttpSolrClientBuilder(new LBHttpSolrClient.Builder()
                        .withHttpSolrClientBuilder(
                                new HttpSolrClient.Builder().withInvariantParams(clientParams))
                        .withBaseSolrUrls(config.get(HTTP_URLS)))
                .withZkHost(Arrays.asList(zookeeperUrl)).sendUpdatesOnlyToShardLeaders();
        if (chroot != null) {
            builder.withZkChroot(chroot);
        }
        final CloudSolrClient cloudServer = builder.build();
        cloudServer.connect();
        solrClient = cloudServer;

        break;
    case HTTP:
        clientParams.add(HttpClientUtil.PROP_ALLOW_COMPRESSION, config.get(HTTP_ALLOW_COMPRESSION).toString());
        clientParams.add(HttpClientUtil.PROP_CONNECTION_TIMEOUT,
                config.get(HTTP_CONNECTION_TIMEOUT).toString());
        clientParams.add(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST,
                config.get(HTTP_MAX_CONNECTIONS_PER_HOST).toString());
        clientParams.add(HttpClientUtil.PROP_MAX_CONNECTIONS,
                config.get(HTTP_GLOBAL_MAX_CONNECTIONS).toString());
        final HttpClient client = HttpClientUtil.createClient(clientParams);
        solrClient = new LBHttpSolrClient.Builder().withHttpClient(client)
                .withBaseSolrUrls(config.get(HTTP_URLS)).build();

        break;
    default:
        throw new IllegalArgumentException("Unsupported Solr operation mode: " + mode);
    }
}