package mil.nga.giat.geowave.core.ingest.operations; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; import com.beust.jcommander.Parameter; import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters; import com.beust.jcommander.ParametersDelegate; import mil.nga.giat.geowave.core.cli.annotations.GeowaveOperation; import mil.nga.giat.geowave.core.cli.api.Command; import mil.nga.giat.geowave.core.cli.api.DefaultOperation; import mil.nga.giat.geowave.core.cli.api.OperationParams; import mil.nga.giat.geowave.core.ingest.local.LocalFileIngestDriver; import mil.nga.giat.geowave.core.ingest.local.LocalFileIngestPlugin; import mil.nga.giat.geowave.core.ingest.local.LocalInputCommandLineOptions; import mil.nga.giat.geowave.core.ingest.operations.options.IngestFormatPluginOptions; import mil.nga.giat.geowave.core.store.operations.remote.options.DataStorePluginOptions; import mil.nga.giat.geowave.core.store.operations.remote.options.IndexLoader; import mil.nga.giat.geowave.core.store.operations.remote.options.IndexPluginOptions; import mil.nga.giat.geowave.core.store.operations.remote.options.StoreLoader; import mil.nga.giat.geowave.core.store.operations.remote.options.VisibilityOptions; @GeowaveOperation(name = "localToGW", parentOperation = IngestSection.class) @Parameters(commandDescription = "Ingest supported files in local file system directly, without using HDFS") public class LocalToGeowaveCommand extends DefaultOperation implements Command { @Parameter(description = "<file or directory> <storename> <comma delimited index/group list>") private List<String> parameters = new ArrayList<String>(); @ParametersDelegate private VisibilityOptions ingestOptions = new VisibilityOptions(); @ParametersDelegate private LocalInputCommandLineOptions localInputOptions = new LocalInputCommandLineOptions(); // This helper is used to load the list of format SPI plugins that will be // used @ParametersDelegate private IngestFormatPluginOptions pluginFormats = new IngestFormatPluginOptions(); @Parameter(names = { "-t", "--threads" }, description = "number of threads to use for ingest, default to 1 (optional)") private int threads = 1; private DataStorePluginOptions inputStoreOptions = null; private List<IndexPluginOptions> inputIndexOptions = null; @Override public boolean prepare( OperationParams params ) { super.prepare(params); // Based on the selected formats, select the format plugins pluginFormats.selectPlugin(localInputOptions.getFormats()); return true; } /** * Prep the driver & run the operation. */ @Override public void execute( OperationParams params ) { // Ensure we have all the required arguments if (parameters.size() != 3) { throw new ParameterException( "Requires arguments: <file or directory> <storename> <comma delimited index/group list>"); } String inputPath = parameters.get(0); String inputStoreName = parameters.get(1); String indexList = parameters.get(2); // Config file File configFile = getGeoWaveConfigFile(params); // Attempt to load input store. if (inputStoreOptions == null) { StoreLoader inputStoreLoader = new StoreLoader( inputStoreName); if (!inputStoreLoader.loadFromConfig(configFile)) { throw new ParameterException( "Cannot find store name: " + inputStoreLoader.getStoreName()); } inputStoreOptions = inputStoreLoader.getDataStorePlugin(); } // Load the Indexes if (inputIndexOptions == null) { IndexLoader indexLoader = new IndexLoader( indexList); if (!indexLoader.loadFromConfig(configFile)) { throw new ParameterException( "Cannot find index(s) by name: " + indexList); } inputIndexOptions = indexLoader.getLoadedIndexes(); } // Ingest Plugins Map<String, LocalFileIngestPlugin<?>> ingestPlugins = pluginFormats.createLocalIngestPlugins(); // Driver LocalFileIngestDriver driver = new LocalFileIngestDriver( inputStoreOptions, inputIndexOptions, ingestPlugins, ingestOptions, localInputOptions, threads); // Execute if (!driver.runOperation(inputPath)) { throw new RuntimeException( "Ingest failed to execute"); } } public List<String> getParameters() { return parameters; } public void setParameters( String fileOrDirectory, String storeName, String commaDelimitedIndexes ) { parameters = new ArrayList<String>(); parameters.add(fileOrDirectory); parameters.add(storeName); parameters.add(commaDelimitedIndexes); } public VisibilityOptions getIngestOptions() { return ingestOptions; } public void setIngestOptions( VisibilityOptions ingestOptions ) { this.ingestOptions = ingestOptions; } public LocalInputCommandLineOptions getLocalInputOptions() { return localInputOptions; } public void setLocalInputOptions( LocalInputCommandLineOptions localInputOptions ) { this.localInputOptions = localInputOptions; } public IngestFormatPluginOptions getPluginFormats() { return pluginFormats; } public void setPluginFormats( IngestFormatPluginOptions pluginFormats ) { this.pluginFormats = pluginFormats; } public int getThreads() { return threads; } public void setThreads( int threads ) { this.threads = threads; } public DataStorePluginOptions getInputStoreOptions() { return inputStoreOptions; } public void setInputStoreOptions( DataStorePluginOptions inputStoreOptions ) { this.inputStoreOptions = inputStoreOptions; } public List<IndexPluginOptions> getInputIndexOptions() { return inputIndexOptions; } public void setInputIndexOptions( List<IndexPluginOptions> inputIndexOptions ) { this.inputIndexOptions = inputIndexOptions; } }