本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
DeleteNamedQueryExample
說明如何使用具名查詢 ID 來刪除具名查詢。
package aws.example.athena;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.athena.AthenaClient;
import software.amazon.awssdk.services.athena.model.DeleteNamedQueryRequest;
import software.amazon.awssdk.services.athena.model.AthenaException;
import software.amazon.awssdk.services.athena.model.CreateNamedQueryRequest;
import software.amazon.awssdk.services.athena.model.CreateNamedQueryResponse;
/**
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class DeleteNamedQueryExample {
public static void main(String[] args) {
final String USAGE = """
Usage:
<name>
Where:
name - the name of the Amazon Athena query.\s
""";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String name = args[0];
AthenaClient athenaClient = AthenaClient.builder()
.region(Region.US_WEST_2)
.build();
String sampleNamedQueryId = getNamedQueryId(athenaClient, name);
deleteQueryName(athenaClient, sampleNamedQueryId);
athenaClient.close();
}
public static void deleteQueryName(AthenaClient athenaClient, String sampleNamedQueryId) {
try {
DeleteNamedQueryRequest deleteNamedQueryRequest = DeleteNamedQueryRequest.builder()
.namedQueryId(sampleNamedQueryId)
.build();
athenaClient.deleteNamedQuery(deleteNamedQueryRequest);
} catch (AthenaException e) {
e.printStackTrace();
System.exit(1);
}
}
public static String getNamedQueryId(AthenaClient athenaClient, String name) {
try {
CreateNamedQueryRequest createNamedQueryRequest = CreateNamedQueryRequest.builder()
.database(ExampleConstants.ATHENA_DEFAULT_DATABASE)
.queryString(ExampleConstants.ATHENA_SAMPLE_QUERY)
.name(name)
.description("Sample description")
.build();
CreateNamedQueryResponse createNamedQueryResponse = athenaClient.createNamedQuery(createNamedQueryRequest);
return createNamedQueryResponse.namedQueryId();
} catch (AthenaException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
}