CreateNamedQueryExample.java - AWS Code Sample

CreateNamedQueryExample.java

CreateNamedQueryExample.java demonstrates how to create a named query

/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package aws.example.athena; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.AthenaException; import software.amazon.awssdk.services.athena.model.CreateNamedQueryRequest; import software.amazon.awssdk.services.athena.model.CreateNamedQueryResponse; /** * CreateNamedQueryExample * ------------------------------------- * This code shows how to create a named query. */ public class CreateNamedQueryExample { public static void main(String[] args) throws Exception { final String USAGE = "\n" + "Usage:\n" + " CreateNamedQueryExample <name>\n\n" + "Where:\n" + " name - the name of the query \n\n" + "Example:\n" + " DescribeTable SampleQuery\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } /* Read the name from command args */ String name = args[0]; // Build an Athena client AthenaClient athenaClient = AthenaClient.builder() .region(Region.US_WEST_2) .build(); createNamedQuery(athenaClient, name); } public static void createNamedQuery(AthenaClient athenaClient, String name) { try { // Create the named query request. CreateNamedQueryRequest createNamedQueryRequest = CreateNamedQueryRequest.builder() .database(ExampleConstants.ATHENA_DEFAULT_DATABASE) .queryString(ExampleConstants.ATHENA_SAMPLE_QUERY) .description("Sample Description") .name(name) .build(); // Call Athena to create the named query. If it fails, an exception is thrown. CreateNamedQueryResponse createNamedQueryResult = athenaClient.createNamedQuery(createNamedQueryRequest); System.out.println("Done"); } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } } }

Sample Details

Service: athena

Last tested: 4/15/2020

Author: scmacdon AWS

Type: full-example