如何在AWS Athena分区表上查询

如何解决如何在AWS Athena分区表上查询

问题摘要

当我尝试使用SELECT子句WHERE查询分区表时,Athena会产生错误。

log表中有4种分区。

  • 年份string
  • string
  • string
  • 小时string

我在分区表上尝试过SELECT查询。 但是收到了以下错误消息。

错误消息

GENERIC_INTERNAL_ERROR: No value present
This query ran against the "default" database,unless qualified by the query.

选择我尝试过的查询

SELECT *
FROM logs
WHERE year='2020'
  AND month='10'
  AND day ='05';

AND

SELECT *
FROM "default"."logs"
WHERE year='2020'
  AND month='10'
  AND day ='05';

由于关于No value present的错误消息,我检查了分区结果。

SHOW PARTITIONS logs;

结果

year=2020/month=10/day=05/hour=17
year=2020/month=10/day=05/hour=11
year=2020/month=10/day=05/hour=19
year=2020/month=10/day=05/hour=04
year=2020/month=10/day=05/hour=18
year=2020/month=10/day=05/hour=15
year=2020/month=10/day=05/hour=14
year=2020/month=10/day=05/hour=16
year=2020/month=10/day=05/hour=13
year=2020/month=10/day=05/hour=21
year=2020/month=10/day=05/hour=05
year=2020/month=10/day=05/hour=08
year=2020/month=10/day=05/hour=20
year=2020/month=10/day=05/hour=12
year=2020/month=10/day=05/hour=03
year=2020/month=10/day=05/hour=01
year=2020/month=10/day=05/hour=10
year=2020/month=10/day=05/hour=02
year=2020/month=10/day=05/hour=09
year=2020/month=10/day=05/hour=22
year=2020/month=10/day=05/hour=23
year=2020/month=10/day=05/hour=06
year=2020/month=10/day=05/hour=07
year=2020/month=10/day=05/hour=00
year=2020/month=10/day=04/hour=00

非常感谢您的帮助。


其他信息

我使用的

CREATE TABLE命令

创建表

CREATE EXTERNAL TABLE `logs`(
  `date` date,`time` string,`location` string,`bytes` bigint,`request_ip` string,`method` string,`host` string,`uri` string,`status` int,`referrer` string,`user_agent` string,`query_string` string,`cookie` string,`result_type` string,`request_id` string,`host_header` string,`request_protocol` string,`request_bytes` bigint,`time_taken` float,`xforwarded_for` string,`ssl_protocol` string,`ssl_cipher` string,`response_result_type` string,`http_version` string,`fle_status` string,`fle_encrypted_fields` int)
PARTITIONED BY ( 
  `year` string,`month` string,`day` string,`hour` string)
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\t' 
WITH SERDEPROPERTIES ( 
  'input.regex'='^(?!#)([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)\\\\s+([^ \\\\t]+)$') 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  's3://mybucket/path'
TBLPROPERTIES (
  'projection.date.format'='yyyy/MM/dd','projection.date.interval'='1','projection.date.interval.unit'='DAYS','projection.date.range'='2019/11/27,NOW-1DAYS','projection.date.type'='date','projection.day.type'='string','projection.enabled'='true','projection.hour.type'='string','projection.month.type'='string','projection.year.type'='string','skip.header.line.count'='2','storage.location.template'='s3://mybucket/path/distributionID/${year}/${month}/${day}/${hour}/','transient_lastDdlTime'='1575005094')

解决方法

您的表使用分区投影,但是您的配置与分区不正确匹配。分区投影是一个相当新的功能,文档仍然有一些不足之处,因此我完全理解它令人困惑。我想我知道您要做什么。

分区投影配置必须与表的分区键完全匹配。在您的情况下,表有四个分区键,分区投影配置提到了五个。除了四个类型错误之外,没有string分区投影类型。

您可以通过进行两项更改来解决问题。首先像这样更改分区键:

PARTITIONED BY (
  `date` string,`hour` string
)

这将删除“年”,“月”和“日”分区键,而使用“日期”键。不必仅因为它们是单独的“目录”而具有单独的日期组件,而仅具有“ date”键将使查询更容易编写。

然后将表属性更改为此:

TBLPROPERTIES (
  'projection.date.format' = 'yyyy/MM/dd','projection.date.interval' = '1','projection.date.interval.unit' = 'DAYS','projection.date.range' = '2019/11/27,NOW-1DAYS','projection.date.type' = 'date','projection.hour.type' = 'integer','projection.hour.range' = '0-23','projection.hour.digits' = '2'
  'projection.enabled' = 'true','storage.location.template'='s3://mybucket/path/distributionID/${date}/${hour}/','skip.header.line.count' = '2'
)

这告诉Athena,“日期”分区键的类型为date,其格式设置为“ YYYY / MM / DD”(与S3 URI中的格式相对应,这一点很重要)。它还告诉Athena,“小时”分区键是范围为0-23的integer,格式为两位数(即零填充)。最后,它指定这些分区键如何映射到S3上的分区位置。当查询中的日期为“ 2020/10/06”时,该字符串将逐字插入位置模板中。

通过这些更改,您应该能够运行如下查询(“ date”是保留字,当它是列名时必须用引号引起来):

SELECT *
FROM logs
WHERE "date" = '2020/10/06'
SELECT *
FROM logs
WHERE "date" BETWEEN '2020/10/01' AND '2020/10/06'
AND hour BETWEEN 9 AND 21

请注意,日期格式必须与分区投影配置中的格式完全相同,即YYYY/MM/DD

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-