Example usage for org.apache.solr.core CoreContainer CoreContainer

List of usage examples for org.apache.solr.core CoreContainer CoreContainer

Introduction

In this page you can find the example usage for org.apache.solr.core CoreContainer CoreContainer.

Prototype

protected CoreContainer(Object testConstructor) 

Source Link

Document

This method allows subclasses to construct a CoreContainer without any default init behavior.

Usage

From source file:com.datasalt.pangool.solr.SolrRecordWriter.java

License:Apache License

public SolrRecordWriter(int batchSize, boolean outputZipFile, int threadCount, int queueSize,
        String localSolrHome, String zipName, TupleDocumentConverter converter, TaskAttemptContext context) {
    this.localSolrHome = localSolrHome;
    this.zipName = zipName;
    conf = context.getConfiguration();/*ww  w  .  j a  va  2 s  .  c o  m*/
    this.batchSize = batchSize;

    setLogLevel("org.apache.solr.core", "WARN");
    setLogLevel("org.apache.solr.update", "WARN");
    Logger.getLogger("org.apache.solr.core").setLevel(Level.WARN);
    Logger.getLogger("org.apache.solr.update").setLevel(Level.WARN);
    java.util.logging.Logger.getLogger("org.apache.solr.core").setLevel(java.util.logging.Level.WARNING);
    java.util.logging.Logger.getLogger("org.apache.solr.update").setLevel(java.util.logging.Level.WARNING);

    setLogLevel("org.apache.solr", "WARN");
    Logger.getLogger("org.apache.solr").setLevel(Level.WARN);
    java.util.logging.Logger.getLogger("org.apache.solr").setLevel(java.util.logging.Level.WARNING);

    heartBeater = new HeartBeater(context);
    try {
        heartBeater.needHeartBeat();
        /** The actual file in hdfs that holds the configuration. */
        this.outputZipFile = outputZipFile;

        this.fs = FileSystem.get(conf);
        perm = new Path(FileOutputFormat.getOutputPath(context), getOutFileName(context, "part"));

        // Make a task unique name that contains the actual index output name to
        // make debugging simpler
        // Note: if using JVM reuse, the sequence number will not be reset for a
        // new task using the jvm

        Path temp = conf.getLocalPath("mapred.local.dir",
                "solr_" + conf.get("mapred.task.id") + '.' + sequence.incrementAndGet());

        if (outputZipFile && !perm.getName().endsWith(".zip")) {
            perm = perm.suffix(".zip");
        }
        fs.delete(temp, true); // delete old, if any
        fs.delete(perm, true); // delete old, if any
        local = fs.startLocalOutput(perm, temp);

        solrHome = findSolrConfig(conf);

        // }
        // Verify that the solr home has a conf and lib directory
        if (solrHome == null) {
            throw new IOException("Unable to find solr home setting");
        }

        // Setup a solr instance that we can batch writes to
        LOG.info("SolrHome: " + solrHome.toUri());
        String dataDir = new File(local.toString(), "data").getAbsoluteFile().toString();
        // copy the schema to the conf dir
        File confDir = new File(local.toString(), "conf");
        confDir.mkdirs();

        File unpackedSolrHome = new File(solrHome.toString());
        FileUtils.copyDirectory(new File(unpackedSolrHome, "conf"), confDir);

        Properties props = new Properties();
        props.setProperty("solr.data.dir", dataDir);
        props.setProperty("solr.home", solrHome.toString());
        SolrResourceLoader loader = new SolrResourceLoader(solrHome.toString(), null, props);
        LOG.info(String.format(
                "Constructed instance information solr.home %s (%s), instance dir %s, conf dir %s, writing index to temporary directory %s, with permdir %s",
                solrHome, solrHome.toUri(), loader.getInstanceDir(), loader.getConfigDir(), dataDir, perm));
        CoreContainer container = new CoreContainer(loader);
        CoreDescriptor descr = new CoreDescriptor(container, "core1", solrHome.toString());
        descr.setDataDir(dataDir);
        descr.setCoreProperties(props);
        core = container.create(descr);
        container.register(core, false);
        solr = new EmbeddedSolrServer(container, "core1");
        batchWriter = new BatchWriter(solr, batchSize, context.getTaskAttemptID().getTaskID(), threadCount,
                queueSize);

        this.converter = converter;
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        throw new IllegalStateException(String.format("Failed to initialize record writer for %s, %s",
                context.getJobName(), conf.get("mapred.task.id")), e);
    } finally {
        heartBeater.cancelHeartBeat();
    }
}

From source file:com.googlecode.solrgeonames.harvester.Harvester.java

License:Open Source License

/**
 * Start up an embedded Solr server.// ww  w  .  j av  a  2  s. c om
 *
 * @param home: The path to the Solr home directory
 * @return EmbeddedSolrServer: The instantiated server
 * @throws Exception if any errors occur
 */
private EmbeddedSolrServer startSolr(String home) throws Exception {
    try {
        SolrConfig solrConfig = new SolrConfig(home, SOLR_CONFIG, null);
        IndexSchema schema = new IndexSchema(solrConfig, SOLR_SCHEMA, null);

        solrContainer = new CoreContainer(new SolrResourceLoader(SolrResourceLoader.locateSolrHome()));
        CoreDescriptor descriptor = new CoreDescriptor(solrContainer, "",
                solrConfig.getResourceLoader().getInstanceDir());
        descriptor.setConfigName(solrConfig.getResourceName());
        descriptor.setSchemaName(schema.getResourceName());

        solrCore = new SolrCore(null, solrConfig.getDataDir(), solrConfig, schema, descriptor);
        solrContainer.register("", solrCore, false);
        return new EmbeddedSolrServer(solrContainer, "");
    } catch (Exception ex) {
        log.error("\nFailed to start Solr server\n");
        throw ex;
    }
}

From source file:com.googlecode.solrgeonames.server.GeoContextListener.java

License:Open Source License

/**
 * Start up an embedded Solr server./*from w  w  w .j a v  a  2s. co  m*/
 *
 * @param home: The path to the Solr home directory
 * @return EmbeddedSolrServer: The instantiated server
 * @throws Exception if any errors occur
 */
private EmbeddedSolrServer startSolr(String home) throws Exception {
    SolrConfig solrConfig = new SolrConfig(home, SOLR_CONFIG, null);
    IndexSchema schema = new IndexSchema(solrConfig, SOLR_SCHEMA, null);

    solrContainer = new CoreContainer(new SolrResourceLoader(SolrResourceLoader.locateSolrHome()));
    CoreDescriptor descriptor = new CoreDescriptor(solrContainer, "",
            solrConfig.getResourceLoader().getInstanceDir());
    descriptor.setConfigName(solrConfig.getResourceName());
    descriptor.setSchemaName(schema.getResourceName());

    SolrCore solrCore = new SolrCore(null, solrConfig.getDataDir(), solrConfig, schema, descriptor);
    solrContainer.register("", solrCore, false);
    return new EmbeddedSolrServer(solrContainer, "");
}

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

License:Apache License

@Override
protected void before() throws Throwable {

    String base = getClass().getResource("/solr/").getPath();

    FileUtils.deleteDirectory(new File(base + "chronix/data"));
    container = new CoreContainer(base);
    container.load();/*from ww  w  .  j  av  a2 s .c  o m*/

    server = new EmbeddedSolrServer(container, "chronix");

    getClient().deleteByQuery("*:*");
    getClient().commit();
}

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

License:Apache License

public static SolrClient build(String url, String embeddedSolrConfigurationPath) {
    if (new File(embeddedSolrConfigurationPath).exists()) {
        deleteOldCoreProperties(embeddedSolrConfigurationPath);

        SolrResourceLoader loader = new SolrResourceLoader(getNormalizedPath(embeddedSolrConfigurationPath));
        NodeConfig nodeConfig = new NodeConfigBuilder(null, loader).build();
        CoreContainer container = new CoreContainer(nodeConfig);
        container.load();/*w  ww  .j a v  a  2  s  .c o m*/

        Map<String, String> properties = new HashMap<String, String>();
        properties.put(CoreDescriptor.CORE_DATADIR, getNormalizedPath(getDataDir(url)).toString());

        SolrCore core = container.create(CORE_NAME, loader.getInstancePath(), properties);
        return new EmbeddedSolrServer(core);
    }

    // use a temporary directory for the resource loader because Solr needs a real directory for this
    SolrResourceLoader loader = new SolrResourceLoader(createTempDirectory());
    NodeConfig nodeConfig = new NodeConfigBuilder(null, loader).build();
    CoreContainer container = new CoreContainer(nodeConfig);
    container.load();

    Map<String, String> properties = new HashMap<String, String>();
    properties.put(CoreDescriptor.CORE_DATADIR, getNormalizedPath(getDataDir(url)).toString());
    properties.put(CoreDescriptor.CORE_CONFIG, embeddedSolrConfigurationPath + "/conf/solrconfig.xml");
    properties.put(CoreDescriptor.CORE_SCHEMA, embeddedSolrConfigurationPath + "/conf/schema.xml");

    SolrCore core = container.create(CORE_NAME, loader.getInstancePath(), properties);
    return new EmbeddedSolrServer(core);

}

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

private CoreContainer init() throws Exception {

    if (solrHomeDirectory.exists()) {
        FileUtils.deleteDirectory(solrHomeDirectory);
    }//from   w ww. java  2 s  . co m
    assertTrue("Failed to mkdirs workDir", solrHomeDirectory.mkdirs());

    copyMinConf(new File(solrHomeDirectory, "source"));
    File target = new File(solrHomeDirectory, "target");
    copyMinConf(target);
    // modify the target to add SidecarIndexReaderFactory
    File targetSolrConf = new File(target, "conf" + File.separator + "solrconfig.xml");
    String xml = FileUtils.readFileToString(targetSolrConf, "UTF-8");
    xml = xml.replaceFirst("<!-- EDIT_SIDECAR_START", "");
    xml = xml.replaceFirst("EDIT_SIDECAR_END -->", "");
    FileUtils.writeStringToFile(targetSolrConf, xml, "UTF-8");
    File solrXml = new File(solrHomeDirectory, "solr.xml");
    FileUtils.write(solrXml, SOLR_XML, IOUtils.CHARSET_UTF_8.toString());
    final CoreContainer cores = new CoreContainer(solrHomeDirectory.getAbsolutePath());
    cores.load();

    //cores.setPersistent(false);
    return cores;
}

From source file:com.mycompany.sparkrentals.client.RentalSolrClientTest.java

@Before
public void setUp() throws IOException, SolrServerException {
    File resourceDir = new File("src/test/resources/solr");
    //use a temporary folder for solr home
    FileUtils.copyDirectory(resourceDir, tempSolrHome.getRoot());
    CoreContainer coreContainer = new CoreContainer(tempSolrHome.getRoot().getAbsolutePath());
    coreContainer.load();/*  www  . j  av  a 2 s  .co m*/
    server = new EmbeddedSolrServer(coreContainer, "new_core");

    //populate initial data
    List<Rental> rentals = new ArrayList<>();
    rentals.add(new Rental("A1234", "city1", "province2", "country2", "fjkso", "Villa", true, true, true, true,
            10f, "$", 1, new Date()));
    rentals.add(new Rental("A2234", "city2", "province2", "country9", "dfsdd", "studio", true, false, true,
            true, 11f, "$", 2, new Date()));
    rentals.add(new Rental("A3234", "city3", "province5", "country1", "e9r3e", "studio", false, true, false,
            true, 7.2f, "$", 3, new Date()));
    Date threeDaysAgo = new Date(new Date().getTime() - 3 * 24 * 3600 * 1000);
    rentals.add(new Rental("A4234", "city4", "province7", "country6", "ghree", "Single Floor", true, false,
            true, true, 8f, "$", 2, threeDaysAgo));

    for (Rental rental : rentals) {
        server.addBean(rental);
        server.commit();
    }

    client = new RentalSolrClient();
    client.setSolrClient(server);
}

From source file:com.ngdata.hbaseindexer.mr.TestUtils.java

License:Apache License

private static EmbeddedSolrServer createEmbeddedSolrServer(File solrHomeDir, FileSystem fs, Path outputShardDir)
        throws IOException {

    LOG.info("Creating embedded Solr server with solrHomeDir: " + solrHomeDir + ", fs: " + fs
            + ", outputShardDir: " + outputShardDir);

    // copy solrHomeDir to ensure it isn't modified across multiple unit tests or multiple EmbeddedSolrServer instances
    File tmpDir = Files.createTempDir();
    tmpDir.deleteOnExit();/*w ww .j a va2 s .  c o  m*/
    FileUtils.copyDirectory(solrHomeDir, tmpDir);
    solrHomeDir = tmpDir;

    Path solrDataDir = new Path(outputShardDir, "data");

    String dataDirStr = solrDataDir.toUri().toString();

    SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrHomeDir.toString()), null, null);

    LOG.info(String.format(Locale.ENGLISH,
            "Constructed instance information solr.home %s (%s), instance dir %s, conf dir %s, writing index to solr.data.dir %s, with permdir %s",
            solrHomeDir, solrHomeDir.toURI(), loader.getInstancePath(), loader.getConfigDir(), dataDirStr,
            outputShardDir));

    // TODO: This is fragile and should be well documented
    System.setProperty("solr.directoryFactory", HdfsDirectoryFactory.class.getName());
    System.setProperty("solr.lock.type", DirectoryFactory.LOCK_TYPE_HDFS);
    System.setProperty("solr.hdfs.nrtcachingdirectory", "false");
    System.setProperty("solr.hdfs.blockcache.enabled", "false");
    System.setProperty("solr.autoCommit.maxTime", "600000");
    System.setProperty("solr.autoSoftCommit.maxTime", "-1");

    CoreContainer container = new CoreContainer(loader);
    container.load();

    SolrCore core = container.create("core1", Paths.get(solrHomeDir.toString()),
            ImmutableMap.of(CoreDescriptor.CORE_DATADIR, dataDirStr), false);

    if (!(core.getDirectoryFactory() instanceof HdfsDirectoryFactory)) {
        throw new UnsupportedOperationException(
                "Invalid configuration. Currently, the only DirectoryFactory supported is "
                        + HdfsDirectoryFactory.class.getSimpleName());
    }

    EmbeddedSolrServer solr = new EmbeddedSolrServer(container, "core1");
    return solr;
}

From source file:com.searchbox.engine.solr.EmbeddedSolr.java

License:Apache License

@Override
public void afterPropertiesSet() throws Exception {
    if (EmbeddedSolr.coreContainer == null) {
        try {// w  w  w. j  a v  a  2 s. c  om
            LOGGER.info("Embedded solr.solr.home is: " + this.solrHome);
            EmbeddedSolr.coreContainer = new CoreContainer(this.solrHome);
            EmbeddedSolr.coreContainer.load();
        } catch (Exception e) {
            LOGGER.error("Could not start search engine", e);
        }
    }
}

From source file:ddf.catalog.source.solr.SolrServerFactory.java

License:Open Source License

/**
 * Provides an already instantiated {@link SolrServer} object. If an instance has not already
 * been instantiated, then the single instance will be instantiated with the provided
 * configuration file. If an instance already exists, it cannot be overwritten with a new
 * configuration./*from  w w w. j ava 2 s  .c om*/
 * 
 * @param solrConfigXml
 *            the name of the solr configuration filename such as solrconfig.xml
 * @param schemaXml
 *            filename of the schema such as schema.xml
 * @param givenConfigFileProxy
 *            a ConfigurationFileProxy instance. If instance is <code>null</code>, a new
 *            {@link ConfigurationFileProxy} is used instead.
 * @return {@link SolrServer} instance
 */
public static EmbeddedSolrServer getEmbeddedSolrServer(String solrConfigXml, String schemaXml,
        ConfigurationFileProxy givenConfigFileProxy) {

    LOGGER.info("Retrieving embedded solr with the following properties: [" + solrConfigXml + "," + schemaXml
            + "," + givenConfigFileProxy + "]");

    String solrConfigFileName = DEFAULT_SOLRCONFIG_XML;

    String schemaFileName = DEFAULT_SCHEMA_XML;

    if (isNotBlank(solrConfigXml)) {
        solrConfigFileName = solrConfigXml;
    }

    if (isNotBlank(schemaXml)) {
        schemaFileName = schemaXml;
    }

    File solrConfigFile = null;
    File solrConfigHome = null;
    File solrSchemaFile = null;

    ConfigurationFileProxy configProxy = givenConfigFileProxy;

    if (givenConfigFileProxy == null) {
        configProxy = new ConfigurationFileProxy(null, ConfigurationStore.getInstance());
    }

    File configurationDir = new File(ConfigurationFileProxy.DEFAULT_SOLR_CONFIG_PARENT_DIR,
            ConfigurationFileProxy.SOLR_CONFIG_LOCATION_IN_BUNDLE);
    configProxy.writeBundleFilesTo(configurationDir);

    try {
        URL url = configProxy.getResource(solrConfigFileName);

        LOGGER.info("Solr config url: " + url);

        solrConfigFile = new File(new URI(url.toString()).getPath());

        solrConfigHome = new File(solrConfigFile.getParent());
    } catch (URISyntaxException e) {
        LOGGER.warn(e);
    }

    try {
        URL url = configProxy.getResource(schemaFileName);

        LOGGER.info("Solr schema url: " + url);

        solrSchemaFile = new File(new URI(url.toString()).getPath());

    } catch (URISyntaxException e) {
        LOGGER.warn(e);
    }

    SolrConfig solrConfig = null;
    IndexSchema indexSchema = null;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(SolrServerFactory.class.getClassLoader());

        // NamedSPILoader uses the thread context classloader to lookup
        // codecs, posting formats, and analyzers
        solrConfig = new SolrConfig(solrConfigHome.getParent(), solrConfigFileName,
                new InputSource(FileUtils.openInputStream(solrConfigFile)));
        indexSchema = new IndexSchema(solrConfig, schemaFileName,
                new InputSource(FileUtils.openInputStream(solrSchemaFile)));
    } catch (ParserConfigurationException e) {
        LOGGER.warn(e);
    } catch (IOException e) {
        LOGGER.warn(e);
    } catch (SAXException e) {
        LOGGER.warn(e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

    CoreContainer container = new CoreContainer(solrConfigHome.getAbsolutePath());
    container.load();
    CoreDescriptor dcore = new CoreDescriptor(container, "core1",
            solrConfig.getResourceLoader().getInstanceDir());

    File dataDir = configProxy.getDataDirectory();
    LOGGER.info("Using data directory [" + dataDir + "]");
    SolrCore core = new SolrCore("core1", dataDir.getAbsolutePath(), solrConfig, indexSchema, dcore);
    container.register("core1", core, false);

    return new EmbeddedSolrServer(container, "core1");
}