1
- //snippet-sourcedescription:[StartQueryExample.java demonstrates how to submit a query to Athena for execution, wait till results are available, and then process the results.]
1
+ //snippet-sourcedescription:[StartQueryExample.java demonstrates how to submit a query to Amazon Athena for execution, wait until results are available, and then process the results.]
2
2
//snippet-keyword:[Java]
3
3
//snippet-sourcesyntax:[java]
4
4
//snippet-keyword:[Code Sample]
27
27
/**
28
28
* StartQueryExample
29
29
* -------------------------------------
30
- * This code shows how to submit a query to Athena for execution, wait till results
30
+ * This code shows how to submit a query to Amazon Athena for execution, wait until results
31
31
* are available, and then process the results.
32
32
*/
33
33
public class StartQueryExample
34
34
{
35
35
public static void main (String [] args ) throws InterruptedException
36
36
{
37
- // Build an AmazonAthena client
37
+ // Build an Athena client.
38
38
AthenaClientFactory factory = new AthenaClientFactory ();
39
39
AmazonAthena athenaClient = factory .createClient ();
40
40
@@ -50,16 +50,16 @@ public static void main(String[] args) throws InterruptedException
50
50
*/
51
51
private static String submitAthenaQuery (AmazonAthena athenaClient )
52
52
{
53
- // The QueryExecutionContext allows us to set the Database .
53
+ // The QueryExecutionContext allows us to set the database .
54
54
QueryExecutionContext queryExecutionContext = new QueryExecutionContext ().withDatabase (ExampleConstants .ATHENA_DEFAULT_DATABASE );
55
55
56
- // The result configuration specifies where the results of the query should go in S3 and encryption options
56
+ // The result configuration specifies where the results of the query should go in Amazon S3 and encryption options.
57
57
ResultConfiguration resultConfiguration = new ResultConfiguration ()
58
58
// You can provide encryption options for the output that is written.
59
59
// .withEncryptionConfiguration(encryptionConfiguration)
60
60
.withOutputLocation (ExampleConstants .ATHENA_OUTPUT_BUCKET );
61
61
62
- // Create the StartQueryExecutionRequest to send to Athena which will start the query.
62
+ // Create the StartQueryExecutionRequest to send to Athena, which will start the query.
63
63
StartQueryExecutionRequest startQueryExecutionRequest = new StartQueryExecutionRequest ()
64
64
.withQueryString (ExampleConstants .ATHENA_SAMPLE_QUERY )
65
65
.withQueryExecutionContext (queryExecutionContext )
@@ -70,8 +70,8 @@ private static String submitAthenaQuery(AmazonAthena athenaClient)
70
70
}
71
71
72
72
/**
73
- * Wait for an Athena query to complete, fail or to be cancelled . This is done by polling Athena over an
74
- * interval of time. If a query fails or is cancelled, then it will throw an exception.
73
+ * Wait for an Athena query to complete or fail, or to be canceled . This is done by polling Athena over an
74
+ * interval of time. If a query fails or is canceled, it will throw an exception.
75
75
*/
76
76
77
77
private static void waitForQueryToComplete (AmazonAthena athenaClient , String queryExecutionId ) throws InterruptedException
@@ -85,19 +85,19 @@ private static void waitForQueryToComplete(AmazonAthena athenaClient, String que
85
85
getQueryExecutionResult = athenaClient .getQueryExecution (getQueryExecutionRequest );
86
86
String queryState = getQueryExecutionResult .getQueryExecution ().getStatus ().getState ();
87
87
if (queryState .equals (QueryExecutionState .FAILED .toString ())) {
88
- throw new RuntimeException ("Query Failed to run with Error Message : " + getQueryExecutionResult .getQueryExecution ().getStatus ().getStateChangeReason ());
88
+ throw new RuntimeException ("Query failed to run with error message : " + getQueryExecutionResult .getQueryExecution ().getStatus ().getStateChangeReason ());
89
89
}
90
90
else if (queryState .equals (QueryExecutionState .CANCELLED .toString ())) {
91
- throw new RuntimeException ("Query was cancelled ." );
91
+ throw new RuntimeException ("Query was canceled ." );
92
92
}
93
93
else if (queryState .equals (QueryExecutionState .SUCCEEDED .toString ())) {
94
94
isQueryStillRunning = false ;
95
95
}
96
96
else {
97
- // Sleep an amount of time before retrying again .
97
+ // Sleep an amount of time before retrying.
98
98
Thread .sleep (ExampleConstants .SLEEP_AMOUNT_IN_MS );
99
99
}
100
- System .out .println ("Current Status is: " + queryState );
100
+ System .out .println ("Current state is: " + queryState );
101
101
}
102
102
}
103
103
@@ -109,8 +109,8 @@ else if (queryState.equals(QueryExecutionState.SUCCEEDED.toString())) {
109
109
private static void processResultRows (AmazonAthena athenaClient , String queryExecutionId )
110
110
{
111
111
GetQueryResultsRequest getQueryResultsRequest = new GetQueryResultsRequest ()
112
- // Max Results can be set but if its not set,
113
- // it will choose the maximum page size
112
+ // MaxResults can be set, but if it is not set,
113
+ // it will choose the maximum page size.
114
114
// As of the writing of this code, the maximum value is 1000
115
115
// .withMaxResults(1000)
116
116
.withQueryExecutionId (queryExecutionId );
@@ -138,34 +138,34 @@ private static void processRow(Row row, List<ColumnInfo> columnInfoList)
138
138
for (int i = 0 ; i < columnInfoList .size (); ++i ) {
139
139
switch (columnInfoList .get (i ).getType ()) {
140
140
case "varchar" :
141
- // Convert and Process as String
141
+ // Convert and process as String.
142
142
break ;
143
143
case "tinyint" :
144
- // Convert and Process as tinyint
144
+ // Convert and process as tinyint.
145
145
break ;
146
146
case "smallint" :
147
- // Convert and Process as smallint
147
+ // Convert and process as smallint.
148
148
break ;
149
149
case "integer" :
150
- // Convert and Process as integer
150
+ // Convert and process as integer.
151
151
break ;
152
152
case "bigint" :
153
- // Convert and Process as bigint
153
+ // Convert and process as bigint.
154
154
break ;
155
155
case "double" :
156
- // Convert and Process as double
156
+ // Convert and process as double.
157
157
break ;
158
158
case "boolean" :
159
- // Convert and Process as boolean
159
+ // Convert and process as boolean.
160
160
break ;
161
161
case "date" :
162
- // Convert and Process as date
162
+ // Convert and process as date.
163
163
break ;
164
164
case "timestamp" :
165
- // Convert and Process as timestamp
165
+ // Convert and process as timestamp.
166
166
break ;
167
167
default :
168
- throw new RuntimeException ("Unexpected Type is not expected" + columnInfoList .get (i ).getType ());
168
+ throw new RuntimeException ("Unexpected type is not expected" + columnInfoList .get (i ).getType ());
169
169
}
170
170
}
171
171
}
0 commit comments