This is an automated email from the ASF dual-hosted git repository.

jmark99 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new 7e563c8  Add tablename option to shell insert command. (#1771)
7e563c8 is described below

commit 7e563c8e9ef94991efe3fde12f48b2e8e68cd964
Author: Mark Owens <jmar...@apache.org>
AuthorDate: Tue Nov 10 11:51:33 2020 -0500

    Add tablename option to shell insert command. (#1771)
    
    This change adds the --table/-t option to the insert command when inside 
the shell or when using the accumulo command with 'accumulo shell -e <task>'. 
The option is not required, but if used it allows data to be inserted into a 
table in situations where the shell is not within a table context or when in a 
table context to insert data into another table.
    
    Although I would not expect this option to be utilized very often, I found 
myself in need of it when working with the various accumulo examples in the 
accumulo-examples repo. I had many situations where I wished to insert some 
test data from a bash shell using the 'accumulo shell -e insert r f q v'  form 
but this would fail due to there being no table context for the insertion. This 
update allows data inserts from a command line using the 'accumulo shell' 
format.
---
 .../accumulo/shell/commands/InsertCommand.java     |  8 ++--
 .../java/org/apache/accumulo/test/ShellIT.java     | 49 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git 
a/shell/src/main/java/org/apache/accumulo/shell/commands/InsertCommand.java 
b/shell/src/main/java/org/apache/accumulo/shell/commands/InsertCommand.java
index 051e307..283387f 100644
--- a/shell/src/main/java/org/apache/accumulo/shell/commands/InsertCommand.java
+++ b/shell/src/main/java/org/apache/accumulo/shell/commands/InsertCommand.java
@@ -63,7 +63,8 @@ public class InsertCommand extends Command {
   public int execute(final String fullCommand, final CommandLine cl, final 
Shell shellState)
       throws AccumuloException, AccumuloSecurityException, 
TableNotFoundException, IOException,
       ConstraintViolationException {
-    shellState.checkTableState();
+
+    final String tableName = OptUtil.getTableOpt(cl, shellState);
 
     final Mutation m = new Mutation(new 
Text(cl.getArgs()[0].getBytes(Shell.CHARSET)));
     final Text colf = new Text(cl.getArgs()[1].getBytes(Shell.CHARSET));
@@ -105,8 +106,7 @@ public class InsertCommand extends Command {
           throw new IllegalArgumentException("Unknown durability: " + 
userDurability);
       }
     }
-    final BatchWriter bw =
-        
shellState.getAccumuloClient().createBatchWriter(shellState.getTableName(), 
cfg);
+    final BatchWriter bw = 
shellState.getAccumuloClient().createBatchWriter(tableName, cfg);
     bw.addMutation(m);
     try {
       bw.close();
@@ -171,6 +171,8 @@ public class InsertCommand extends Command {
         "durability to use for insert, should be one of \"none\" \"log\" 
\"flush\" or \"sync\"");
     o.addOption(durabilityOption);
 
+    o.addOption(OptUtil.tableOpt("table into which data will be inserted"));
+
     return o;
   }
 
diff --git a/test/src/main/java/org/apache/accumulo/test/ShellIT.java 
b/test/src/main/java/org/apache/accumulo/test/ShellIT.java
index 753aeca..a109f2d 100644
--- a/test/src/main/java/org/apache/accumulo/test/ShellIT.java
+++ b/test/src/main/java/org/apache/accumulo/test/ShellIT.java
@@ -239,6 +239,55 @@ public class ShellIT extends SharedMiniClusterBase {
   }
 
   @Test
+  public void insertIntoSpecifiedTableTest() throws IOException {
+    Shell.log.debug("Starting insertIntoSpecifiedTableTest -----------------");
+    // create two tables for insertion tests
+    exec("createtable tab1", true);
+    exec("createtable tab2", true);
+    // insert data into tab2 while in tab2 context
+    exec("insert row1 f q tab2", true);
+    // insert another with the table and t argument to verify also works
+    exec("insert row2 f q tab2 --table tab2", true);
+    exec("insert row3 f q tab2 -t tab2", true);
+    // leave all table contexts
+    exec("notable", true);
+    // without option cannot insert when not in a table context, also cannot 
add to a table
+    // using 'accumulo shell -e "insert ...." fron command line due to no 
table context being set.
+    exec("insert row1 f q tab1", false, "java.lang.IllegalStateException: Not 
in a table context");
+    // but using option can insert to a table with tablename option without 
being in a table context
+    exec("insert row1 f q tab1 --table tab1", true);
+    exec("insert row4 f q tab2 -t tab2", true);
+    exec("table tab2", true);
+    // can also insert into another table even if a different table context
+    exec("insert row2 f q tab1 -t tab1", true);
+    exec("notable", true);
+    // must supply a tablename if option is used
+    exec("insert row5 f q tab5 --table", false,
+        "org.apache.commons.cli.MissingArgumentException: Missing argument for 
option:");
+    exec("insert row5 f q tab5 --t", false,
+        "org.apache.commons.cli.AmbiguousOptionException: Ambiguous option: 
'--t'");
+    // verify expected data is in both tables
+    exec("scan -t tab1", true, "row1 f:q []    tab1\nrow2 f:q []    tab1");
+    exec("scan -t tab2", true,
+        "row1 f:q []    tab2\nrow2 f:q []    tab2\nrow3 f:q []    tab2\nrow4 
f:q []    tab2");
+    // check that if in table context, inserting into a non-existent table 
does not change context
+    exec("createtable tab3", true);
+    exec("table tab3", true);
+    exec("insert row1 f1 q1 tab3", true);
+    exec("insert row2 f2 q2 tab3 --table idontexist", false,
+        "org.apache.accumulo.core.client.TableNotFoundException:");
+    exec("insert row2 f2 q2 tab3 -t idontexist", false,
+        "org.apache.accumulo.core.client.TableNotFoundException:");
+    exec("insert row3 f3 q3 tab3", true); // should be able to insert w/o 
changing tables
+    // verify expected data is in tab3
+    exec("scan", true, "row1 f1:q1 []    tab3\nrow3 f3:q3 []    tab3");
+    // cleanup
+    exec("deletetable tab1 -f", true, "Table: [tab1] has been deleted");
+    exec("deletetable tab2 -f", true, "Table: [tab2] has been deleted");
+    exec("deletetable tab3 -f", true, "Table: [tab3] has been deleted");
+  }
+
+  @Test
   public void deleteManyTest() throws IOException {
     exec("deletemany", false, "java.lang.IllegalStateException: Not in a table 
context");
     exec("createtable test", true);

Reply via email to