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

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

Introduction

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

Prototype

protected CloudSolrClient(Builder builder) 

Source Link

Document

Create a new client object that connects to Zookeeper and is always aware of the SolrCloud state.

Usage

From source file:FullReindexer.java

License:Apache License

public FullReindexer(String sourceCollection, String destCollection, String zkHost, int totalThreads) {
    this.sourceCollection = sourceCollection;
    this.destCollection = destCollection;
    this.zkHost = zkHost;
    this.totalThreads = totalThreads;

    readClient = new CloudSolrClient(zkHost);
    readClient.setDefaultCollection(sourceCollection);

    writeClient = new CloudSolrClient(zkHost);
    writeClient.setDefaultCollection(destCollection);
}

From source file:bamboo.trove.rule.RuleChangeUpdateManager.java

License:Apache License

@PostConstruct
public void init() {
    log.info("***** RuleChangeUpdateManager *****");
    // The core Trove indexer doesn't really match the model we have here were all of the domains share worker pools,
    // so this startup pattern will look a little odd to align with that view of the world. This domain will configure
    // and init (via statics) the base class all of the other domains extend. They will wait until we are done.
    BaseWarcDomainManager.setBambooApiBaseUrl(bambooBaseUrl);
    BaseWarcDomainManager.setWorkerCounts(maxFilterWorkers, maxTransformWorkers, maxIndexWorkers);
    BaseWarcDomainManager.rankingService = this.rankingService;

    // We must acquire the start lock before letting the other domains complete their init() methods.

    log.info("Solr zk path          : {}", zookeeperConfig);
    log.info("Collection            : {}", collection);
    log.info("Number of workers     : {}", NUMBER_OF_WORKERS);
    log.info("Solr read size        : {}", solrReadSize);
    if (disableRulesUpdates) {
        log.warn("!!! Rule updating is currently disabled by configuration");
    }/*  w ww .  j  a v a 2s  . c o m*/

    client = new CloudSolrClient(zookeeperConfig);
    client.setDefaultCollection(collection);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    workProcessor = new WorkProcessor(NUMBER_OF_WORKERS);
    lastProcessed = restrictionsService.getLastProcessed();

    // Typically this doesn't change, but the 'throughput' domain is experimental
    if (useAsyncSolrClient) {
        EndPointRotator.registerNewEndPoint(solrThroughputDomainManager);
    } else {
        EndPointRotator.registerNewEndPoint(solrManager);
    }

    // Find our initial run state
    boolean runNow = false;
    if (restrictionsService.isInRecovery()) {
        // Nest the if test... we are disabled, but we don't want to go down the 'else' branch
        if (!disableRulesUpdates) {
            log.info("Restart into Rule recovery mode.");
            runNow = true;
        }

    } else {
        long oneDayAgo = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
        if (lastProcessed != null && lastProcessed.getAllCompleted() != null
                && lastProcessed.getAllCompleted().getTime() < oneDayAgo) {
            log.info("Restart into Rule processing mode as last check was more that a day ago.");
            runNow = true;
        } else {
            Date nextRun = nextRunDate();
            Schedule.nextRun(this, nextRun);
        }
    }

    // Start running?
    if (runNow && !disableRulesUpdates) {
        startProcessing();
        // wait until the recovery process has had a chance to get the lock
        while (!hasPassedLock) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // ignore. log at a very low level to avoid IDE objections about empty catch block
                log.trace("Sleep interrupted... sleeping again.", e);
            }
        }
    }
    // Never start this until all the end points are registered
    startMe(filteringService, indexFullText);
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static void setupSolrCloudConfigsets(String zkHost) {
    // before anything else, try to get a zookeeper client
    CloudSolrClient zkClient = new CloudSolrClient(zkHost);

    // get resources and copy them to a temporary directory
    Path databaseDir = null;/*from  w w w .  j a  va 2 s.  c o m*/
    Path tableDir = null;
    Path savedSearchesDir = null;
    try {
        final File jarFile = new File(
                SolrManager.class.getProtectionDomain().getCodeSource().getLocation().toURI());

        // if it is a directory the application in being run from an IDE
        // in that case do not setup (assuming setup is done)
        if (!jarFile.isDirectory()) {
            databaseDir = Files.createTempDirectory("dbv_db_");
            tableDir = Files.createTempDirectory("dbv_tab_");
            savedSearchesDir = Files.createTempDirectory("dbv_tab_");
            final JarFile jar = new JarFile(jarFile);
            final Enumeration<JarEntry> entries = jar.entries();

            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                String name = entry.getName();

                String nameWithoutOriginPart = null;
                Path destination = null;
                if (name.startsWith(ViewerSafeConstants.SOLR_CONFIGSET_DATABASE_RESOURCE + "/")) {
                    nameWithoutOriginPart = name
                            .substring(ViewerSafeConstants.SOLR_CONFIGSET_DATABASE_RESOURCE.length() + 1);
                    destination = databaseDir;
                } else if (name.startsWith(ViewerSafeConstants.SOLR_CONFIGSET_TABLE_RESOURCE + "/")) {
                    nameWithoutOriginPart = name
                            .substring(ViewerSafeConstants.SOLR_CONFIGSET_TABLE_RESOURCE.length() + 1);
                    destination = tableDir;
                } else if (name.startsWith(ViewerSafeConstants.SOLR_CONFIGSET_SEARCHES_RESOURCE + "/")) {
                    nameWithoutOriginPart = name
                            .substring(ViewerSafeConstants.SOLR_CONFIGSET_SEARCHES_RESOURCE.length() + 1);
                    destination = savedSearchesDir;
                } else {
                    continue;
                }

                Path output = destination.resolve(nameWithoutOriginPart);
                if (name.endsWith("/")) {
                    Files.createDirectories(output);
                } else {
                    InputStream inputStream = SolrManager.class.getResourceAsStream("/" + name);
                    output = Files.createFile(output);
                    OutputStream outputStream = Files.newOutputStream(output, StandardOpenOption.CREATE,
                            StandardOpenOption.WRITE);
                    IOUtils.copy(inputStream, outputStream);
                    inputStream.close();
                    outputStream.close();
                }
            }
            jar.close();
        }
    } catch (IOException | URISyntaxException e) {
        LOGGER.error("Could not extract Solr configset", e);
        if (databaseDir != null) {
            try {
                FileUtils.deleteDirectoryRecursive(databaseDir);
            } catch (IOException e1) {
                LOGGER.debug("IO error deleting temporary folder: " + databaseDir, e1);
            }
        }
        if (tableDir != null) {
            try {
                FileUtils.deleteDirectoryRecursive(tableDir);
            } catch (IOException e1) {
                LOGGER.debug("IO error deleting temporary folder: " + tableDir, e1);
            }
        }
        databaseDir = null;
        tableDir = null;
    }

    // copy configurations to solr
    if (databaseDir != null && tableDir != null) {
        try {
            zkClient.uploadConfig(databaseDir, ViewerSafeConstants.SOLR_CONFIGSET_DATABASE);
        } catch (IOException e) {
            LOGGER.debug("IO error uploading database config to solr cloud", e);
        }
        try {
            zkClient.uploadConfig(tableDir, ViewerSafeConstants.SOLR_CONFIGSET_TABLE);
        } catch (IOException e) {
            LOGGER.debug("IO error uploading table config to solr cloud", e);
        }
        try {
            zkClient.uploadConfig(savedSearchesDir, ViewerSafeConstants.SOLR_CONFIGSET_SEARCHES);
        } catch (IOException e) {
            LOGGER.debug("IO error uploading saved searches config to solr cloud", e);
        }

        try {
            FileUtils.deleteDirectoryRecursive(databaseDir);
        } catch (IOException e1) {
            LOGGER.debug("IO error deleting temporary folder: " + databaseDir, e1);
        }
        try {
            FileUtils.deleteDirectoryRecursive(tableDir);
        } catch (IOException e1) {
            LOGGER.debug("IO error deleting temporary folder: " + tableDir, e1);
        }
        try {
            FileUtils.deleteDirectoryRecursive(savedSearchesDir);
        } catch (IOException e1) {
            LOGGER.debug("IO error deleting temporary folder: " + savedSearchesDir, e1);
        }
    }

    try {
        zkClient.close();
    } catch (IOException e) {
        LOGGER.debug("IO error closing connection to solr cloud", e);
    }
}

From source file:com.digitalpebble.storm.crawler.solr.SolrConnection.java

License:Apache License

public static SolrClient getClient(Map stormConf, String boltType) {
    String zkHost = ConfUtils.getString(stormConf, "solr." + boltType + ".zkhost", null);

    String solrUrl = ConfUtils.getString(stormConf, "solr." + boltType + ".url", "localhost");
    String collection = ConfUtils.getString(stormConf, "solr." + boltType + ".collection", null);

    SolrClient client;//  ww w .  j a va2 s.  c o  m

    if (zkHost != null && zkHost.isEmpty() == false) {
        client = new CloudSolrClient(zkHost);
        ((CloudSolrClient) client).setDefaultCollection(collection);
    } else {
        client = new HttpSolrClient(solrUrl);
    }

    return client;
}

From source file:com.francelabs.datafari.service.search.SolrServers.java

License:Apache License

public static SolrClient getSolrServer(Core core) throws Exception {
    // Zookeeper Hosts
    String solrHosts = ScriptConfiguration.getProperty("SOLRHOSTS");
    if (!solrClients.containsKey(core)) {
        try {//from   ww  w.j  av  a 2 s .  c o m
            // TODO : change for ZK ensemble
            CloudSolrClient solrClient = new CloudSolrClient(solrHosts);
            solrClient.setDefaultCollection(core.toString());
            solrClient.setZkClientTimeout(60000);
            SolrPing ping = new SolrPing();
            solrClient.request(ping);
            solrClients.put(core, solrClient);
        } catch (Exception e) {
            // test default param
            try {
                CloudSolrClient solrClient = new CloudSolrClient(defaultURL);
                solrClient.setDefaultCollection(core.toString());
                SolrPing ping = new SolrPing();
                solrClient.request(ping);
                solrClients.put(core, solrClient);
            } catch (Exception e2) {
                LOGGER.error("Cannot instanciate Solr Client for core : " + core.toString(), e);
                throw new Exception("Cannot instanciate Solr Client for core : " + core.toString());
            }
        }
    }
    return solrClients.get(core);
}

From source file:com.github.fengtan.sophie.dialogs.ConnectDialog.java

License:Open Source License

@Override
protected void buttonPressed(int buttonId) {
    if (buttonId != IDialogConstants.OK_ID) {
        super.buttonPressed(buttonId);
        return;//w w  w  . ja  va  2  s  . co m
    }

    // Populate connection label.
    value = combo.getText();

    // Instantiate Solr client based on what the user provided.
    switch (selectedType) {
    case DIRECT_HTTP:
    default:
        client = new HttpSolrClient(combo.getText());
        break;
    case SOLR_CLOUD:
        client = new CloudSolrClient(combo.getText());
        break;
    }

    super.buttonPressed(buttonId);
}

From source file:com.hurence.logisland.service.solr.Solr_5_5_5_ClientService.java

License:Apache License

protected SolrClient createCloudClient(String connectionString, String collection) {
    CloudSolrClient cloudSolrClient = new CloudSolrClient(connectionString);
    cloudSolrClient.setDefaultCollection(collection);
    cloudSolrClient.setZkClientTimeout(30000);
    cloudSolrClient.setZkConnectTimeout(30000);

    return cloudSolrClient;
}

From source file:com.stratio.decision.functions.SaveToSolrActionExecutionFunction.java

License:Apache License

private SolrClient getSolrclient(String core) {
    SolrClient solrClient;// www  .j a  v  a 2  s  .com
    if (solrClients.containsKey(core)) {
        //we have a client for this core
        return solrClients.get(core);
    } else {
        if (isCloud) {
            solrClient = new CloudSolrClient(zkHost);
            ((CloudSolrClient) solrClient).setDefaultCollection(core);
        } else {
            solrClient = new HttpSolrClient("http://" + solrHost + "/solr/" + core);
        }
        solrClients.put(core, solrClient);
    }
    return solrClient;
}

From source file:com.stratio.decision.service.SolrOperationsService.java

License:Apache License

private SolrClient getSolrclient(String core) {
    SolrClient solrClient;//from ww w . ja va2s . c  o  m
    if (isCloud) {
        solrClient = new CloudSolrClient(zkHosts);
    } else {
        solrClient = new HttpSolrClient("http://" + solrHosts + "/solr");
    }
    return solrClient;
}

From source file:com.yahoo.ycsb.db.solr.SolrClient.java

License:Open Source License

/**
 * Initialize any state for this DB. Called once per DB instance; there is one DB instance per
 * client thread.//from  w  w w.  j  av a2s.com
 */
@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));
    }
}