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:13 UTC

[39/50] [abbrv] incubator-lens git commit: LENS-474 : Add cube latestdate command on cli (Rajat Khandelwal via amareshwari)

LENS-474 : Add cube latestdate command on cli (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/127c0810
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/127c0810
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/127c0810

Branch: refs/heads/current-release-line
Commit: 127c081075178a90d32c8743b03959ab0c562ca5
Parents: f0798b1
Author: Rajat Khandelwal <pr...@apache.org>
Authored: Fri Apr 10 06:36:17 2015 +0530
Committer: Amareshwari Sriramadasu <am...@apache.org>
Committed: Fri Apr 10 06:36:17 2015 +0530

----------------------------------------------------------------------
 .../lens/cli/commands/BaseLensCommand.java      | 16 +++++++++++
 .../lens/cli/commands/LensCubeCommands.java     | 19 +++++++++++++
 .../apache/lens/cli/TestLensCubeCommands.java   | 11 ++++++--
 .../apache/lens/cli/TestLensFactCommands.java   | 28 +++++++++++++++++++-
 .../java/org/apache/lens/client/LensClient.java | 10 +++++++
 .../apache/lens/client/LensMetadataClient.java  | 10 +++++++
 6 files changed, 91 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/127c0810/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java b/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java
index dbb8b39..bf90cdc 100644
--- a/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java
+++ b/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java
@@ -19,6 +19,9 @@
 package org.apache.lens.cli.commands;
 
 import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 import org.apache.lens.client.LensClient;
 import org.apache.lens.client.LensClientSingletonWrapper;
@@ -49,6 +52,19 @@ public class BaseLensCommand {
 
   /** The is connection active. */
   protected static boolean isConnectionActive;
+  public static final String DATE_FMT = "yyyy-MM-dd'T'HH:mm:ss:SSS";
+
+  public static final ThreadLocal<DateFormat> DATE_PARSER =
+    new ThreadLocal<DateFormat>() {
+      @Override
+      protected SimpleDateFormat initialValue() {
+        return new SimpleDateFormat(DATE_FMT);
+      }
+    };
+
+  public static String formatDate(Date dt) {
+    return DATE_PARSER.get().format(dt);
+  }
 
   static {
     Runtime.getRuntime().addShutdownHook(new Thread() {

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/127c0810/lens-cli/src/main/java/org/apache/lens/cli/commands/LensCubeCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/main/java/org/apache/lens/cli/commands/LensCubeCommands.java b/lens-cli/src/main/java/org/apache/lens/cli/commands/LensCubeCommands.java
index 9d835da..675e830 100644
--- a/lens-cli/src/main/java/org/apache/lens/cli/commands/LensCubeCommands.java
+++ b/lens-cli/src/main/java/org/apache/lens/cli/commands/LensCubeCommands.java
@@ -20,6 +20,7 @@ package org.apache.lens.cli.commands;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Date;
 import java.util.List;
 
 import org.apache.lens.api.APIResult;
@@ -140,4 +141,22 @@ public class LensCubeCommands extends BaseLensCommand implements CommandMarker {
 
     }
   }
+
+  /**
+   * Describe cube.
+   *
+   * @param specPair &lt;cube name, timePartition&gt;
+   * @return the string
+   */
+  @CliCommand(value = "cube latestdate", help = "cube get latest")
+  public String getLatest(
+    @CliOption(key = {"", "cube"}, mandatory = true, help = "<cube-name> <timePartition>") String specPair) {
+    Iterable<String> parts = Splitter.on(' ').trimResults().omitEmptyStrings().split(specPair);
+    String[] pair = Iterables.toArray(parts, String.class);
+    if (pair.length != 2) {
+      return "Syntax error, please try in following " + "format. cube get latest <cubeName> <timePartition>";
+    }
+    Date dt = getClient().getLatestDateOfCube(pair[0], pair[1]);
+    return dt == null ? "No Data Available" : formatDate(dt);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/127c0810/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java
index 41f2c93..8334317 100644
--- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java
+++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java
@@ -54,10 +54,17 @@ public class TestLensCubeCommands extends LensCliApplicationTest {
     Assert.assertFalse(cubeList.contains("sample_cube"));
     command.createCube(new File(cubeSpec.toURI()).getAbsolutePath());
     cubeList = command.showCubes();
+    Assert.assertEquals(command.getLatest("sample_cube dt"), "No Data Available");
     Assert.assertTrue(cubeList.contains("sample_cube"));
 
     testUpdateCommand(new File(cubeSpec.toURI()), command);
     command.dropCube("sample_cube");
+    try {
+      command.getLatest("sample_cube dt");
+      Assert.fail("should have failed as cube doesn't exist");
+    } catch (Exception e) {
+      //pass
+    }
     cubeList = command.showCubes();
     Assert.assertFalse(cubeList.contains("sample_cube"));
   }
@@ -83,8 +90,8 @@ public class TestLensCubeCommands extends LensCliApplicationTest {
     String xmlContent = sb.toString();
 
     xmlContent = xmlContent.replace("<property name=\"sample_cube.prop\" value=\"sample\" />\n",
-        "<property name=\"sample_cube.prop\" value=\"sample\" />"
-            + "\n<property name=\"sample_cube.prop1\" value=\"sample1\" />\n");
+      "<property name=\"sample_cube.prop\" value=\"sample\" />"
+        + "\n<property name=\"sample_cube.prop1\" value=\"sample1\" />\n");
 
     File newFile = new File("/tmp/sample_cube1.xml");
     try {

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/127c0810/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 f056bb7..244b9ec 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
@@ -19,8 +19,10 @@
 package org.apache.lens.cli;
 
 import java.io.*;
+import java.net.URISyntaxException;
 import java.net.URL;
 
+import org.apache.lens.cli.commands.LensCubeCommands;
 import org.apache.lens.cli.commands.LensFactCommands;
 import org.apache.lens.client.LensClient;
 
@@ -42,6 +44,7 @@ public class TestLensFactCommands extends LensCliApplicationTest {
 
   /** The command. */
   private static LensFactCommands command = null;
+  private static LensCubeCommands cubeCommands = null;
 
   /**
    * Test fact commands.
@@ -49,12 +52,25 @@ public class TestLensFactCommands extends LensCliApplicationTest {
    * @throws IOException
    */
   @Test
-  public void testFactCommands() throws IOException {
+  public void testFactCommands() throws IOException, URISyntaxException {
+    createSampleCube();
     addFact1Table();
     updateFact1Table();
     testFactStorageActions();
     testFactPartitionActions();
     dropFact1Table();
+    dropSampleCube();
+  }
+
+  private void createSampleCube() throws URISyntaxException {
+    URL cubeSpec = TestLensCubeCommands.class.getClassLoader().getResource("sample-cube.xml");
+    String cubeList = getCubeCommand().showCubes();
+    Assert.assertFalse(cubeList.contains("sample_cube"));
+    getCubeCommand().createCube(new File(cubeSpec.toURI()).getAbsolutePath());
+  }
+
+  private void dropSampleCube() {
+    getCubeCommand().dropCube("sample_cube");
   }
 
   private static LensFactCommands getCommand() {
@@ -66,6 +82,15 @@ public class TestLensFactCommands extends LensCliApplicationTest {
     return command;
   }
 
+  private static LensCubeCommands getCubeCommand() {
+    if (cubeCommands == null) {
+      LensClient client = new LensClient();
+      cubeCommands = new LensCubeCommands();
+      cubeCommands.setClient(client);
+    }
+    return cubeCommands;
+  }
+
   /**
    * Adds the fact1 table.
    *
@@ -218,6 +243,7 @@ public class TestLensFactCommands extends LensCliApplicationTest {
   }
 
   private void verifyAndDeletePartitions() {
+    Assert.assertEquals(getCubeCommand().getLatest("sample_cube dt"), "2014-03-27T12:00:00:000");
     String result = command.getAllPartitionsOfFact("fact1 " + FACT_LOCAL);
     Assert.assertTrue(result.contains("HOURLY"));
     String dropPartitionsStatus = command.dropAllPartitionsOfFact("fact1 " + FACT_LOCAL);

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/127c0810/lens-client/src/main/java/org/apache/lens/client/LensClient.java
----------------------------------------------------------------------
diff --git a/lens-client/src/main/java/org/apache/lens/client/LensClient.java b/lens-client/src/main/java/org/apache/lens/client/LensClient.java
index 8f1b7e1..b5b4a90 100644
--- a/lens-client/src/main/java/org/apache/lens/client/LensClient.java
+++ b/lens-client/src/main/java/org/apache/lens/client/LensClient.java
@@ -18,6 +18,7 @@
  */
 package org.apache.lens.client;
 
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 
@@ -83,6 +84,10 @@ public class LensClient {
     return conn;
   }
 
+  public Date getLatestDateOfCube(String cubeName, String timePartition) {
+    return mc.getLatestDateOfCube(cubeName, timePartition);
+  }
+
   public static class LensClientResultSetWithStats {
     private final LensClientResultSet resultSet;
     private final LensQuery query;
@@ -437,6 +442,7 @@ public class LensClient {
   public APIResult addPartitionToFact(String table, String storage, String partSpec) {
     return mc.addPartitionToFactTable(table, storage, partSpec);
   }
+
   public APIResult addPartitionsToFact(String table, String storage, String partsSpec) {
     return mc.addPartitionsToFactTable(table, storage, partsSpec);
   }
@@ -444,6 +450,7 @@ public class LensClient {
   public APIResult addPartitionToFact(String table, String storage, XPartition xp) {
     return mc.addPartitionToFactTable(table, storage, xp);
   }
+
   public APIResult addPartitionsToFact(String table, String storage, XPartitionList xpList) {
     return mc.addPartitionsToFactTable(table, storage, xpList);
   }
@@ -451,12 +458,15 @@ public class LensClient {
   public APIResult addPartitionToDim(String table, String storage, String partSpec) {
     return mc.addPartitionToDimensionTable(table, storage, partSpec);
   }
+
   public APIResult addPartitionToDim(String table, String storage, XPartition xp) {
     return mc.addPartitionToDimensionTable(table, storage, xp);
   }
+
   public APIResult addPartitionsToDim(String table, String storage, XPartitionList xpList) {
     return mc.addPartitionsToDimensionTable(table, storage, xpList);
   }
+
   public APIResult addPartitionsToDim(String table, String storage, String partsSpec) {
     return mc.addPartitionsToDimensionTable(table, storage, partsSpec);
   }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/127c0810/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 ad79cf2..1e243e8 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
@@ -24,6 +24,7 @@ import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.Charset;
+import java.util.Date;
 import java.util.List;
 
 import javax.ws.rs.client.Client;
@@ -39,6 +40,7 @@ import javax.xml.bind.Unmarshaller;
 
 import org.apache.lens.api.APIResult;
 import org.apache.lens.api.APIResult.Status;
+import org.apache.lens.api.DateTime;
 import org.apache.lens.api.StringList;
 import org.apache.lens.api.metastore.*;
 
@@ -924,4 +926,12 @@ public class LensMetadataClient {
       return new APIResult(Status.FAILED, "File not found");
     }
   }
+
+  public Date getLatestDateOfCube(String cubeName, String timePartition) {
+    return getMetastoreWebTarget().path("cubes").path(cubeName).path("latestdate")
+      .queryParam("timeDimension", timePartition)
+      .queryParam("sessionid", this.connection.getSessionHandle())
+      .request(MediaType.APPLICATION_XML)
+      .get(DateTime.class).getDate();
+  }
 }