Skip to content

Commit 29babfd

Browse files
committed
[Add] - athena functions
1 parent d6b27c8 commit 29babfd

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

services/athena/aws_athena.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
3+
###################################################################
4+
# # @script aws_athena.sh
5+
# # @author lamhaison
6+
7+
###################################################################
8+
9+
#!/bin/bash
10+
11+
# Function to start an AWS Athena query
12+
aws_athena_run_query() {
13+
local query_string="$1" # The SQL query to execute
14+
local s3_output_path="$2" # The S3 path for query results
15+
16+
# Validate required parameters
17+
if [[ -z "$query_string" || -z "$s3_output_path" ]]; then
18+
echo "Error: Missing required arguments."
19+
echo "Usage: aws_athena_run_query <query_string> <s3_output_path>"
20+
return 1
21+
fi
22+
23+
# Start the Athena query and get the query execution ID
24+
local query_execution_id
25+
query_execution_id=$(aws athena start-query-execution \
26+
--query-string "$query_string" \
27+
--result-configuration "OutputLocation=$s3_output_path" \
28+
--query "QueryExecutionId" \
29+
--output text 2>&1)
30+
31+
# Check for errors
32+
if [[ $? -ne 0 ]]; then
33+
echo "Error starting the query: $query_execution_id"
34+
return 1
35+
fi
36+
37+
echo "$query_execution_id"
38+
}
39+
40+
# Function to get AWS Athena query results
41+
aws_athena_get_query() {
42+
local query_execution_id="$1" # The query execution ID
43+
local region="$2" # Optional: AWS region (default is set in AWS CLI config)
44+
45+
# Validate required parameters
46+
if [[ -z "$query_execution_id" ]]; then
47+
echo "Error: Missing required argument."
48+
echo "Usage: aws_athena_get_query <query_execution_id> [region]"
49+
return 1
50+
fi
51+
52+
# Wait for the query to complete
53+
local query_status="RUNNING"
54+
while [[ "$query_status" == "RUNNING" || "$query_status" == "QUEUED" ]]; do
55+
echo "Waiting for query to complete..."
56+
sleep 2
57+
query_status=$(aws athena get-query-execution \
58+
--query-execution-id "$query_execution_id" \
59+
--query "QueryExecution.Status.State" \
60+
--output text --region "$region" 2>&1)
61+
done
62+
63+
# Check if the query succeeded
64+
if [[ "$query_status" != "SUCCEEDED" ]]; then
65+
echo "Error: Query did not succeed. Status: $query_status"
66+
return 1
67+
fi
68+
69+
# Retrieve query results
70+
local results
71+
results=$(aws athena get-query-results \
72+
--query-execution-id "$query_execution_id" \
73+
--output json --region "$region" 2>&1)
74+
75+
if [[ $? -ne 0 ]]; then
76+
echo "Error retrieving query results: $results"
77+
return 1
78+
fi
79+
80+
echo "$results"
81+
}

0 commit comments

Comments
 (0)