【发布时间】:2018-07-18 23:11:52
【问题描述】:
我正在将行数据集映射到自定义类的数据集。
Dataset<Row> rows= sparkSession.read().orc(path);
Dataset<customClass> dataset =
rows.map(I'm parsing row to map it to customClass,
Encoders.bean(customClass.class));
我得到了这个 AnalysisException:
AnalysisException:由于数据类型不匹配,无法解析“named_struct()”:函数 named_struct 的输入需要至少一个参数;
我正在使用 Spark 2.3.0,并且正在使用 javaBeans 对我的自定义类进行编码。
我检查了编码器是否有效地推断出架构,确实如此。因此,从技术上讲,地图操作应该可以工作。
有没有人遇到过这个异常消息? named_struct 函数有什么作用?我没有找到与 Spark 相关的相关信息...
root
|-- field1: struct (nullable = true)
| |-- value: string (nullable = true)
|-- field2: string (nullable = true)
|-- field3: integer (nullable = true)
|-- field4: double (nullable = true)
|-- field5: struct (nullable = true)
| |-- value: double (nullable = true)
|-- field6: struct (nullable = true)
| |-- field61: double (nullable = true)
| |-- field62: string (nullable = true)
| |-- field63: integer (nullable = true)
| |-- field64: struct (nullable = true)
| | |-- value: string (nullable = true)
|-- field7: struct (nullable = true)
| |-- value: double (nullable = true)
|-- field8: struct (nullable = true)
| |-- value: double (nullable = true)
|-- field9: struct (nullable = true)
| |-- field91: map (nullable = true)
| | |-- key: struct
| | |-- value: struct (valueContainsNull = true)
| | | |-- value: string (nullable = true)
| | | |-- field911: struct (nullable = true)
| | | | |-- value: double (nullable = true)
| | | |-- field912: struct (nullable = true)
| | | | |-- value: double (nullable = true)
| | | |-- field913: map (nullable = true)
| | | | |-- key: struct
| | | | |-- value: struct (valueContainsNull = true)
| | | | | |-- value: integer (nullable = false)
| | | | | |-- field9131: struct (nullable = true)
| | | | | | |-- value: double (nullable = true)
| | | | | |-- field9131: struct (nullable = true)
| | | | | | |-- value: double (nullable = true)
| | | |-- field914: struct (nullable = true)
| | | | |-- value: double (nullable = true)
| | | |-- field915: string (nullable = true)
|-- field10: string (nullable = true)
|-- field11: struct (nullable = true)
| |-- field111: map (nullable = true)
| | |-- key: struct
| | |-- value: struct (valueContainsNull = true)
| | | |-- value: integer (nullable = false)
| | | |-- field1111: struct (nullable = true)
| | | | |-- value: double (nullable = true)
| | | |-- field1112: struct (nullable = true)
| | | | |-- value: double (nullable = true)
|-- field12: boolean (nullable = true)
|-- field13: struct (nullable = true)
| |-- field131: integer (nullable = false)
| |-- field132: integer (nullable = false)
|-- field14: struct (nullable = true)
| |-- field141: string (nullable = true)
【问题讨论】:
-
你如何“我正在解析行以将其映射到 customClass”。我仍然期待发现类似于
named_struct的东西。你customClass的字段是什么? -
customClass 具有以下字段:String、Integer、Double、Boolean 和 Enumeration。除此之外,它还有其他类作为字段。让 A、B、C、D、E 成为这些类:A 有一个 Map。 B 只有一个字符串。 C 有 String、Integer、Double、Enumeration 和 Map
。 D 有字符串、双精度和整数。 E 有一个 Map . -
您能否编辑您的问题并包含逻辑查询计划,例如
q.explain(true)?你也可以添加System.out.println(Encoders.bean(customClass.class))的输出吗?你能在你的customClass中删除所有使用Map并重新开始(只是为了测试它可以在没有地图的情况下提供更好的结果,因为它们是部分支持的)? -
我无法获得逻辑查询计划,因为在我创建
Dataset<customClass>时程序崩溃了。我认为这个问题与Maps有关:在customClass架构上,Maps的键是struct,并且没有链接到它们的值。也许这就是我遇到这个问题的原因。 -
我终于找到了为什么会出现这个
named_struct错误:我使用的字段之一被声明为final,这意味着它没有设置器。这违反了JavaBean合同。
标签: java apache-spark apache-spark-sql