AWS Athena CTAS查询控制台正常,Boto3调用报错
解决AWS Athena CTAS语句Boto3调用报错"Queries of this type are not supported"的问题
可能的原因及修复方案
1. 移除CTAS语句中的ORDER BY子句
Athena的CTAS查询不支持在语句中直接使用ORDER BY——虽然控制台可能允许执行,但API调用时会触发类型不支持的错误。CTAS的核心是创建表,表数据本身无序,排序逻辑应放在后续查询中实现。
修改后的CTAS语句:
CREATE TABLE "ave_calc_tb_xyzint_1" AS SELECT substring(created_at, 2,8) day, AVG(sentimentscore_positive) FILTER (WHERE emotion_happy > 0 AND emotion_happy >= emotion_angry AND emotion_happy >= emotion_surprise AND emotion_happy >= emotion_sad AND emotion_happy >= emotion_fear) ave_pos, AVG(sentimentscore_neutral) FILTER (WHERE emotion_happy > 0 AND emotion_happy >= emotion_angry AND emotion_happy >= emotion_surprise AND emotion_happy >= emotion_sad AND emotion_happy >= emotion_fear) ave_neu, AVG(sentimentscore_negative) FILTER (WHERE emotion_happy > 0 AND emotion_happy >= emotion_angry AND emotion_happy >= emotion_surprise AND emotion_happy >= emotion_sad AND emotion_happy >= emotion_fear) ave_neg, count(sentiment) FILTER (WHERE sentiment =1 ) sent_pos_cnt, count(sentiment) FILTER (WHERE sentiment =0 ) sent_neu_cnt, count(sentiment) FILTER (WHERE sentiment =-1 ) sent_neg_cnt, count(*) tweet_count, AVG(sentimentscore_positive) ave_pos_all, AVG(sentimentscore_neutral) ave_neu_all, AVG(sentimentscore_negative) ave_neg_all FROM "db_xyzint_1"."unique_tb_xyzint_1" GROUP BY substring(created_at, 2,8)
2. 显式指定表存储位置(LOCATION)
控制台会自动使用数据库默认存储位置,但Boto3调用时,显式添加LOCATION参数可避免解析异常:
CREATE TABLE "ave_calc_tb_xyzint_1" WITH ( location = 's3://你的存储桶/表存储路径/' ) AS -- 后续SELECT语句同上
3. 调整Boto3调用参数
CTAS查询的结果是创建表,不需要在ResultConfiguration中指定OutputLocation(该参数用于普通SELECT查询的结果文件输出)。正确的调用示例:
import boto3 client = boto3.client('athena') # CTAS查询调用 response = client.start_query_execution( QueryString='你的CTAS语句', QueryExecutionContext={ 'Database': 'db_xyzint_1' }, ResultConfiguration={} )
4. 优化标识符引号使用
Athena中双引号用于区分大小写的表/列名,若你的标识符无需大小写敏感,可去掉双引号(或改用反引号),避免解析冲突:
CREATE TABLE ave_calc_tb_xyzint_1 AS SELECT ... FROM db_xyzint_1.unique_tb_xyzint_1 ...
内容的提问来源于stack exchange,提问作者Ben
相关产品推荐
相关产品推荐

