-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathathena-helpers.ts
More file actions
61 lines (52 loc) · 1.58 KB
/
athena-helpers.ts
File metadata and controls
61 lines (52 loc) · 1.58 KB
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
import {
AthenaClient,
GetQueryExecutionCommand,
QueryExecutionState,
StartQueryExecutionCommand,
} from '@aws-sdk/client-athena';
export { QueryExecutionState } from '@aws-sdk/client-athena';
const client = new AthenaClient();
/**
* Triggers a metadata refresh for an Athena table using the MSCK REPAIR TABLE command.
*
* This will cause any new files in S3 to be picked up.
*
* @param database - The name of the Athena database
* @param tableName - The name of the table to repair
* @param workgroup - The Athena workgroup to run the query in
* @returns The query execution ID
*/
export async function triggerTableMetadataRefresh(
database: string,
tableName: string,
workgroup: string,
): Promise<string> {
const command = new StartQueryExecutionCommand({
QueryString: `MSCK REPAIR TABLE ${tableName};`,
QueryExecutionContext: {
Database: database,
Catalog: 'AwsDataCatalog',
},
WorkGroup: workgroup,
});
const response = await client.send(command);
if (!response.QueryExecutionId) {
throw new Error(
'Failed to start query execution - no query execution ID returned',
);
}
return response.QueryExecutionId;
}
export async function getQueryState(
queryExecutionId: string,
): Promise<QueryExecutionState> {
const queryExecutionInfo = await client.send(
new GetQueryExecutionCommand({
QueryExecutionId: queryExecutionId,
}),
);
if (!queryExecutionInfo.QueryExecution?.Status?.State) {
throw new Error('Failed to get query execution state');
}
return queryExecutionInfo.QueryExecution?.Status?.State;
}