scala - 如何将 udf 应用于 Dafaframe 上的所有字符串和字符串数组
问题描述
我有这个简单的 UDF 将所有字符串都大写
val upper = (s: String) => {
s.toUpperCase
}
val upperUDF: UserDefinedFunction = spark.udf.register("upper", upper)
我正在尝试像这样使用UDF,所以我可以获得所有大写所有字符串都是嵌套模式
def extractNames(schema: StructType): Seq[String] = {
schema.fields.flatMap { field =>
field.dataType match {
case structType: StructType =>
extractNames(structType).map(field.name + "." + _)
case _: StringType =>
field.name :: Nil
case s: ArrayType if (s.elementType == StringType) =>
field.name + "." + "element" :: Nil
case _ =>
Nil
}
}
}
extractNames(df.schema)
.foldLeft(df)({ (memoDF, colName) =>
memoDF.withColumn(colName, upperUDF(col(colName)))
})
.as[B]
但是当我得到一个字符串数组时出现此错误
cannot resolve '`alert`['element']' due to data type mismatch: argument 2 requires integral type, however, ''element'' is of string type.;;
alert 是一个字符串数组
解决方案
推荐阅读
- blockchain - Hyperledger Sawtooth 未满足的依赖项交易
- java - 将 Json 字符串转换为 JSONArray
- javascript - WP Block Styles - 选择块样式时触发 JS
- python-3.x - 当索引有名称时,使用 Pandas 的 doctest 函数不起作用
- vb.net - 集合到 vb.net 中的 string() 转换
- javascript - GET请求nodejs reactjs上的410(消失)错误
- c++ - 函数末尾的断点
- packer - 在打包器中,如何在不调用函数的情况下在字符串中添加文字大括号 {{}}?
- java - 我无法让 RecyclerView 在底部导航内的片段上显示它的项目。我阅读了有关它的所有主题,但没有任何效果
- machine-learning - 如何根据输入形状定义 conv2D 大小