【问题标题】:How to resolve this sql error of schema_of_json如何解决 schema_of_json 的这个 sql 错误
【发布时间】:2021-04-15 10:10:34
【问题描述】:

我需要找出给定 JSON 文件的架构,我看到 sql 有 schema_of_json function 像这样的东西完美无缺

> SELECT schema_of_json('[{"col":0}]');
 ARRAY<STRUCT<`col`: BIGINT>>

但是如果我查询我的表名,它会给我以下错误

>SELECT schema_of_json(Transaction) as json_data from table_name;
Error in SQL statement: AnalysisException: cannot resolve 'schemaofjson(`Transaction`)' due to data type mismatch: The input json should be a string literal and not null; however, got `Transaction`.; line 1 pos 7;

事务是我表中的列之一,在手动检查后我可以证明它是字符串类型(json)。

SQL语句有它给我JSON的架构,怎么办?

【问题讨论】:

  • 看起来“交易”列包含一些 null 值。你可以试试SELECT schema_of_json(Transaction) as json_data from table_name where Transaction is not null;
  • 这仍然不起作用,显示相同的错误。就像我说的 Transaction 列是一个字符串而不是 null
  • 你能分享一个minimal reproducible example吗?

标签: sql azure-databricks


【解决方案1】:

在进一步查看documentation 之后,很明显foldable 一词表示静态的,而表 JSON 中的列将不起作用

这里是最小的可重复示例:

SELECT schema_of_json(CAST('{ "a": "b" }' AS STRING))

只要在上面的语句中引入了强制转换,schema_of_json 就会失败............它需要一个静态 JSON 作为输入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多