scala - 在 Spark Scala 中找不到错误值
问题描述
架构:
root
|-- col_a: struct (nullable = true)
| |-- $numberLong: string (nullable = true)
|-- col_b: string (nullable = true)
|-- col_c: struct (nullable = true)
| |-- $numberLong: string (nullable = true)
打破 (col_a) 结构的代码
df = df.select($"col_a.*",$"col_b",$"col_c")
df.printSchema()
操作:
|-- $numberLong: string (nullable = true)
|-- col_b: string (nullable = true)
|-- col_c: struct (nullable = true)
| |-- $numberLong: string (nullable = true)
现在,当我尝试仅选择第一列(“$numberLong”)并重命名它时
df = df.select($"$numberLong".as("test"))
我收到以下错误:
error: not found: value numberLong
df = df.select($"$numberLong")
^
当该列明显存在时,我无法理解错误的原因。
解决方案
如果列的列名中有前导$
,则不能引用该列$"colName"
- 即使您将 colName 括在backticks
. 而是col("colName")
如下所示使用:
case class A(`$numberLong`: String)
val df = Seq(
(A("x1"), "d1", A("y1")),
(A("x2"), "d2", A("y2")),
(A("x3"), "d3", A("y3"))
).toDF("col_a", "col_b", "col_c")
val df2 = df.select($"col_a.*", $"col_b", $"col_c")
df2.printSchema
// root
// |-- $numberLong: string (nullable = true)
// |-- col_b: string (nullable = true)
// |-- col_c: struct (nullable = true)
// | |-- $numberLong: string (nullable = true)
df2.select(col("$numberLong").as("test")).printSchema
// root
// |-- test: string (nullable = true)
推荐阅读
- java - 如何将 Edittext 中的项目添加到列表视图?
- c# - C# 的 Queue.dequeue() 是按引用传递还是按值传递?
- docker - docker nginx 容器不包含 nginx?
- apache-camel - Camel 独立 - PropertiesComponent 的配置不适用于 @BeanInject 的 bean
- react-native - RN code-push:挂钩到 codePushStatusDidChange 的测试组件
- performance - 如何在 UML 类图中衡量系统的性能和可维护性?
- javascript - AngularJS突出显示表,ng-repeat包含一个下拉列表
- excel - 如何使用从 VBA 中的工作表名称创建的书签将 Excel 导出为 PDF?
- java - Android如何从外部存储中获取资源标识符
- python - 使用 Anaconda Env Python 路径安装 Opencv 3.4 - “未找到模块”