From 3cbd9a6537df45f0625e1892688908339e097f30 Mon Sep 17 00:00:00 2001 From: Adeet Patel Date: Wed, 24 Jan 2024 18:52:44 -0500 Subject: [PATCH] GEOMESA-3293 Provide error message if trying to ingest from stdin without a converter (#3035) --- .../tools/ingest/IngestCommandTest.scala | 49 +++++++++++++++++++ .../geomesa/tools/ingest/IngestCommand.scala | 5 ++ 2 files changed, 54 insertions(+) diff --git a/geomesa-accumulo/geomesa-accumulo-tools/src/test/scala/org/locationtech/geomesa/accumulo/tools/ingest/IngestCommandTest.scala b/geomesa-accumulo/geomesa-accumulo-tools/src/test/scala/org/locationtech/geomesa/accumulo/tools/ingest/IngestCommandTest.scala index fd8847590272..9ac762d1e6ff 100644 --- a/geomesa-accumulo/geomesa-accumulo-tools/src/test/scala/org/locationtech/geomesa/accumulo/tools/ingest/IngestCommandTest.scala +++ b/geomesa-accumulo/geomesa-accumulo-tools/src/test/scala/org/locationtech/geomesa/accumulo/tools/ingest/IngestCommandTest.scala @@ -8,6 +8,7 @@ package org.locationtech.geomesa.accumulo.tools.ingest +import com.beust.jcommander.ParameterException import com.typesafe.config.{ConfigFactory, ConfigRenderOptions} import org.apache.commons.io.IOUtils import org.junit.runner.RunWith @@ -112,6 +113,54 @@ class IngestCommandTest extends Specification { } } + "fail to ingest from stdin if no converter is specified" in { + val confFile = new File(getClass.getClassLoader.getResource("examples/example1-csv.conf").getFile) + val dataFile = WithClose(getClass.getClassLoader.getResourceAsStream("examples/example1.csv")) { in => + IOUtils.toByteArray(in) + } + val input = new ByteArrayInputStream(dataFile) + + val args = baseArgs ++ Array("-s", confFile.getPath, "-") + + val command = AccumuloRunner.parseCommand(args).asInstanceOf[AccumuloDataStoreCommand] + + val in = System.in + in.synchronized { + try { + System.setIn(input) + command.execute() must throwA[ParameterException].like { + case e => e.getMessage mustEqual "Cannot infer types from stdin - please specify a converter/sft or ingest from a file" + } + } finally { + System.setIn(in) + } + } + } + + "fail to ingest from stdin if no sft is specified" in { + val confFile = new File(getClass.getClassLoader.getResource("examples/example1-csv.conf").getFile) + val dataFile = WithClose(getClass.getClassLoader.getResourceAsStream("examples/example1.csv")) { in => + IOUtils.toByteArray(in) + } + val input = new ByteArrayInputStream(dataFile) + + val args = baseArgs ++ Array("--converter", confFile.getPath, "-") + + val command = AccumuloRunner.parseCommand(args).asInstanceOf[AccumuloDataStoreCommand] + + val in = System.in + in.synchronized { + try { + System.setIn(input) + command.execute() must throwA[ParameterException].like { + case e => e.getMessage mustEqual "SimpleFeatureType name and/or specification argument is required" + } + } finally { + System.setIn(in) + } + } + } + "not ingest csv to tsv " in { val confFile = new File(getClass.getClassLoader.getResource("examples/example1-tsv.conf").getFile) val dataFile = new File(getClass.getClassLoader.getResource("examples/example1.csv").getFile) diff --git a/geomesa-tools/src/main/scala/org/locationtech/geomesa/tools/ingest/IngestCommand.scala b/geomesa-tools/src/main/scala/org/locationtech/geomesa/tools/ingest/IngestCommand.scala index 98e335c33b06..01ae1707b49a 100644 --- a/geomesa-tools/src/main/scala/org/locationtech/geomesa/tools/ingest/IngestCommand.scala +++ b/geomesa-tools/src/main/scala/org/locationtech/geomesa/tools/ingest/IngestCommand.scala @@ -239,6 +239,11 @@ object IngestCommand extends LazyLogging { var converter: Config = Option(params.config).map(CLArgResolver.getConfig).orNull if (converter == null && inputs.nonEmpty) { + // ingesting from stdin without specifying a converter is not supported + if (inputs == Inputs.StdInInputs) { + throw new ParameterException("Cannot infer types from stdin - please specify a converter/sft or ingest from a file") + } + // if there is no converter passed in, try to infer the schema from the input files themselves Command.user.info("No converter defined - will attempt to detect schema from input files") val file = inputs.iterator.flatMap(PathUtils.interpretPath).headOption.getOrElse {