Created
July 25, 2023 07:15
-
-
Save titanjer/6dab817a63fb88ac27c3e35eb064d2a8 to your computer and use it in GitHub Desktop.
athena.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
AthenaClient, | |
GetQueryExecutionCommand, | |
QueryExecutionState, | |
StartQueryExecutionCommand | |
} from "@aws-sdk/client-athena"; | |
const client = new AthenaClient({}); | |
const runAthenaQuery = async (queryString: string, workGroup: string): Promise<any> => { | |
// run query | |
const { QueryExecutionId: queryExecutionId } = await client.send( | |
new StartQueryExecutionCommand({ | |
QueryString: queryString, | |
WorkGroup: workGroup | |
}) | |
); | |
if (queryExecutionId === undefined) { | |
throw new Error('QueryExecutionId is undefined.'); | |
} | |
console.log('queryExecutionId [ %s ]', queryExecutionId); | |
// wait query | |
let queryStatusState = QueryExecutionState.RUNNING; | |
while (queryStatusState === QueryExecutionState.RUNNING) { | |
await new Promise((resolve) => setTimeout(resolve, 1000)); | |
const { QueryExecution } = await client.send( | |
new GetQueryExecutionCommand({ | |
QueryExecutionId: queryExecutionId, | |
}) | |
); | |
if ( | |
QueryExecution == undefined || | |
QueryExecution.Status == undefined || | |
QueryExecution.Status.State == undefined | |
) { | |
throw new Error('QueryExecution is undefined.'); | |
} | |
queryStatusState = QueryExecution.Status.State as QueryExecutionState; | |
console.log('queryStatusState [ %s ]', queryStatusState); | |
} | |
if (queryStatusState !== QueryExecutionState.SUCCEEDED) { | |
throw new Error( | |
`Query ${queryExecutionId} failed with status ${queryStatusState}` | |
); | |
} | |
} | |
(async () => { | |
const workGroup = 'core_eddiedev'; | |
const queryString = ` | |
CREATE TABLE IF NOT EXISTS core_eddiedev.ddb_iceberg ( | |
_type string, | |
pk string, | |
sk string, | |
filtertype string, | |
filternames string, | |
filterstrings string, | |
created timestamp, | |
updated timestamp, | |
item string | |
) | |
# PARTITIONED BY (_type) | |
LOCATION 's3://atsoul/_dev-ddb_iceberg' | |
TBLPROPERTIES ( | |
'table_type'='ICEBERG', | |
'format'='parquet' | |
) | |
` | |
await runAthenaQuery(queryString, workGroup) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment