【问题标题】:Unable to cast field creation to bigint无法将字段创建转换为 bigint
【发布时间】:2020-01-27 12:58:08
【问题描述】:

我正在尝试合并一堆表,其中一些字段是 bigint 类型的数组。当我尝试在 Hive 中投射这些内容时,它一直给我一个“不在 GROUP BY 键‘field_name’中的表达式。

我的代码如下:

    CREATE TABLE sample_table stored AS avro AS 
    SELECT cast(cast(rand()* 1000000000000000000 AS BIGINT) AS string)        id, 
           'PERSON'AS person, 
           name AS name, 
           age AS age, 
           gender AS gender
FROM   table_a
UNION ALL 
SELECT cast(cast(rand()* 1000000000000000000 AS BIGINT) AS string)    id,  
          'PERSON'AS person, 
           name AS name, 
           collect_list(
    CAST(NULL AS BIGINT)
  ) AS age, 
           null AS gender
FROM   table_b

产生的错误如下:

SQL 错误 [500051] [HY000]:[Cloudera]HiveJDBCDriver 错误处理查询/语句。错误代码:10025,SQL 状态:TStatus(statusCode:ERROR_STATUS, infoMessages:[*org.apache.hive.service.cli.HiveSQLException:Error while compile statement: FAILED: SemanticException [Error 10025]: Line 4:7 Expression not in GROUP BY key 'age':28:27, org.apache.hive.service.cli.operation.Operation:toSQLException:Operation.java:400, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation .java:187

【问题讨论】:

  • 我假设此 SQL 代码不完整,因为此处未使用分组 SQL 关键字,例如 GROUP BY/DISTINCT,其中错误 (Expression not in GROUP BY key ) 是基于?
  • 据我了解,我认为 group by 是通过被调用的基础表触发的,但我不确定.. 这是我在 env 中运行的完整代码
  • " 这是我在环境中运行它的完整代码" 检查这些表是否是视图然后可能?
  • 这是我检查的第一件事。看起来他们都很好。它们按所需的字段进行分组。还有其他想法吗?

标签: sql arrays list hadoop hive


【解决方案1】:

collect_list 是聚合函数。您需要group by 每个非常量字段。

SELECT cast(cast(rand()* 1000000000000000000 AS BIGINT) AS string)    id,  
          'PERSON'AS person, 
           name, 
           collect_list(CAST(NULL AS BIGINT)) AS age, 
           null AS gender
FROM   table_b
group by cast(cast(rand()* 1000000000000000000 AS BIGINT) AS string), name 

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 2015-06-02
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    相关资源
    最近更新 更多