You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by am...@apache.org on 2015/04/15 21:50:05 UTC

[31/50] [abbrv] incubator-lens git commit: LENS-472 : Fix adding single partition cli command (Rajat Khandelwal via amareshwari)

LENS-472 : Fix adding single partition cli command (Rajat Khandelwal via amareshwari)


Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/d597314c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/d597314c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/d597314c

Branch: refs/heads/current-release-line
Commit: d597314cb2fa8a26c64e050b80978c6024ae6eb7
Parents: 5e492d9
Author: Rajat Khandelwal <pr...@apache.org>
Authored: Tue Apr 7 17:42:51 2015 +0530
Committer: Amareshwari Sriramadasu <am...@apache.org>
Committed: Tue Apr 7 17:42:51 2015 +0530

----------------------------------------------------------------------
 .../commands/LensDimensionTableCommands.java    |   6 +-
 .../lens/cli/commands/LensFactCommands.java     |   8 +-
 .../apache/lens/cli/TestLensFactCommands.java   |  16 ++
 .../apache/lens/client/LensMetadataClient.java  | 256 ++++++++++---------
 4 files changed, 165 insertions(+), 121 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/d597314c/lens-cli/src/main/java/org/apache/lens/cli/commands/LensDimensionTableCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/main/java/org/apache/lens/cli/commands/LensDimensionTableCommands.java b/lens-cli/src/main/java/org/apache/lens/cli/commands/LensDimensionTableCommands.java
index 5a3fef3..c61be09 100644
--- a/lens-cli/src/main/java/org/apache/lens/cli/commands/LensDimensionTableCommands.java
+++ b/lens-cli/src/main/java/org/apache/lens/cli/commands/LensDimensionTableCommands.java
@@ -333,7 +333,7 @@ public class LensDimensionTableCommands extends BaseLensCommand implements Comma
    * @param specPair the spec pair
    * @return the string
    */
-  @CliCommand(value = "dimtable add partition", help = "add a partition to dim table")
+  @CliCommand(value = "dimtable add single-partition", help = "add a partition to dim table")
   public String addPartitionToDimTable(
     @CliOption(key = {"", "table"}, mandatory = true, help = "<dimension-table-name> <storage-name>"
       + " <path to partition specification>") String specPair) {
@@ -342,7 +342,7 @@ public class LensDimensionTableCommands extends BaseLensCommand implements Comma
     APIResult result;
     if (pair.length != 3) {
       return "Syntax error, please try in following "
-        + "format. dimtable add partition <table> <storage> <partition spec>";
+        + "format. dimtable add single-partition <table> <storage> <partition spec>";
     }
 
     File f = new File(pair[2]);
@@ -373,7 +373,7 @@ public class LensDimensionTableCommands extends BaseLensCommand implements Comma
     APIResult result;
     if (pair.length != 3) {
       return "Syntax error, please try in following "
-        + "format. dimtable add partition <table> <storage> <partition spec>";
+        + "format. dimtable add partitions <table> <storage> <partition spec>";
     }
 
     File f = new File(pair[2]);

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/d597314c/lens-cli/src/main/java/org/apache/lens/cli/commands/LensFactCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/main/java/org/apache/lens/cli/commands/LensFactCommands.java b/lens-cli/src/main/java/org/apache/lens/cli/commands/LensFactCommands.java
index 47686e9..a69d361 100644
--- a/lens-cli/src/main/java/org/apache/lens/cli/commands/LensFactCommands.java
+++ b/lens-cli/src/main/java/org/apache/lens/cli/commands/LensFactCommands.java
@@ -311,7 +311,7 @@ public class LensFactCommands extends BaseLensCommand implements CommandMarker {
    * @param specPair the spec pair
    * @return the string
    */
-  @CliCommand(value = "fact add partition", help = "add a partition to fact table")
+  @CliCommand(value = "fact add single-partition", help = "add a partition to fact table")
   public String addPartitionToFact(
     @CliOption(key = {"", "table"}, mandatory = true, help
       = "<table> <storage> <path to partition spec>") String specPair) {
@@ -319,7 +319,8 @@ public class LensFactCommands extends BaseLensCommand implements CommandMarker {
     String[] pair = Iterables.toArray(parts, String.class);
     APIResult result;
     if (pair.length != 3) {
-      return "Syntax error, please try in following " + "format. fact add partition <table> <storage> <partition spec>";
+      return "Syntax error, please try in following " + "format. fact add single-partition "
+        + "<table> <storage> <partition spec>";
     }
 
     File f = new File(pair[2]);
@@ -349,7 +350,8 @@ public class LensFactCommands extends BaseLensCommand implements CommandMarker {
     String[] pair = Iterables.toArray(parts, String.class);
     APIResult result;
     if (pair.length != 3) {
-      return "Syntax error, please try in following " + "format. fact add partition <table> <storage> <partition spec>";
+      return "Syntax error, please try in following "
+        + "format. fact add partitions <table> <storage> <partitions spec>";
     }
 
     File f = new File(pair[2]);

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/d597314c/lens-cli/src/test/java/org/apache/lens/cli/TestLensFactCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensFactCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensFactCommands.java
index f225155..f056bb7 100644
--- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensFactCommands.java
+++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensFactCommands.java
@@ -199,6 +199,22 @@ public class TestLensFactCommands extends LensCliApplicationTest {
       Assert.fail("Unable to locate the storage part file for adding new storage to fact table fact1");
     }
     verifyAndDeletePartitions();
+
+    // Wrong files:
+    try {
+      command.addPartitionToFact("fact1 " + FACT_LOCAL + " " + new File(
+        TestLensFactCommands.class.getClassLoader().getResource("fact1-local-parts.xml").toURI()).getAbsolutePath());
+      Assert.fail("Should fail");
+    } catch (Throwable t) {
+      // pass
+    }
+    try {
+      command.addPartitionsToFact("fact1 " + FACT_LOCAL + " " + new File(
+        TestLensFactCommands.class.getClassLoader().getResource("fact1-local-part.xml").toURI()).getAbsolutePath());
+      Assert.fail("Should fail");
+    } catch (Throwable t) {
+      // pass
+    }
   }
 
   private void verifyAndDeletePartitions() {

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/d597314c/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java
----------------------------------------------------------------------
diff --git a/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java b/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java
index 3462dc3..ad79cf2 100644
--- a/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java
+++ b/lens-client/src/main/java/org/apache/lens/client/LensMetadataClient.java
@@ -186,23 +186,22 @@ public class LensMetadataClient {
     return result;
   }
 
-  private Object readFromXML(String filename) throws JAXBException, IOException {
+  private <T> T readFromXML(String filename) throws JAXBException, IOException {
     if (filename.startsWith("/")) {
-      return ((JAXBElement) JAXB_UNMARSHALLER.unmarshal(new File(filename))).getValue();
+      return ((JAXBElement<T>) JAXB_UNMARSHALLER.unmarshal(new File(filename))).getValue();
     } else {
       // load from classpath
       InputStream file = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
       if (file == null) {
         throw new IOException("File not found:" + filename);
       }
-      return ((JAXBElement) JAXB_UNMARSHALLER.unmarshal(file)).getValue();
+      return ((JAXBElement<T>) JAXB_UNMARSHALLER.unmarshal(file)).getValue();
     }
   }
 
   public APIResult createCube(String cubeSpec) {
-    XCube cube;
     try {
-      cube = (XCube) readFromXML(cubeSpec);
+      return createCube(this.<XCube>readFromXML(cubeSpec));
     } catch (JAXBException e) {
       LOG.info("Unmarshalling error:", e);
       return new APIResult(Status.FAILED, "Unmarshalling failed");
@@ -210,7 +209,6 @@ public class LensMetadataClient {
       LOG.info("File error:", e);
       return new APIResult(Status.FAILED, "File not found");
     }
-    return createCube(cube);
   }
 
   public APIResult updateCube(String cubeName, XCube cube) {
@@ -223,9 +221,8 @@ public class LensMetadataClient {
   }
 
   public APIResult updateCube(String cubeName, String cubeSpec) {
-    XCube cube;
     try {
-      cube = (XCube) readFromXML(cubeSpec);
+      return updateCube(cubeName, this.<XCube>readFromXML(cubeSpec));
     } catch (JAXBException e) {
       LOG.info("Unmarshalling error:", e);
       return new APIResult(Status.FAILED, "Unmarshalling failed");
@@ -233,7 +230,6 @@ public class LensMetadataClient {
       LOG.info("File error:", e);
       return new APIResult(Status.FAILED, "File not found");
     }
-    return updateCube(cubeName, cube);
   }
 
   public XCube getCube(String cubeName) {
@@ -279,12 +275,15 @@ public class LensMetadataClient {
   }
 
   public APIResult createDimension(String dimSpec) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("dimensions")
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.xml(getContent(dimSpec)), APIResult.class);
-    return result;
+    try {
+      return createDimension(this.<XDimension>readFromXML(dimSpec));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   public APIResult updateDimension(String dimName, XDimension dimension) {
@@ -297,12 +296,15 @@ public class LensMetadataClient {
   }
 
   public APIResult updateDimension(String dimName, String dimSpec) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("dimensions").path(dimName)
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .put(Entity.xml(getContent(dimSpec)), APIResult.class);
-    return result;
+    try {
+      return updateDimension(dimName, this.<XDimension>readFromXML(dimSpec));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   public XDimension getDimension(String dimName) {
@@ -342,12 +344,15 @@ public class LensMetadataClient {
 
 
   public APIResult createNewStorage(String storage) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("storages")
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.xml(getContent(storage)), APIResult.class);
-    return result;
+    try {
+      return createNewStorage(this.<XStorage>readFromXML(storage));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   public APIResult dropAllStorages() {
@@ -369,12 +374,15 @@ public class LensMetadataClient {
   }
 
   public APIResult updateStorage(String storageName, String storage) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("storages").path(storageName)
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .put(Entity.xml(getContent(storage)), APIResult.class);
-    return result;
+    try {
+      return updateStorage(storageName, this.<XStorage>readFromXML(storage));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   public XStorage getStorage(String storageName) {
@@ -452,18 +460,15 @@ public class LensMetadataClient {
   }
 
   public APIResult createFactTable(String factSpec) {
-    WebTarget target = getMetastoreWebTarget();
-    FormDataMultiPart mp = new FormDataMultiPart();
-    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionid")
-      .build(), this.connection.getSessionHandle(), MediaType.APPLICATION_XML_TYPE));
-    mp.bodyPart(new FormDataBodyPart(
-      FormDataContentDisposition.name("fact").fileName("fact").build(),
-      getContent(factSpec), MediaType.APPLICATION_XML_TYPE));
-    APIResult result = target.path("facts")
-      .request(MediaType.APPLICATION_XML_TYPE)
-      .post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE),
-        APIResult.class);
-    return result;
+    try {
+      return createFactTable(this.<XFactTable>readFromXML(factSpec));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   private String getContent(String path) {
@@ -499,12 +504,15 @@ public class LensMetadataClient {
   }
 
   public APIResult updateFactTable(String factName, String table) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("facts").path(factName)
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML_TYPE)
-      .put(Entity.xml(getContent(table)), APIResult.class);
-    return result;
+    try {
+      return updateFactTable(factName, this.<XFactTable>readFromXML(table));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   public APIResult dropFactTable(String factName, boolean cascade) {
@@ -548,13 +556,16 @@ public class LensMetadataClient {
     return result;
   }
 
-  public APIResult addStorageToFactTable(String factname, String storage) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("facts").path(factname).path("storages")
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.xml(getContent(storage)), APIResult.class);
-    return result;
+  public APIResult addStorageToFactTable(String factname, String storageSpec) {
+    try {
+      return addStorageToFactTable(factname, this.<XStorageTableElement>readFromXML(storageSpec));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   public APIResult dropStorageFromFactTable(String factName, String storageName) {
@@ -649,19 +660,15 @@ public class LensMetadataClient {
   }
 
   public APIResult createDimensionTable(String tableXml) {
-    WebTarget target = getMetastoreWebTarget();
-
-    FormDataMultiPart mp = new FormDataMultiPart();
-    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionid").build(),
-      this.connection.getSessionHandle(), MediaType.APPLICATION_XML_TYPE));
-    mp.bodyPart(new FormDataBodyPart(
-      FormDataContentDisposition.name("dimensionTable").fileName("dimtable").build(),
-      getContent(tableXml), MediaType.APPLICATION_XML_TYPE));
-
-    APIResult result = target.path("dimtables")
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), APIResult.class);
-    return result;
+    try {
+      return createDimensionTable(this.<XDimensionTable>readFromXML(tableXml));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
 
@@ -676,12 +683,18 @@ public class LensMetadataClient {
   }
 
   public APIResult updateDimensionTable(String dimTblName, String dimSpec) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("dimtables").path(dimTblName)
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .put(Entity.xml(getContent(dimSpec)), APIResult.class);
-    return result;
+    try {
+      XDimensionTable dimensionTable = readFromXML(dimSpec);
+      dimensionTable.setTableName(dimTblName);
+      return updateDimensionTable(dimensionTable);
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
+
   }
 
   public APIResult dropDimensionTable(String table, boolean cascade) {
@@ -728,12 +741,15 @@ public class LensMetadataClient {
   }
 
   public APIResult addStorageToDimTable(String dimTblName, String table) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("dimtables").path(dimTblName).path("storages")
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.xml(getContent(table)), APIResult.class);
-    return result;
+    try {
+      return addStorageToDimTable(dimTblName, this.<XStorageTableElement>readFromXML(table));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   public XStorageTableElement getStorageOfDimensionTable(String dimTblName, String storage) {
@@ -825,15 +841,18 @@ public class LensMetadataClient {
   }
 
   public APIResult addPartitionToDimensionTable(String dimTblName, String storage,
-    String partition) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("dimtables").path(dimTblName)
-      .path("storages").path(storage).path("partition")
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.xml(getContent(partition)), APIResult.class);
-    return result;
+    String partitionSpec) {
+    try {
+      return addPartitionToDimensionTable(dimTblName, storage, (XPartition) readFromXML(partitionSpec));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
+
   public APIResult addPartitionsToDimensionTable(String dimTblName, String storage,
     XPartitionList partitions) {
     WebTarget target = getMetastoreWebTarget();
@@ -846,14 +865,16 @@ public class LensMetadataClient {
   }
 
   public APIResult addPartitionsToDimensionTable(String dimTblName, String storage,
-    String partitions) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("dimtables").path(dimTblName)
-      .path("storages").path(storage).path("partitions")
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.xml(getContent(partitions)), APIResult.class);
-    return result;
+    String partitionsSpec) {
+    try {
+      return addPartitionsToDimensionTable(dimTblName, storage, (XPartitionList) readFromXML(partitionsSpec));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 
   public APIResult addPartitionToFactTable(String fact, String storage,
@@ -868,15 +889,18 @@ public class LensMetadataClient {
   }
 
   public APIResult addPartitionToFactTable(String fact, String storage,
-    String partition) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("facts").path(fact)
-      .path("storages").path(storage).path("partition")
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.xml(getContent(partition)), APIResult.class);
-    return result;
+    String partitionSpec) {
+    try {
+      return addPartitionToFactTable(fact, storage, (XPartition) readFromXML(partitionSpec));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
+
   public APIResult addPartitionsToFactTable(String fact, String storage,
     XPartitionList partitions) {
     WebTarget target = getMetastoreWebTarget();
@@ -889,13 +913,15 @@ public class LensMetadataClient {
   }
 
   public APIResult addPartitionsToFactTable(String fact, String storage,
-    String partitions) {
-    WebTarget target = getMetastoreWebTarget();
-    APIResult result = target.path("facts").path(fact)
-      .path("storages").path(storage).path("partitions")
-      .queryParam("sessionid", this.connection.getSessionHandle())
-      .request(MediaType.APPLICATION_XML)
-      .post(Entity.xml(getContent(partitions)), APIResult.class);
-    return result;
+    String partitionsSpec) {
+    try {
+      return addPartitionsToFactTable(fact, storage, (XPartitionList) readFromXML(partitionsSpec));
+    } catch (JAXBException e) {
+      LOG.info("Unmarshalling error:", e);
+      return new APIResult(Status.FAILED, "Unmarshalling failed");
+    } catch (IOException e) {
+      LOG.info("File error:", e);
+      return new APIResult(Status.FAILED, "File not found");
+    }
   }
 }