[
https://issues.apache.org/jira/browse/DRILL-6423?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16481763#comment-16481763
]
ASF GitHub Bot commented on DRILL-6423:
---------------------------------------
asfgit closed pull request #1266: DRILL-6423: Export query result as a CSV file
URL: https://github.com/apache/drill/pull/1266
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryResources.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryResources.java
index c46c4b5d94..e69b261c99 100644
---
a/exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryResources.java
+++
b/exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryResources.java
@@ -96,8 +96,10 @@ public Viewable submitQuery(@FormParam("query") String query,
public static class TabularResult {
private final List<String> columns;
private final List<List<String>> rows;
+ private final String queryId;
public TabularResult(QueryResult result) {
+ queryId = result.getQueryId();
final List<List<String>> rows = Lists.newArrayList();
for (Map<String, String> rowMap:result.rows) {
final List<String> row = Lists.newArrayList();
@@ -115,6 +117,10 @@ public boolean isEmpty() {
return columns.isEmpty();
}
+ public String getQueryId() {
+ return queryId;
+ }
+
public List<String> getColumns() {
return columns;
}
diff --git
a/exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryWrapper.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryWrapper.java
index 6a9294295f..911ac0fb06 100644
---
a/exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryWrapper.java
+++
b/exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryWrapper.java
@@ -23,6 +23,7 @@
import org.apache.drill.exec.proto.UserBitShared.QueryId;
import org.apache.drill.exec.proto.UserBitShared.QueryType;
import org.apache.drill.exec.proto.UserProtos.RunQuery;
+import org.apache.drill.exec.proto.helper.QueryIdHelper;
import org.apache.drill.exec.proto.UserProtos.QueryResultsMode;
import org.apache.drill.exec.work.WorkManager;
@@ -79,18 +80,23 @@ public QueryResult run(final WorkManager workManager, final
WebUserConnection we
}
// Return the QueryResult.
- return new QueryResult(webUserConnection.columns,
webUserConnection.results);
+ return new QueryResult(queryId, webUserConnection.columns,
webUserConnection.results);
}
public static class QueryResult {
+ private final String queryId;
public final Collection<String> columns;
-
public final List<Map<String, String>> rows;
- public QueryResult(Collection<String> columns, List<Map<String, String>>
rows) {
+ public QueryResult(QueryId queryId, Collection<String> columns,
List<Map<String, String>> rows) {
+ this.queryId = QueryIdHelper.getQueryId(queryId);
this.columns = columns;
this.rows = rows;
}
+
+ public String getQueryId() {
+ return queryId;
+ }
}
@Override
diff --git a/exec/java-exec/src/main/resources/rest/query/result.ftl
b/exec/java-exec/src/main/resources/rest/query/result.ftl
index 37eeca8d30..96e68b0c91 100644
--- a/exec/java-exec/src/main/resources/rest/query/result.ftl
+++ b/exec/java-exec/src/main/resources/rest/query/result.ftl
@@ -30,6 +30,22 @@
<a href="/queries">back</a><br/>
<div class="page-header">
</div>
+ <div>
+ <table><tr>
+ <td align='left'>
+ <button type="button" title="Open in new window"
onclick="popOutProfile('${model.getQueryId()}');" class="btn btn-default
btn-sm">
+ <b>Query Profile:</b> ${model.getQueryId()} <span class="glyphicon
glyphicon-new-window"/></button>
+ </td><td align="right" width="100%">
+ <div class="input-group">
+ <span class="input-group-addon" style="font-size:95%">Delimiter
</span>
+ <input id="delimitBy" type="text" class="form-control input-sm"
name="delimitBy" title="Specify delimiter" placeholder="Required" maxlength="2"
size="2" value=",">
+ </div></td><td>
+ <button type="button" title="Export visible table as CSV. Show ALL
rows to export entire resultSet"
onclick="exportTableAsCsv('${model.getQueryId()}');" class="btn btn-default
btn-sm">
+ <b>Export </b> <span class="glyphicon glyphicon-export"/></button>
+ </td>
+ </tr>
+ </table>
+ </div>
<#if model.isEmpty()>
<div class="jumbotron">
<p class="lead">No result found.</p>
@@ -59,11 +75,64 @@
$('#result').dataTable( {
"aaSorting": [],
"scrollX" : true,
+ "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
+ "lengthChange": true,
"dom": '<"H"lCfr>t<"F"ip>',
"jQueryUI" : true
} );
} );
- </script>
+
+ //Pop out profile (needed to avoid losing query results)
+ function popOutProfile(queryId) {
+ var profileUrl = location.protocol+'//'+
location.host+'/profiles/'+queryId;
+ var tgtWindow = '_blank';
+ window.open(profileUrl, tgtWindow);
+ }
+
+ //Ref: https://jsfiddle.net/gengns/j1jm2tjx/
+ function downloadCsv(csvRecords, filename) {
+ var csvFile;
+ var downloadElem;
+
+ //CSV File
+ csvFile = new Blob([csvRecords], {type: "text/csv"});
+ // Download link
+ downloadElem = document.createElement("a");
+ // File name
+ downloadElem.download = filename;
+
+ // We have to create a link to the file
+ downloadElem.href = window.URL.createObjectURL(csvFile);
+
+ // Make sure that the link is not displayed
+ downloadElem.style.display = "none";
+
+ // Add the link to your DOM
+ document.body.appendChild(downloadElem);
+
+ // Launch the download prompt
+ downloadElem.click();
+ }
+
+ function exportTableAsCsv(queryId) {
+ var filename = queryId + '.csv';
+ var csv = []; //Array of records
+ var rows = document.getElementById('result').querySelectorAll("tr");
+ var delimiter = document.getElementById('delimitBy').value;
+ if (delimiter == 'undefined' || delimiter.length==0) {
+ delimiter = ",";
+ }
+ for (var i = 0; i < rows.length; i++) {
+ var row = [], cols = rows[i].querySelectorAll("th, td");
+ for (var j = 0; j < cols.length; j++)
+ row.push(cols[j].textContent);
+ csv.push(row.join(delimiter));
+ }
+ // Download CSV
+ downloadCsv(csv.join("\n"), filename);
+ }
+
+ </script>
</#macro>
<@page_html/>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Export query result as a CSV file
> ---------------------------------
>
> Key: DRILL-6423
> URL: https://issues.apache.org/jira/browse/DRILL-6423
> Project: Apache Drill
> Issue Type: New Feature
> Components: Web Server
> Reporter: Kunal Khatua
> Assignee: Kunal Khatua
> Priority: Major
> Labels: ready-to-commit
> Fix For: 1.14.0
>
>
> This feature request is based on a question posted in the user mailing list:
> [is there any way to download the data through Drill Web
> UI?|https://lists.apache.org/thread.html/bd6b2453c83d818b618ec6334891309f42556d4a7de34d84024a43cd@<user.drill.apache.org>]
> Since the results of the WebUI allows for sorting and filtering as well, it
> can be very useful if there is a way for a user to download the results of an
> executed query for further processing in other tools (like Excel)
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)