Class SolrImportExport


  • public class SolrImportExport
    extends Object
    Utility class to export, clear and import Solr indexes.
    Author:
    Andrea Schweer schweer@waikato.ac.nz for the LCoNZ Institutional Research Repositories
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ROWS_PER_FILE  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clearIndex​(String solrUrl)
      Remove all documents from the Solr index with the given URL, then commit and optimise the index.
      static void exportIndex​(String indexName, File toDir, String solrUrl, String timeField, boolean overwrite)
      Exports all documents in the given index to the specified target directory in batches of #ROWS_PER_FILE.
      static void exportIndex​(String indexName, File toDir, String solrUrl, String timeField, String fromWhen, boolean overwrite)
      Exports documents from the given index to the specified target directory in batches of #ROWS_PER_FILE, starting at fromWhen (or all documents).
      static void importIndex​(String indexName, File fromDir, String solrUrl, boolean clear)
      Import previously exported documents (or externally created CSV files that have the appropriate structure) into the specified index.
      static void main​(String[] args)
      Entry point for command-line invocation
    • Method Detail

      • main

        public static void main​(String[] args)
                         throws org.apache.commons.cli.ParseException
        Entry point for command-line invocation
        Parameters:
        args - command-line arguments; see help for description
        Throws:
        org.apache.commons.cli.ParseException - if the command-line arguments cannot be parsed
      • exportIndex

        public static void exportIndex​(String indexName,
                                       File toDir,
                                       String solrUrl,
                                       String timeField,
                                       boolean overwrite)
                                throws org.apache.solr.client.solrj.SolrServerException,
                                       SolrImportExportException,
                                       IOException
        Exports all documents in the given index to the specified target directory in batches of #ROWS_PER_FILE. See #makeExportFilename for the file names that are generated.
        Parameters:
        indexName - The index to export.
        toDir - The target directory for the export. Will be created if it doesn't exist yet. The directory must be writeable.
        solrUrl - The solr URL for the index to export. Must not be null.
        timeField - The time field to use for sorting the export. Must not be null.
        overwrite - If set, allow export files to be overwritten
        Throws:
        org.apache.solr.client.solrj.SolrServerException - if there is a problem with exporting the index.
        IOException - if there is a problem creating the files or communicating with Solr.
        SolrImportExportException - if there is a problem in communicating with Solr.
      • importIndex

        public static void importIndex​(String indexName,
                                       File fromDir,
                                       String solrUrl,
                                       boolean clear)
                                throws IOException,
                                       org.apache.solr.client.solrj.SolrServerException,
                                       SolrImportExportException
        Import previously exported documents (or externally created CSV files that have the appropriate structure) into the specified index.
        Parameters:
        indexName - the index to import.
        fromDir - the source directory. Must exist and be readable. The importer will look for files whose name starts with
        indexName
        and ends with .csv (to match what is generated by #makeExportFilename).
        solrUrl - The solr URL for the index to export. Must not be null.
        clear - if true, clear the index before importing.
        overwrite - if true, skip _version_ field on import to disable Solr's optimistic concurrency functionality
        Throws:
        IOException - if there is a problem reading the files or communicating with Solr.
        org.apache.solr.client.solrj.SolrServerException - if there is a problem reading the files or communicating with Solr.
        SolrImportExportException - if there is a problem communicating with Solr.
      • clearIndex

        public static void clearIndex​(String solrUrl)
                               throws IOException,
                                      org.apache.solr.client.solrj.SolrServerException
        Remove all documents from the Solr index with the given URL, then commit and optimise the index.
        Parameters:
        solrUrl - URL of the Solr core to clear.
        Throws:
        IOException - if there is a problem in communicating with Solr.
        org.apache.solr.client.solrj.SolrServerException - if there is a problem in communicating with Solr.
      • exportIndex

        public static void exportIndex​(String indexName,
                                       File toDir,
                                       String solrUrl,
                                       String timeField,
                                       String fromWhen,
                                       boolean overwrite)
                                throws org.apache.solr.client.solrj.SolrServerException,
                                       IOException,
                                       SolrImportExportException
        Exports documents from the given index to the specified target directory in batches of #ROWS_PER_FILE, starting at fromWhen (or all documents). See #makeExportFilename for the file names that are generated.
        Parameters:
        indexName - The index to export.
        toDir - The target directory for the export. Will be created if it doesn't exist yet. The directory must be writeable.
        solrUrl - The solr URL for the index to export. Must not be null.
        timeField - The time field to use for sorting the export. Must not be null.
        fromWhen - Optionally, from when to export. See options for allowed values. If null or empty, all documents will be exported.
        overwrite - If set, allow export files to be overwritten
        Throws:
        org.apache.solr.client.solrj.SolrServerException - if there is a problem with exporting the index.
        IOException - if there is a problem creating the files or communicating with Solr.
        SolrImportExportException - if there is a problem in communicating with Solr.