sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cheol...@apache.org
Subject [1/3] SQOOP-918: Introduce client API and change Sqoop shell to use it
Date Mon, 11 Mar 2013 04:35:01 GMT
http://git-wip-us.apache.org/repos/asf/sqoop/blob/2f9a2a71/client/src/main/java/org/apache/sqoop/client/utils/SubmissionDisplayer.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/utils/SubmissionDisplayer.java b/client/src/main/java/org/apache/sqoop/client/utils/SubmissionDisplayer.java
index 8afb492..c56cefd 100644
--- a/client/src/main/java/org/apache/sqoop/client/utils/SubmissionDisplayer.java
+++ b/client/src/main/java/org/apache/sqoop/client/utils/SubmissionDisplayer.java
@@ -17,94 +17,94 @@
  */
 package org.apache.sqoop.client.utils;
 
-import org.apache.sqoop.client.core.Environment;
 import org.apache.sqoop.model.MSubmission;
 import org.apache.sqoop.submission.SubmissionStatus;
 import org.apache.sqoop.submission.counter.Counter;
 import org.apache.sqoop.submission.counter.CounterGroup;
 import org.apache.sqoop.submission.counter.Counters;
-import org.codehaus.groovy.tools.shell.IO;
 
 import java.text.SimpleDateFormat;
 
+import static org.apache.sqoop.client.shell.ShellEnvironment.*;
+
 /**
  *
  */
 public final class SubmissionDisplayer {
 
-  public static void display(IO io, MSubmission submission) {
+  public static void display(MSubmission submission) {
 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
-    io.out.println("@|bold Submission details|@");
+    println("@|bold Submission details|@");
 
-    io.out.print("Job id: ");
-    io.out.println(submission.getJobId());
+    print("Job id: ");
+    println(submission.getJobId());
 
-    io.out.print("Status: ");
-    printColoredStatus(io, submission.getStatus());
-    io.out.println();
+    print("Status: ");
+    printColoredStatus(submission.getStatus());
+    println();
 
-    io.out.print("Creation date: ");
-    io.out.println(dateFormat.format(submission.getCreationDate()));
+    print("Creation date: ");
+    println(dateFormat.format(submission.getCreationDate()));
 
-    io.out.print("Last update date: ");
-    io.out.println(dateFormat.format(submission.getLastUpdateDate()));
+    print("Last update date: ");
+    println(dateFormat.format(submission.getLastUpdateDate()));
 
     String externalId = submission.getExternalId();
     if(externalId != null) {
-      io.out.print("External Id: ");
-      io.out.println(externalId);
+      print("External Id: ");
+      println(externalId);
 
       String externalLink = submission.getExternalLink();
       if(externalLink != null) {
-        io.out.println("\t" + externalLink);
+        println("\t" + externalLink);
       }
     }
 
     if(submission.getStatus().isRunning()) {
       double progress = submission.getProgress();
-      io.out.print("Progress: ");
+      print("Progress: ");
       if(progress == -1) {
-        io.out.println("Progress is not available");
+        println("Progress is not available");
       } else {
-        io.out.println(String.format("%.2f %%", progress * 100));
+        println(String.format("%.2f %%", progress * 100));
       }
     }
 
     Counters counters = submission.getCounters();
     if(counters != null) {
-      io.out.println("Counters:");
+      println("Counters:");
       for(CounterGroup group : counters) {
-        io.out.print("\t");
-        io.out.println(group.getName());
+        print("\t");
+        println(group.getName());
         for(Counter counter : group) {
-          io.out.print("\t\t");
-          io.out.print(counter.getName());
-          io.out.print(": ");
-          io.out.println(counter.getValue());
+          print("\t\t");
+          print(counter.getName());
+          print(": ");
+          println(counter.getValue());
         }
       }
     }
 
     // Exception handling
     if(submission.getExceptionInfo() != null) {
-      io.out.print("@|red Exception: |@");
-      io.out.println(submission.getExceptionInfo());
+      print("@|red Exception: |@");
+      println(submission.getExceptionInfo());
 
-      if(Environment.isVerboose() && submission.getExceptionStackTrace() != null)
{
-        io.out.print("@|bold Stack trace: |@");
-        io.out.println(submission.getExceptionStackTrace());
+      if(isVerboose() && submission.getExceptionStackTrace() != null) {
+        print("@|bold Stack trace: |@");
+        println(submission.getExceptionStackTrace());
       }
     }
   }
 
-  public static void printColoredStatus(IO io, SubmissionStatus status) {
+  public static void printColoredStatus(SubmissionStatus status) {
     if(status.isRunning()) {
-      io.out.print("@|green " + status.toString() + " |@");
+      print("@|green " + status.toString() + " |@");
     } else if(status.isFailure()) {
-      io.out.print("@|red " + status.toString() + " |@");
+      print("@|red " + status.toString() + " |@");
     } else {
-      io.out.print(status.toString());
+      print(status.toString());
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/2f9a2a71/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
index 63d2ef4..79ea96f 100644
--- a/client/src/main/java/org/apache/sqoop/client/utils/TableDisplayer.java
+++ b/client/src/main/java/org/apache/sqoop/client/utils/TableDisplayer.java
@@ -18,11 +18,12 @@
 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;
 
+import static org.apache.sqoop.client.shell.ShellEnvironment.*;
+
 /**
  * Display table based data
  */
@@ -31,13 +32,10 @@ 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;
+  public static void display(List<String> headers, List<String> ...columns) {
     assert headers != null;
     assert columns != null;
     assert headers.size() == columns.length;
@@ -52,50 +50,49 @@ public class TableDisplayer {
     }
 
     // First line is border
-    drawLine(io, widths);
+    drawLine(widths);
 
     // Print out header (text is centralised)
-    io.out.print("| ");
+    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) ? " |" : " | ");
+      print(StringUtils.center(headers.get(i), widths.get(i), ' '));
+      print((i == columnCount -1) ? " |" : " | ");
     }
-    io.out.println();
+    println();
 
     // End up header by border
-    drawLine(io, widths);
+    drawLine(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("| ");
+      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) ? " |" : " | ");
+        print(StringUtils.rightPad(columns[i].get(row), widths.get(i), ' '));
+        print((i == columnCount -1) ? " |" : " | ");
       }
-      io.out.println();
+      println();
     }
 
     // End table by final border
-    drawLine(io, widths);
+    drawLine(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) {
+  private static void drawLine(List<Integer> widths) {
     int last = widths.size() - 1;
-    io.out.print("+-");
+    print("+-");
     for(int i = 0; i < widths.size(); i++) {
-      io.out.print(StringUtils.repeat("-", widths.get(i)));
-      io.out.print((i == last) ? "-+" : "-+-");
+      print(StringUtils.repeat("-", widths.get(i)));
+      print((i == last) ? "-+" : "-+-");
     }
-    io.out.println();
+    println();
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/sqoop/blob/2f9a2a71/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java b/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java
index 45c78fb..73aeb1f 100644
--- a/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java
+++ b/client/src/main/java/org/apache/sqoop/client/utils/ThrowableDisplayer.java
@@ -19,9 +19,9 @@ package org.apache.sqoop.client.utils;
 
 import groovy.lang.MissingPropertyException;
 import org.apache.sqoop.client.core.ClientError;
-import org.apache.sqoop.client.core.Environment;
 import org.apache.sqoop.common.SqoopException;
-import org.codehaus.groovy.tools.shell.IO;
+
+import static org.apache.sqoop.client.shell.ShellEnvironment.*;
 
 /**
  * Pretty printing of Throwable objects
@@ -29,18 +29,6 @@ import org.codehaus.groovy.tools.shell.IO;
 public class ThrowableDisplayer {
 
   /**
-   * Associated shell IO object.
-   *
-   * This objects needs to be set explicitly as some of the methods are called
-   * by Groovy shell without ability to pass additional arguments.
-   */
-  private static IO io;
-
-  public static void setIo(IO ioObject) {
-    io = ioObject;
-  }
-
-  /**
    * Error hook installed to Groovy shell.
    *
    * Will display exception that appeared during executing command. In most
@@ -52,48 +40,46 @@ public class ThrowableDisplayer {
    * @param t Throwable to be displayed
    */
   public static void errorHook(Throwable t) {
-    io.out.println("@|red Exception has occurred during processing command |@");
+    println("@|red Exception has occurred during processing command |@");
 
     // If this is server exception from server
     if(t instanceof SqoopException
       && ((SqoopException)t).getErrorCode() == ClientError.CLIENT_0006) {
-      io.out.print("@|red Server has returned exception: |@");
-      printThrowable(io, t.getCause(), Environment.isVerboose());
+      print("@|red Server has returned exception: |@");
+      printThrowable(t.getCause(), isVerboose());
     } else if(t.getClass() == MissingPropertyException.class) {
-      io.out.print("@|red Unknown command: |@");
-      io.out.println(t.getMessage());
+      print("@|red Unknown command: |@");
+      println(t.getMessage());
     } else {
-      printThrowable(io, t, Environment.isVerboose());
+      printThrowable(t, isVerboose());
     }
   }
 
   /**
    * Pretty print Throwable instance including stack trace and causes.
    *
-   * @param io IO object to use for generating output
    * @param t Throwable to display
    */
-  protected static void printThrowable(IO io, Throwable t, boolean verbose) {
-    io.out.print("@|red Exception: |@");
-    io.out.print(t.getClass().getName());
-    io.out.print(" @|red Message: |@");
-    io.out.print(t.getMessage());
-    io.out.println();
+  protected static void printThrowable(Throwable t, boolean verbose) {
+    print("@|red Exception: |@");
+    print(t.getClass().getName());
+    print(" @|red Message: |@");
+    print(t.getMessage());
+    println();
 
     if(verbose) {
-      io.out.println("Stack trace:");
+      println("Stack trace:");
       for(StackTraceElement e : t.getStackTrace()) {
-        io.out.print("\t @|bold at |@ ");
-        io.out.print(e.getClassName());
-        io.out.print(" (@|bold " + e.getFileName() + ":"
-          + e.getLineNumber() + ") |@ ");
-        io.out.println();
+        print("\t @|bold at |@ ");
+        print(e.getClassName());
+        print(" (@|bold " + e.getFileName() + ":" + e.getLineNumber() + ") |@ ");
+        println();
       }
 
       Throwable cause = t.getCause();
       if(cause != null) {
-        io.out.print("Caused by: ");
-        printThrowable(io, cause, verbose);
+        print("Caused by: ");
+        printThrowable(cause, verbose);
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/2f9a2a71/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 58cfe55..c3a22bb 100644
--- a/client/src/main/resources/client-resource.properties
+++ b/client/src/main/resources/client-resource.properties
@@ -26,8 +26,9 @@ object-name.help = Non unique name of the entity to help you remember \
 #############################
 # Messages
 #
-#  Missing args
+# Argument related
 #
+args.function.unknown = The specified function "{0}" is not recognized.
 args.xid_missing = Required argument --xid is missing.
 args.jid_missing = Required argument --jid is missing.
 args.cid_missing = Required argument --cid is missing.

http://git-wip-us.apache.org/repos/asf/sqoop/blob/2f9a2a71/test/src/test/java/org/apache/sqoop/integration/TomcatTestCase.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/sqoop/integration/TomcatTestCase.java b/test/src/test/java/org/apache/sqoop/integration/TomcatTestCase.java
index 8b46179..5631144 100644
--- a/test/src/test/java/org/apache/sqoop/integration/TomcatTestCase.java
+++ b/test/src/test/java/org/apache/sqoop/integration/TomcatTestCase.java
@@ -19,6 +19,7 @@ package org.apache.sqoop.integration;
 
 import org.apache.log4j.Logger;
 import org.apache.commons.lang.StringUtils;
+import org.apache.sqoop.client.SqoopClient;
 import org.apache.sqoop.test.minicluster.TomcatSqoopMiniCluster;
 import org.junit.After;
 import org.junit.Before;
@@ -61,10 +62,17 @@ abstract public class TomcatTestCase {
    */
   private TomcatSqoopMiniCluster cluster;
 
+  /**
+   * Sqoop client API.
+   */
+  private SqoopClient client;
+
   @Before
   public void startServer() throws Exception {
     cluster = new TomcatSqoopMiniCluster(getTemporaryPath());
     cluster.start();
+
+    client = new SqoopClient(getServerUrl());
   }
 
   @After
@@ -72,6 +80,15 @@ abstract public class TomcatTestCase {
     cluster.stop();
   }
 
+  /**
+   * Return SqoopClient configured to talk to testing server.
+   *
+   * @return
+   */
+  public SqoopClient getClient() {
+    return client;
+  }
+
   public String getTemporaryPath() {
     return TMP_PATH;
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/2f9a2a71/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
b/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
index 20588d3..538a277 100644
--- a/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
+++ b/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
@@ -18,32 +18,20 @@
 package org.apache.sqoop.integration.connector.jdbc.generic;
 
 import org.apache.log4j.Logger;
-import org.apache.sqoop.client.request.ConnectionRequest;
-import org.apache.sqoop.client.request.ConnectorRequest;
-import org.apache.sqoop.client.request.FrameworkRequest;
-import org.apache.sqoop.client.request.JobRequest;
-import org.apache.sqoop.client.request.SubmissionRequest;
 import org.apache.sqoop.framework.configuration.OutputFormat;
 import org.apache.sqoop.framework.configuration.StorageType;
 import org.apache.sqoop.integration.connector.ConnectorTestCase;
-import org.apache.sqoop.json.ConnectorBean;
-import org.apache.sqoop.json.FrameworkBean;
-import org.apache.sqoop.json.ValidationBean;
 import org.apache.sqoop.model.MConnection;
-import org.apache.sqoop.model.MConnector;
 import org.apache.sqoop.model.MEnumInput;
-import org.apache.sqoop.model.MFramework;
 import org.apache.sqoop.model.MJob;
+import org.apache.sqoop.model.MPersistableEntity;
 import org.apache.sqoop.model.MStringInput;
 import org.apache.sqoop.model.MSubmission;
 import org.apache.sqoop.validation.Status;
-import org.apache.sqoop.validation.Validation;
 import org.junit.Test;
 
-import java.util.Map;
-
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertTrue;
 
 /**
@@ -74,13 +62,7 @@ public class TableImportTest extends ConnectorTestCase {
     // the client API for embedding Sqoop client will be ready.
 
     // Connection creation
-    FrameworkBean frameworkBean = (new FrameworkRequest()).read(getServerUrl());
-    ConnectorBean connectorBean = (new ConnectorRequest()).read(getServerUrl(), "1");
-    MFramework framework = frameworkBean.getFramework();
-    MConnector connector = connectorBean.getConnectors().get(0);
-    MConnection connection = new MConnection(connector.getPersistenceId(),
-                                             connector.getConnectionForms(),
-                                             framework.getConnectionForms());
+    MConnection connection = getClient().newConnection(1L);
 
     // Connector values
     ((MStringInput) (connection.getConnectorPart().getForms().get(0).getInputs().get(0))).setValue(provider.getJdbcDriver());
@@ -90,21 +72,11 @@ public class TableImportTest extends ConnectorTestCase {
     // Framework values
     // No need to set anything
 
-    ValidationBean validationBean = (new ConnectionRequest()).create(getServerUrl(), connection);
-
-    assertEquals(Status.FINE, validationBean.getConnectorValidation().getStatus());
-    assertEquals(Status.FINE, validationBean.getFrameworkValidation().getStatus());
-    assertNotNull(validationBean.getId());
-    connection.setPersistenceId(validationBean.getId());
+    assertEquals(Status.FINE, getClient().createConnection(connection));
+    assertNotSame(MPersistableEntity.PERSISTANCE_ID_DEFAULT, connection.getPersistenceId());
 
     // Job creation
-    MJob job = new MJob(
-      connector.getPersistenceId(),
-      connection.getPersistenceId(),
-      MJob.Type.IMPORT,
-      connector.getJobForms(MJob.Type.IMPORT),
-      framework.getJobForms(MJob.Type.IMPORT)
-    );
+    MJob job = getClient().newJob(connection.getPersistenceId(), MJob.Type.IMPORT);
 
     // Connector values
     ((MStringInput) (job.getConnectorPart().getForms().get(0).getInputs().get(0))).setValue(provider.escapeTableName(getTableName()));
@@ -114,21 +86,16 @@ public class TableImportTest extends ConnectorTestCase {
     ((MEnumInput) (job.getFrameworkPart().getForms().get(0).getInputs().get(1))).setValue(OutputFormat.TEXT_FILE.toString());
     ((MStringInput) (job.getFrameworkPart().getForms().get(0).getInputs().get(2))).setValue(getMapreduceDirectory());
 
-    validationBean = (new JobRequest()).create(getServerUrl(), job);
-    assertEquals(Status.FINE, validationBean.getConnectorValidation().getStatus());
-    assertEquals(Status.FINE, validationBean.getFrameworkValidation().getStatus());
-    assertNotNull(validationBean.getId());
-    job.setPersistenceId(validationBean.getId());
-
-    SubmissionRequest submissionRequest = new SubmissionRequest();
+    assertEquals(Status.FINE, getClient().createJob(job));
+    assertNotSame(MPersistableEntity.PERSISTANCE_ID_DEFAULT, job.getPersistenceId());
 
-    MSubmission submission = submissionRequest.create(getServerUrl(), "" + job.getPersistenceId()).getSubmission();
+    MSubmission submission = getClient().startSubmission(job.getPersistenceId());
     assertTrue(submission.getStatus().isRunning());
 
     // Wait until the job finish
     do {
       Thread.sleep(5000);
-      submission = submissionRequest.read(getServerUrl(), "" + job.getPersistenceId()).getSubmission();
+      submission = getClient().getSubmissionStatus(job.getPersistenceId());
     } while(submission.getStatus().isRunning());
 
     // Assert correct output


Mime
View raw message