sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cheol...@apache.org
Subject git commit: SQOOP-643: Implement simple listings for connector and job objects rek
Date Wed, 23 Jan 2013 01:36:19 GMT
Updated Branches:
  refs/heads/sqoop2 5be8eb680 -> 03408d573


SQOOP-643: Implement simple listings for connector and job objects rek

(Jarcec Cecho via Cheolsoo Park)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/03408d57
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/03408d57
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/03408d57

Branch: refs/heads/sqoop2
Commit: 03408d573e7edcc605e06ce2d253cb01f486ef21
Parents: 5be8eb6
Author: Cheolsoo Park <cheolsoo@apache.org>
Authored: Tue Jan 22 16:50:39 2013 -0800
Committer: Cheolsoo Park <cheolsoo@apache.org>
Committed: Tue Jan 22 16:50:39 2013 -0800

----------------------------------------------------------------------
 .../org/apache/sqoop/client/core/Constants.java    |   14 ++
 .../sqoop/client/shell/ShowConnectionFunction.java |   33 +++-
 .../sqoop/client/shell/ShowConnectorFunction.java  |   36 +++-
 .../apache/sqoop/client/shell/ShowJobFunction.java |   36 +++-
 .../apache/sqoop/client/utils/TableDisplayer.java  |  141 +++++++++++++++
 .../src/main/resources/client-resource.properties  |    8 +
 6 files changed, 247 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/03408d57/client/src/main/java/org/apache/sqoop/client/core/Constants.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/core/Constants.java b/client/src/main/java/org/apache/sqoop/client/core/Constants.java
index 8ede029..c364aa8 100644
--- a/client/src/main/java/org/apache/sqoop/client/core/Constants.java
+++ b/client/src/main/java/org/apache/sqoop/client/core/Constants.java
@@ -300,6 +300,20 @@ public class Constants {
       "update.job";
   public static final String RES_UPDATE_JOB_SUCCESSFUL =
       "update.job_successful";
+
+  public static final String RES_TABLE_HEADER_ID =
+      "table.header.id";
+  public static final String RES_TABLE_HEADER_NAME =
+      "table.header.name";
+  public static final String RES_TABLE_HEADER_VERSION =
+      "table.header.version";
+  public static final String RES_TABLE_HEADER_CLASS =
+      "table.header.class";
+  public static final String RES_TABLE_HEADER_TYPE =
+      "table.header.type";
+  public static final String RES_TABLE_HEADER_CONNECTOR =
+      "table.header.connector";
+
   private Constants() {
     // Instantiation is prohibited
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/03408d57/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectionFunction.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectionFunction.java
b/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectionFunction.java
index 8066789..af325d5 100644
--- a/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectionFunction.java
+++ b/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectionFunction.java
@@ -20,6 +20,7 @@ package org.apache.sqoop.client.shell;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.sqoop.client.core.Constants;
+import org.apache.sqoop.client.utils.TableDisplayer;
 import org.apache.sqoop.json.ConnectionBean;
 import org.apache.sqoop.model.MConnection;
 import org.codehaus.groovy.tools.shell.IO;
@@ -27,6 +28,7 @@ import org.codehaus.groovy.tools.shell.IO;
 import java.io.PrintWriter;
 import java.text.DateFormat;
 import java.text.MessageFormat;
+import java.util.LinkedList;
 import java.util.List;
 
 import static org.apache.sqoop.client.utils.FormDisplayer.*;
@@ -63,23 +65,40 @@ public class ShowConnectionFunction extends SqoopFunction {
   }
 
   public Object execute(List<String> args) {
-    if (args.size() == 1) {
-      printHelp(io.out);
-      io.out.println();
-      return null;
-    }
-
     CommandLine line = parseOptions(this, 1, args);
     if (line.hasOption(Constants.OPT_ALL)) {
       showConnection(null);
-
     } else if (line.hasOption(Constants.OPT_XID)) {
       showConnection(line.getOptionValue(Constants.OPT_XID));
+    } else {
+      showSummary();
     }
 
     return null;
   }
 
+  private void showSummary() {
+    ConnectionBean connectionBean = readConnection(null);
+    List<MConnection> connections = connectionBean.getConnections();
+
+    List<String> header = new LinkedList<String>();
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_ID));
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_NAME));
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_CONNECTOR));
+
+    List<String> ids = new LinkedList<String>();
+    List<String> names = new LinkedList<String>();
+    List<String> connectors = new LinkedList<String>();
+
+    for(MConnection connection : connections) {
+      ids.add(String.valueOf(connection.getPersistenceId()));
+      names.add(connection.getName());
+      connectors.add(String.valueOf(connection.getConnectorId()));
+    }
+
+    TableDisplayer.display(io, header, ids, names, connectors);
+  }
+
   private void showConnection(String xid) {
     ConnectionBean connectionBean = readConnection(xid);
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/03408d57/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
b/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
index 6276b3e..c2464d2 100644
--- a/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
+++ b/client/src/main/java/org/apache/sqoop/client/shell/ShowConnectorFunction.java
@@ -19,6 +19,7 @@ package org.apache.sqoop.client.shell;
 
 import java.io.PrintWriter;
 import java.text.MessageFormat;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.ResourceBundle;
@@ -27,6 +28,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.sqoop.client.core.Constants;
+import org.apache.sqoop.client.utils.TableDisplayer;
 import org.apache.sqoop.json.ConnectorBean;
 import org.apache.sqoop.model.MConnector;
 import org.codehaus.groovy.tools.shell.IO;
@@ -63,23 +65,43 @@ public class ShowConnectorFunction extends SqoopFunction
   }
 
   public Object execute(List<String> args) {
-    if (args.size() == 1) {
-      printHelp(io.out);
-      io.out.println();
-      return null;
-    }
-
     CommandLine line = parseOptions(this, 1, args);
     if (line.hasOption(Constants.OPT_ALL)) {
       showConnector(null);
-
     } else if (line.hasOption(Constants.OPT_CID)) {
       showConnector(line.getOptionValue(Constants.OPT_CID));
+    } else {
+      showSummary();
     }
 
     return null;
   }
 
+  private void showSummary() {
+    ConnectorBean connectorBean = readConnector(null);
+    List<MConnector> connectors = connectorBean.getConnectors();
+
+    List<String> header = new LinkedList<String>();
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_ID));
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_NAME));
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_VERSION));
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_CLASS));
+
+    List<String> ids = new LinkedList<String>();
+    List<String> uniqueNames = new LinkedList<String>();
+    List<String> versions = new LinkedList<String>();
+    List<String> classes = new LinkedList<String>();
+
+    for(MConnector connector : connectors) {
+      ids.add(String.valueOf(connector.getPersistenceId()));
+      uniqueNames.add(connector.getUniqueName());
+      versions.add(connector.getVersion());
+      classes.add(connector.getClassName());
+    }
+
+    TableDisplayer.display(io, header, ids, uniqueNames, versions, classes);
+  }
+
   private void showConnector(String cid) {
     ConnectorBean connectorBean = readConnector(cid);
     List<MConnector> connectors = connectorBean.getConnectors();

http://git-wip-us.apache.org/repos/asf/sqoop/blob/03408d57/client/src/main/java/org/apache/sqoop/client/shell/ShowJobFunction.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/shell/ShowJobFunction.java b/client/src/main/java/org/apache/sqoop/client/shell/ShowJobFunction.java
index 851ddca..0d67133 100644
--- a/client/src/main/java/org/apache/sqoop/client/shell/ShowJobFunction.java
+++ b/client/src/main/java/org/apache/sqoop/client/shell/ShowJobFunction.java
@@ -20,6 +20,7 @@ package org.apache.sqoop.client.shell;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.sqoop.client.core.Constants;
+import org.apache.sqoop.client.utils.TableDisplayer;
 import org.apache.sqoop.json.JobBean;
 import org.apache.sqoop.model.MJob;
 import org.codehaus.groovy.tools.shell.IO;
@@ -27,6 +28,7 @@ import org.codehaus.groovy.tools.shell.IO;
 import java.io.PrintWriter;
 import java.text.DateFormat;
 import java.text.MessageFormat;
+import java.util.LinkedList;
 import java.util.List;
 
 import static org.apache.sqoop.client.utils.FormDisplayer.*;
@@ -61,23 +63,43 @@ public class ShowJobFunction extends SqoopFunction {
   }
 
   public Object execute(List<String> args) {
-    if (args.size() == 1) {
-      printHelp(io.out);
-      io.out.println();
-      return null;
-    }
-
     CommandLine line = parseOptions(this, 1, args);
     if (line.hasOption(Constants.OPT_ALL)) {
       showJob(null);
-
     } else if (line.hasOption(Constants.OPT_JID)) {
       showJob(line.getOptionValue(Constants.OPT_JID));
+    } else {
+      showSummary();
     }
 
     return null;
   }
 
+  private void showSummary() {
+    JobBean jobBean = readJob(null);
+    List<MJob> jobs = jobBean.getJobs();
+
+    List<String> header = new LinkedList<String>();
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_ID));
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_NAME));
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_TYPE));
+    header.add(getResource().getString(Constants.RES_TABLE_HEADER_CONNECTOR));
+
+    List<String> ids = new LinkedList<String>();
+    List<String> names = new LinkedList<String>();
+    List<String> types = new LinkedList<String>();
+    List<String> connectors = new LinkedList<String>();
+
+    for(MJob job : jobs) {
+      ids.add(String.valueOf(job.getPersistenceId()));
+      names.add(job.getName());
+      types.add(job.getType().toString());
+      connectors.add(String.valueOf(job.getConnectorId()));
+    }
+
+    TableDisplayer.display(io, header, ids, names, types, connectors);
+  }
+
   private void showJob(String jid) {
     JobBean jobBean = readJob(jid);
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/03408d57/client/src/main/java/org/apache/sqoop/client/utils/TableDisplayer.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/utils/TableDisplayer.java b/client/src/main/java/org/apache/sqoop/client/utils/TableDisplayer.java
new file mode 100644
index 0000000..63d2ef4
--- /dev/null
+++ b/client/src/main/java/org/apache/sqoop/client/utils/TableDisplayer.java
@@ -0,0 +1,141 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sqoop.client.utils;
+
+import org.apache.commons.lang.StringUtils;
+import org.codehaus.groovy.tools.shell.IO;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Display table based data
+ */
+public class TableDisplayer {
+
+  /**
+   * Display given columns in nice table structure to given IO object.
+   *
+   * @param io Shell's IO object
+   * @param headers List of headers
+   * @param columns Array of columns
+   */
+  public static void display(IO io, List<String> headers, List<String> ...columns)
{
+
+    assert io != null;
+    assert headers != null;
+    assert columns != null;
+    assert headers.size() == columns.length;
+
+    // Count of columns
+    int columnCount = headers.size();
+
+    // List of all maximal widths of each column
+    List<Integer> widths = new LinkedList<Integer>();
+    for(int i = 0; i < columnCount; i++) {
+      widths.add(getMaximalWidth(headers.get(i), columns[i]));
+    }
+
+    // First line is border
+    drawLine(io, widths);
+
+    // Print out header (text is centralised)
+    io.out.print("| ");
+    for(int i = 0 ; i < columnCount; i++) {
+      io.out.print(StringUtils.center(headers.get(i), widths.get(i), ' '));
+      io.out.print((i == columnCount -1) ? " |" : " | ");
+    }
+    io.out.println();
+
+    // End up header by border
+    drawLine(io, widths);
+
+    // Number of rows in the table
+    int rows = getMaximalRows(columns);
+
+    // Print out each row
+    for(int row = 0 ; row < rows; row++) {
+      io.out.print("| ");
+      for(int i = 0 ; i < columnCount; i++) {
+        io.out.print(StringUtils.rightPad(columns[i].get(row), widths.get(i), ' '));
+        io.out.print((i == columnCount -1) ? " |" : " | ");
+      }
+      io.out.println();
+    }
+
+    // End table by final border
+    drawLine(io, widths);
+  }
+
+  /**
+   * Draw border line
+   *
+   * @param io Shell's associated IO object
+   * @param widths List of widths of each column
+   */
+  private static void drawLine(IO io, List<Integer> widths) {
+    int last = widths.size() - 1;
+    io.out.print("+-");
+    for(int i = 0; i < widths.size(); i++) {
+      io.out.print(StringUtils.repeat("-", widths.get(i)));
+      io.out.print((i == last) ? "-+" : "-+-");
+    }
+    io.out.println();
+  }
+
+  /**
+   * Get maximal width for given column with it's associated header.
+   *
+   * @param header Associated header
+   * @param column All column values
+   * @return Maximal
+   */
+  private static int getMaximalWidth(String header, List<String> column) {
+    int max = header.length();
+
+    for(String value : column) {
+      if(value.length() > max) {
+        max = value.length();
+      }
+    }
+
+    return max;
+  }
+
+  /**
+   * Get maximal number of rows available in the column list
+   *
+   * @param columns Array with all column values
+   * @return
+   */
+  private static int getMaximalRows(List<String>... columns) {
+    int max = 0;
+
+    for(List<String> column : columns) {
+      if(column.size() > max) {
+        max = column.size();
+      }
+    }
+
+    return max;
+  }
+
+  private TableDisplayer() {
+    // Instantiation is prohibited
+  }
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/03408d57/client/src/main/resources/client-resource.properties
----------------------------------------------------------------------
diff --git a/client/src/main/resources/client-resource.properties b/client/src/main/resources/client-resource.properties
index 8d69463..b5f57c9 100644
--- a/client/src/main/resources/client-resource.properties
+++ b/client/src/main/resources/client-resource.properties
@@ -161,3 +161,11 @@ sqoop.prompt_shell_loadrc = Loading resource file {0}
 sqoop.prompt_shell_loadedrc = Resource file loaded.
 
 submission.usage = Usage: submission {0}
+
+# Various Table headers
+table.header.id = Id
+table.header.name = Name
+table.header.version = Version
+table.header.class = Class
+table.header.type = Type
+table.header.connector = Connector


Mime
View raw message