You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by kr...@apache.org on 2019/10/21 14:13:01 UTC

[knox] branch master updated: KNOX-2060 - Extend KnoxShellTable statistics methods to work with columns of Strings (#168)

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

krisden pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new ae65dd8  KNOX-2060 - Extend KnoxShellTable statistics methods to work with columns of Strings (#168)
ae65dd8 is described below

commit ae65dd89b0a18b10db4ec6c250d82704137f2638
Author: Larry McCay IV <46...@users.noreply.github.com>
AuthorDate: Mon Oct 21 10:12:53 2019 -0400

    KNOX-2060 - Extend KnoxShellTable statistics methods to work with columns of Strings (#168)
---
 .../knox/gateway/shell/table/KnoxShellTable.java   | 54 +++++++++++++---------
 .../gateway/shell/table/KnoxShellTableTest.java    | 13 ++++--
 2 files changed, 42 insertions(+), 25 deletions(-)

diff --git a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
index 65fa568..6365a14 100644
--- a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
+++ b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
@@ -42,7 +42,8 @@ public class KnoxShellTable {
         FLOAT,
         BYTE,
         SHORT,
-        LONG
+        LONG,
+        STRING
     }
 
   private static final String LINE_SEPARATOR = System.getProperty("line.separator");
@@ -96,29 +97,37 @@ public class KnoxShellTable {
   }
 
   private Conversions getConversion(Comparable<? extends Object> colIndex) {
-      Conversions type = null;
-      if (colIndex instanceof Double) {
-        type = Conversions.DOUBLE;
-      }
-      else if (colIndex instanceof Integer) {
-        type = Conversions.INTEGER;
-      }
-      else if (colIndex instanceof Float) {
-        type = Conversions.FLOAT;
-      }
-      else if (colIndex instanceof Byte) {
-        type = Conversions.BYTE;
-      }
-      else if (colIndex instanceof Short) {
-        type = Conversions.SHORT;
-      }
-      else if (colIndex instanceof Long) {
-        type = Conversions.LONG;
+    Conversions type = null;
+    if (colIndex instanceof Double) {
+      type = Conversions.DOUBLE;
+    }
+    else if (colIndex instanceof Integer) {
+      type = Conversions.INTEGER;
+    }
+    else if (colIndex instanceof Float) {
+      type = Conversions.FLOAT;
+    }
+    else if (colIndex instanceof Byte) {
+      type = Conversions.BYTE;
+    }
+    else if (colIndex instanceof Short) {
+      type = Conversions.SHORT;
+    }
+    else if (colIndex instanceof Long) {
+      type = Conversions.LONG;
+    }
+    else if (colIndex instanceof String) {
+      if (((String) colIndex).matches("-?\\d+(\\.\\d+)?")) {
+          type = Conversions.STRING;
       }
       else {
-          throw new IllegalArgumentException();
+        throw new IllegalArgumentException("String contains non-numeric characters");
       }
-      return type;
+    }
+    else {
+        throw new IllegalArgumentException("Unsupported data type");
+    }
+    return type;
   }
 
   private double[] toDoubleArray(String colName) throws IllegalArgumentException {
@@ -148,6 +157,9 @@ public class KnoxShellTable {
         case LONG:
           colArray[i] = (double) ((Long) col.get(i)).longValue();
           break;
+        case STRING:
+          colArray[i] = (double) (Double.parseDouble((String) col.get(i)));
+          break;
       }
     }
     return colArray;
diff --git a/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java b/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
index 1d0952c..5a7b282 100644
--- a/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
+++ b/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
@@ -385,11 +385,15 @@ public class KnoxShellTableTest {
       Long long20 = (long) 200000000;
       Long long30 = (long) 300000000;
 
+      String string10 = "1023.98000000000";
+      String string20 = "2089743.6";
+      String string30 = "-3";
+
       KnoxShellTable TABLE = new KnoxShellTable();
-      TABLE.header("Column Integer").header("Column Double").header("Column Float").header("Column Byte").header("Column Short").header("Column Long");
-      TABLE.row().value(10).value(20d).value(20.9999).value(byte10).value(short10).value(long10);
-      TABLE.row().value(30).value(40d).value(40.9999).value(byte20).value(short20).value(long20);
-      TABLE.row().value(27).value(60d).value(60.9999).value(byte30).value(short30).value(long30);
+      TABLE.header("Column Integer").header("Column Double").header("Column Float").header("Column Byte").header("Column Short").header("Column Long").header("Column String");
+      TABLE.row().value(10).value(20d).value(20.9999).value(byte10).value(short10).value(long10).value(string10);
+      TABLE.row().value(30).value(40d).value(40.9999).value(byte20).value(short20).value(long20).value(string20);
+      TABLE.row().value(27).value(60d).value(60.9999).value(byte30).value(short30).value(long30).value(string30);
 
       assertEquals(22.3, TABLE.mean("Column Integer"), 0.1);
       assertEquals(40, TABLE.mean("Column Double"), 0.1);
@@ -400,6 +404,7 @@ public class KnoxShellTableTest {
       assertEquals(20, TABLE.mean("Column Byte"), 0.1);
       assertEquals(20, TABLE.mean("Column Short"), 0.1);
       assertEquals(2.0E8, TABLE.mean("Column Long"), 0.1);
+      assertEquals(696921.52666666, TABLE.mean("Column String"), 0.1);
   }
 
   @Test