forked from ghdna/athena-express
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathathenaExpress.js
149 lines (131 loc) · 4.81 KB
/
athenaExpress.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";
//athenaExpress.js
const COST_PER_MB = 0.000004768, //Based on $5/TB
BYTES_IN_MB = 1048576,
COST_FOR_10MB = COST_PER_MB * 10;
const helpers = require("./helpers.js");
module.exports = class AthenaExpress {
constructor(init) {
helpers.validateConstructor(init);
this.config = {
athena: init.athena,
s3: init.s3,
s3Bucket: init.s3Bucket,
encryption: init.encryption,
db: init.db || "default",
catalog: init.catalog || null,
sql: null,
workgroup: init.workgroup || "primary",
retry: Number(init.retry) || 200,
formatJson: init.formatJson !== false,
getStats: init.getStats || init.skipResults,
ignoreEmpty: init.ignoreEmpty !== false,
skipResults: init.skipResults,
waitForResults: init.waitForResults !== false,
QueryExecutionId: null,
pagination: Number(init.pagination) || 0,
NextToken: init.nextToken || null,
};
}
/**
* Executes an Athena SQL Query.
*
* If the 'query' parameter is a string, SQL parameters should be
* passed in via the 'queryParams' optional parameter.
*
* If the 'query' parameter is an object, parameters should be
* passed in via the 'query.QueryParams' optional array.
*
* Examples:
* await athenaExpress.query('SELECT * FROM movies WHERE movie_title = ?', ['Spider-Man']);
* await athenaExpress
* .query({ sql: 'SELECT * FROM movies WHERE movie_title = ?', QueryParams: ['Spider-Man']});
*
* @param {String|Object} query
* @param {Array} [queryParams=[]] Optional
* @returns {Promise<Object>}
*/
async query(query, queryParams) {
const config = { ...this.config };
let initiateQueryInAthena = true;
let results = {};
if (!config)
throw new TypeError("Config object not present in the constructor");
if (!query) throw new TypeError("SQL query is missing");
if (typeof query === "object") {
const loweredCaseKeys = helpers.lowerCaseKeys(query);
if (loweredCaseKeys.hasOwnProperty("nexttoken")) {
config.NextToken = loweredCaseKeys.nexttoken;
}
if (loweredCaseKeys.hasOwnProperty("pagination")) {
config.pagination = loweredCaseKeys.pagination;
}
if (loweredCaseKeys.hasOwnProperty("db")) {
config.db = loweredCaseKeys.db;
}
if (loweredCaseKeys.hasOwnProperty("catalog")) {
config.catalog = loweredCaseKeys.catalog;
}
if (loweredCaseKeys.hasOwnProperty("queryparams")) {
config.QueryParams = loweredCaseKeys.queryparams;
}
if (loweredCaseKeys.hasOwnProperty("sql")) {
config.sql = loweredCaseKeys.sql;
}
if (loweredCaseKeys.hasOwnProperty("queryexecutionid")) {
config.QueryExecutionId = loweredCaseKeys.queryexecutionid;
initiateQueryInAthena = false;
}
} else if (query.trim().length === 36 && query.trim().indexOf(" ") === -1) {
//indicates that the query is actually a string containing just the QueryExecutionId
initiateQueryInAthena = false;
config.QueryExecutionId = query;
} else {
config.sql = query;
config.QueryParams = queryParams;
}
try {
if (initiateQueryInAthena) {
config.QueryExecutionId = await helpers.startQueryExecution(config);
if (!config.waitForResults) {
results.QueryExecutionId = config.QueryExecutionId;
return results;
}
}
const queryStatus = await helpers.checkIfExecutionCompleted(config);
const s3Output =
queryStatus.QueryExecution.ResultConfiguration.OutputLocation,
statementType = queryStatus.QueryExecution.StatementType;
if (!config.skipResults || !initiateQueryInAthena) {
if (/.txt/.test(s3Output) || /.csv/.test(s3Output)) {
const queryResultsFromS3 = await helpers.getQueryResultsFromS3({
s3Output,
statementType,
config,
});
results.Items = queryResultsFromS3.items;
if (queryResultsFromS3.nextToken) {
results.NextToken = queryResultsFromS3.nextToken;
results.QueryExecutionId = config.QueryExecutionId;
}
}
}
if (config.getStats) {
const statistics = queryStatus.QueryExecution.Statistics;
results = Object.assign(results, statistics);
const dataInMb = Math.round(
queryStatus.QueryExecution.Statistics.DataScannedInBytes / BYTES_IN_MB
);
results.DataScannedInMB = dataInMb;
results.QueryCostInUSD =
dataInMb > 10 ? dataInMb * COST_PER_MB : COST_FOR_10MB;
results.Count = results.Items ? results.Items.length : 0;
results.QueryExecutionId = config.QueryExecutionId;
results.S3Location = s3Output;
}
return results;
} catch (error) {
throw new Error(error);
}
}
};