aws / aws-sdk

Landing page for the AWS SDKs on GitHub
https://aws.amazon.com/tools/
Other
68 stars 12 forks source link

would like to see Add GetNamedQuery By QueryName #753

Closed Diwamoto closed 4 days ago

Diwamoto commented 1 month ago

Describe the feature

hello. It is an automatic translation and may result in strange English. Sorry.

I would like to be able to search an Athena NamedQuery by the Name of the Query. The current implementation of GetNamedQuery() is only from the auto-generated query_id, and it is difficult to keep track of this id from the program.

ref: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/athena/get-named-query.html

aws athena get-named-query --named-query-id <auto-generated-id>

Also, ListNamedQueries() must be implemented to retrieve this ID programmatically, but the response from this API is only a list of IDs, and we do not know which ID is the desired query.

ref: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/athena/list-named-queries.html

aws athena list-named-queries
--> 
{
    "NamedQueryIds": [
        "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
        "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222",
        "a1b2c3d4-5678-90ab-cdef-EXAMPLE33333"
    ]
}

(For example, if the name of the query as well as the ID is recorded here, you can use the query option of the AWS CLI to filter the results.)

Use Case

Looking at the implementation of getting NamedQuery in AWS Athena Console from Chrome Dev tools, it executes all ListNamedQueries and then GetNamedQuery one by one, which becomes more inefficient as the number of NamedQueries increases.

Proposed Solution

I would like to see GetNamedQueryByName added or name added to the ListNamedQueries response.

aws athena get-named-query --named-query-name <query_name>
aws athena list-named-queries
--> 
{
    "NamedQueries": [
        {
            "Id": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
            "Name": "named_query_example_1"
        },
        {
            "Id": "a1b2c3d4-5678-90ab-cdef-EXAMPLE22222",
            "Name": "named_query_example_2"
        },
        {
            "Id": "a1b2c3d4-5678-90ab-cdef-EXAMPLE33333",
            "Name": "named_query_example_3"
        }
    ]
}

Changing the response of ListNamedQueries would lose backward compatibility, so if anything we should add GetNamedQueryByName or another solution.

Other Information

No response

Acknowledgements

CLI version used

aws-cli/2.15.36 Python/3.11.9 Darwin/23.4.0 source/arm64 prompt/off

Environment details (OS name and version, etc.)

Mac OS Sonoma 14.4.1

tim-finnigan commented 1 month ago

Thanks for the feature request. The Athena team maintains the underlying GetNamedQuery and ListNamedQueries APIs, so we would have to forward feature requests involving the API behavior to them.


Changing the response of ListNamedQueries would lose backward compatibility, so if anything we should add GetNamedQueryByName or another solution.

As you suggested above, creating a new API may be the best solution due to the need to maintain backwards compatibility.

To make sure that we correctly understand the ask here, can you verify that the following accomplishes what you're trying to do using Boto3:

import boto3

client = boto3.client('athena')
response = client.list_named_queries()

query_ids = response['NamedQueryIds']

named_queries = [client.get_named_query(NamedQueryId=id)['NamedQuery'] for id in query_ids]

filtered_queries = [q for q in named_queries if q['Name'] == '<query name>']

for query in filtered_queries:
    print(query)

Or using a bash script:

#!/bin/bash

# Set the query name to filter for
query_name="<query name>"

# List named queries
named_query_ids=$(aws athena list-named-queries --query "NamedQueryIds" --output text)

# Loop through the named query IDs
for query_id in $named_query_ids
do
    # Get the named query details
    named_query=$(aws athena get-named-query --named-query-id "$query_id")

    # Extract the query name
    query_name_from_response=$(echo "$named_query" | jq -r '.NamedQuery.Name')

    # Check if the query name matches the filter
    if [ "$query_name_from_response" == "$query_name" ]; then
        echo "$named_query"
    fi
done

I think those workarounds meet your use case, but we can still forward the feature request to the Athena team for review.

Diwamoto commented 1 month ago

Yes, it can be accomplished with both the boto3 and shell scripts listed. (The AWS Athena console implementation was the same.)

That is where I would like to write this if possible (I will show an example with boto3)

import boto3

already_known_query_name = 'the query'

client = boto3.client('athena')
response = client.get_named_queries(name=already_known_query_name)

print(response[0]) # the query data
tim-finnigan commented 1 month ago

Thanks for confirming, I'll transfer this to our cross-SDK repository (since service APIs like this are used across AWS SDKs) and I'll reach out to the Athena team for feedback on this feature request. (ref: P131190888)

tim-finnigan commented 4 days ago

Thanks for your patience. The Athena team has added this feature request to their backlog for further review and consideration. We can't provide any guarantees on if or when this would be added, but please refer to the Athena Blog and CHANGELOG for related updates.

github-actions[bot] commented 4 days ago

This issue is now closed.

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.