vogievetsky commented on a change in pull request #7898: Web console: Add
download path to SQL Query
URL: https://github.com/apache/incubator-druid/pull/7898#discussion_r294026884
##########
File path: web-console/src/views/sql-view/sql-view.tsx
##########
@@ -169,6 +170,42 @@ export class SqlView extends
React.PureComponent<SqlViewProps, SqlViewState> {
localStorageSet(LocalStorageKeys.QUERY_VIEW_PANE_SIZE,
String(secondaryPaneSize));
}
+ formatStr(s: string | number, format: 'csv' | 'tsv') {
+ if (format === 'csv') {
+ // remove line break, single quote => double quote, handle ','
+ return `"${s.toString().replace(/(?:\r\n|\r|\n)/g, ' ').replace(/"/g,
'""')}"`;
+ } else { // tsv
+ // remove line break, single quote => double quote, \t => ''
+ return `${s.toString().replace(/(?:\r\n|\r|\n)/g, ' ').replace(/\t/g,
'').replace(/"/g, '""')}`;
+ }
+ }
+
+ onDownload = (format: string) => {
+ const { result } = this.state;
+ if (!result) return;
+ let data: string = '';
+ let seperator: string = '';
+ const lineBreak = '\n';
+
+ if (format === 'csv' || format === 'tsv') {
+ seperator = format === 'csv' ? ',' : '\t';
+ data = result.header.map(str => this.formatStr(str,
format)).join(seperator) + lineBreak;
+ data += result.rows.map(r => r.map(cell => this.formatStr(cell,
format)).join(seperator)).join(lineBreak);
+ } else { // json
+ data = result.rows.map(r => {
+ const outputObject: Record<string, any> = {};
+ for (let k = 0; k < r.length; k++) {
+ const newName = result.header[k];
+ if (newName) {
+ outputObject[newName] = r[k];
+ }
+ }
+ return JSON.stringify(outputObject, null, 2);
Review comment:
This should be `JSON.stringify(outputObject)` to get [line
json](http://jsonlines.org/)
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]