ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject [3/8] ambari git commit: AMBARI-18496. Include an option to download the saved query in hive view. (Anita Gnanamalar Jebaraj via dipayanb)
Date Mon, 10 Oct 2016 14:40:50 GMT
AMBARI-18496. Include an option to download the saved query in hive view. (Anita Gnanamalar
Jebaraj via dipayanb)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/8813b1f9
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/8813b1f9
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/8813b1f9

Branch: refs/heads/branch-dev-patch-upgrade
Commit: 8813b1f9295f052a0f28ac622fdd214964cec7c6
Parents: e7074ec
Author: Dipayan Bhowmick <dipayan.bhowmick@gmail.com>
Authored: Sat Oct 8 01:16:16 2016 +0530
Committer: Dipayan Bhowmick <dipayan.bhowmick@gmail.com>
Committed: Sat Oct 8 01:17:39 2016 +0530

----------------------------------------------------------------------
 .../savedQueries/SavedQueryService.java         | 47 +++++++++++++++++--
 .../ui/hive-web/app/controllers/queries.js      | 24 +++++++++-
 .../ui/hive-web/app/initializers/i18n.js        |  5 ++
 .../savedQueries/SavedQueryService.java         | 48 +++++++++++++++++---
 .../ui/hive-web/app/controllers/queries.js      | 22 ++++++++-
 .../ui/hive-web/app/initializers/i18n.js        |  5 +-
 .../savedQueries/SavedQueryServiceTest.java     | 16 +++++--
 7 files changed, 149 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/8813b1f9/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java
b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java
index ccc4512..9b844d6 100644
--- a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java
@@ -37,7 +37,16 @@ import javax.ws.rs.*;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.StreamingOutput;
 import javax.ws.rs.core.UriInfo;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.util.List;
 
 /**
@@ -73,12 +82,40 @@ public class SavedQueryService extends BaseService {
   @GET
   @Path("{queryId}")
   @Produces(MediaType.APPLICATION_JSON)
-  public Response getOne(@PathParam("queryId") String queryId) {
+  public Response getOne(@PathParam("queryId") String queryId,
+		         @QueryParam("op") String operation) {
     try {
-      SavedQuery savedQuery = getResourceManager().read(queryId);
-      JSONObject object = new JSONObject();
-      object.put("savedQuery", savedQuery);
-      return Response.ok(object).build();
+      final SavedQuery savedQuery = getResourceManager().read(queryId);    
+      if(operation.equals("download")) {
+    	StreamingOutput stream = new StreamingOutput() {
+    	@Override
+    	public void write(OutputStream os) throws IOException, WebApplicationException {
+    	  Writer writer = new BufferedWriter(new OutputStreamWriter(os));
+    	  try { 
+            BufferedReader br=new BufferedReader(new InputStreamReader(getSharedObjectsFactory().getHdfsApi().open(savedQuery.getQueryFile())));
+	    String line;
+    	    line=br.readLine();
+    	    while (line != null){
+    	      writer.write(line+"\n");  
+    	      line = br.readLine();
+            }
+            writer.flush();
+    	  } catch (InterruptedException e) {
+	    e.printStackTrace();
+	  } finally {
+	    writer.close();
+    	  }
+    	}
+    	};
+    	return Response.ok(stream).
+               type(MediaType.TEXT_PLAIN).
+    	       build();
+      }
+      else {
+    	 JSONObject object = new JSONObject();
+         object.put("savedQuery", savedQuery);
+         return Response.ok(object).build();
+      }
     } catch (WebApplicationException ex) {
       throw ex;
     } catch (ItemNotFound itemNotFound) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/8813b1f9/contrib/views/hive-next/src/main/resources/ui/hive-web/app/controllers/queries.js
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/resources/ui/hive-web/app/controllers/queries.js
b/contrib/views/hive-next/src/main/resources/ui/hive-web/app/controllers/queries.js
index cbf6b42..4438c5a 100644
--- a/contrib/views/hive-next/src/main/resources/ui/hive-web/app/controllers/queries.js
+++ b/contrib/views/hive-next/src/main/resources/ui/hive-web/app/controllers/queries.js
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -60,7 +60,8 @@ export default Ember.ArrayController.extend(FilterableMixin, {
   //row buttons
   links: [
     "buttons.history",
-    "buttons.delete"
+    "buttons.delete",
+    "buttons.downloadQuery"
   ],
 
   model: function () {
@@ -92,6 +93,25 @@ export default Ember.ArrayController.extend(FilterableMixin, {
           });
 
           break;
+        case "buttons.downloadQuery":
+          var self = this,
+          defer = Ember.RSVP.defer();
+          this.send('openModal', 
+                    'modal-save', 
+                    {
+                       heading: "modals.downloadQuery.heading",
+                       text: savedQuery.get('title')+".hql",
+                       defer: defer
+                    });
+          defer.promise.then(function (text) {
+            var adapter = self.container.lookup('adapter:application').buildURL();
+            var a = document.createElement('a');
+            a.href = adapter + '/savedQueries/' + savedQuery.get('id') + '?op=download';
+            a.download = text;
+            document.body.appendChild(a);
+            a.click();
+          });
+          break;
       }
     },
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/8813b1f9/contrib/views/hive-next/src/main/resources/ui/hive-web/app/initializers/i18n.js
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/resources/ui/hive-web/app/initializers/i18n.js
b/contrib/views/hive-next/src/main/resources/ui/hive-web/app/initializers/i18n.js
index f7f7706..cc789e8 100644
--- a/contrib/views/hive-next/src/main/resources/ui/hive-web/app/initializers/i18n.js
+++ b/contrib/views/hive-next/src/main/resources/ui/hive-web/app/initializers/i18n.js
@@ -96,6 +96,10 @@ TRANSLATIONS = {
       hdfs: 'Please enter save path and name'
     },
 
+    downloadQuery: {
+      heading: 'Download Query'
+    },
+
     changeTitle: {
       heading: 'Rename worksheet'
     },
@@ -189,6 +193,7 @@ TRANSLATIONS = {
     explain: 'Explain',
     saveAs: 'Save as...',
     save: 'Save',
+    downloadQuery: 'Download',
     newQuery: 'New Worksheet',
     newUdf: 'New UDF',
     history: 'History',

http://git-wip-us.apache.org/repos/asf/ambari/blob/8813b1f9/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryService.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryService.java
b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryService.java
index 9ea19c6..4a5872c 100644
--- a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryService.java
+++ b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryService.java
@@ -37,7 +37,15 @@ import javax.ws.rs.*;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.StreamingOutput;
 import javax.ws.rs.core.UriInfo;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.util.List;
 
 /**
@@ -73,12 +81,40 @@ public class SavedQueryService extends BaseService {
   @GET
   @Path("{queryId}")
   @Produces(MediaType.APPLICATION_JSON)
-  public Response getOne(@PathParam("queryId") String queryId) {
-    try {
-      SavedQuery savedQuery = getResourceManager().read(queryId);
-      JSONObject object = new JSONObject();
-      object.put("savedQuery", savedQuery);
-      return Response.ok(object).build();
+  public Response getOne(@PathParam("queryId") String queryId,
+                         @QueryParam("op") String operation) {
+  try {
+    final SavedQuery savedQuery = getResourceManager().read(queryId);
+    if(operation.equals("download")) {
+      StreamingOutput stream = new StreamingOutput() {
+      @Override
+      public void write(OutputStream os) throws IOException, WebApplicationException {
+        Writer writer = new BufferedWriter(new OutputStreamWriter(os));
+        try {
+          BufferedReader br=new BufferedReader(new InputStreamReader(getSharedObjectsFactory().getHdfsApi().open(savedQuery.getQueryFile())));
+          String line;
+          line=br.readLine();
+          while (line != null){
+            writer.write(line+"\n");
+            line = br.readLine();
+          }
+          writer.flush();
+        } catch (InterruptedException e) {
+          e.printStackTrace();
+        } finally {
+          writer.close();
+        }
+        }
+      };
+      return Response.ok(stream).
+             type(MediaType.TEXT_PLAIN).
+             build();
+    }
+    else {
+       JSONObject object = new JSONObject();
+       object.put("savedQuery", savedQuery);
+       return Response.ok(object).build();
+    }
     } catch (WebApplicationException ex) {
       throw ex;
     } catch (ItemNotFound itemNotFound) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/8813b1f9/contrib/views/hive/src/main/resources/ui/hive-web/app/controllers/queries.js
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/main/resources/ui/hive-web/app/controllers/queries.js
b/contrib/views/hive/src/main/resources/ui/hive-web/app/controllers/queries.js
index cbf6b42..4d82ae0 100644
--- a/contrib/views/hive/src/main/resources/ui/hive-web/app/controllers/queries.js
+++ b/contrib/views/hive/src/main/resources/ui/hive-web/app/controllers/queries.js
@@ -60,7 +60,8 @@ export default Ember.ArrayController.extend(FilterableMixin, {
   //row buttons
   links: [
     "buttons.history",
-    "buttons.delete"
+    "buttons.delete",
+    "buttons.downloadQuery"
   ],
 
   model: function () {
@@ -92,6 +93,25 @@ export default Ember.ArrayController.extend(FilterableMixin, {
           });
 
           break;
+        case "buttons.downloadQuery":
+          var self = this,
+          defer = Ember.RSVP.defer();
+          this.send('openModal', 
+                    'modal-save', 
+                    {
+                       heading: "modals.downloadQuery.heading",
+                       text: savedQuery.get('title') + ".hql",
+                       defer: defer
+                    });
+          defer.promise.then(function (text) {
+            var adapter  = self.container.lookup('adapter:application').buildURL();
+            var a = document.createElement('a');
+            a.href = adapter + '/savedQueries/' + savedQuery.get('id') + '?op=download';
+            a.download = text;
+            document.body.appendChild(a);
+            a.click();
+          });
+          break;
       }
     },
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/8813b1f9/contrib/views/hive/src/main/resources/ui/hive-web/app/initializers/i18n.js
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/main/resources/ui/hive-web/app/initializers/i18n.js b/contrib/views/hive/src/main/resources/ui/hive-web/app/initializers/i18n.js
index f7f7706..aca366f 100644
--- a/contrib/views/hive/src/main/resources/ui/hive-web/app/initializers/i18n.js
+++ b/contrib/views/hive/src/main/resources/ui/hive-web/app/initializers/i18n.js
@@ -95,7 +95,9 @@ TRANSLATIONS = {
       csv: 'Download results as CSV',
       hdfs: 'Please enter save path and name'
     },
-
+    downloadQuery: {
+      heading: 'Download Query'
+    },
     changeTitle: {
       heading: 'Rename worksheet'
     },
@@ -189,6 +191,7 @@ TRANSLATIONS = {
     explain: 'Explain',
     saveAs: 'Save as...',
     save: 'Save',
+    downloadQuery: 'Download',
     newQuery: 'New Worksheet',
     newUdf: 'New UDF',
     history: 'History',

http://git-wip-us.apache.org/repos/asf/ambari/blob/8813b1f9/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java
b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java
index d55858f..68cb0c8 100644
--- a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java
+++ b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/resources/savedQueries/SavedQueryServiceTest.java
@@ -122,7 +122,7 @@ public class SavedQueryServiceTest extends HDFSTest {
   @Test
   public void notFound() {
     thrown.expect(NotFoundFormattedException.class);
-    savedQueryService.getOne("4242");
+    savedQueryService.getOne("4242", null);
   }
 
   @Test
@@ -137,7 +137,7 @@ public class SavedQueryServiceTest extends HDFSTest {
     Response response = savedQueryService.update(request, String.valueOf(createdId));
     Assert.assertEquals(204, response.getStatus());
 
-    Response response2 = savedQueryService.getOne(String.valueOf(createdId));
+    Response response2 = savedQueryService.getOne(String.valueOf(createdId), "");
     Assert.assertEquals(200, response2.getStatus());
 
     JSONObject obj = ((JSONObject) response2.getEntity());
@@ -154,7 +154,7 @@ public class SavedQueryServiceTest extends HDFSTest {
     Assert.assertEquals(204, response.getStatus());
 
     thrown.expect(NotFoundFormattedException.class);
-    savedQueryService.getOne(String.valueOf(createdId));
+    savedQueryService.getOne(String.valueOf(createdId),null);
   }
 
   @Test
@@ -178,4 +178,14 @@ public class SavedQueryServiceTest extends HDFSTest {
         containsTitle = containsTitle || item.getTitle().compareTo("Title 2") == 0;
     Assert.assertTrue(containsTitle);
   }
+  @Test
+  public void downloadQuery() {
+    Response created = doCreateSavedQuery();
+    Object createdId = ((SavedQuery) ((JSONObject) created.getEntity()).get("savedQuery")).getId();
+    SavedQueryService.SavedQueryRequest request = new SavedQueryService.SavedQueryRequest();
+    request.savedQuery = new SavedQuery();
+    request.savedQuery.setTitle("Download Query");
+    Response response = savedQueryService.getOne(String.valueOf(createdId), "download");
+    Assert.assertEquals(200, response.getStatus());
+  }
 }


Mime
View raw message