scala - 用于转换 spark 数据框列的通用函数
问题描述
我编写了以下函数,它转换了 spark 数据框列,但我想让它使用类型化函数:
def castCol(newName: String, previosuNmae: String, datatType: DataType): DataFrame = {
df.withColumn(newName, col(previosuNmae).cast(datatType))
我宁愿让它像:
def castCol[DataType](newName: String, previosuNmae: String): DataFrame = {
df.withColumn(newName, col(previosuNmae).cast(DataType))
但这不起作用。你知道谁将 dataType 作为类型而不是参数传递给函数吗
解决方案
这个怎么样:
def castCol[T<:DataType](newColName: String, newColType:T, previousColName: String): DataFrame =
df.withColumn(newColName, col(previousColName).cast(newColType))
希望能帮助到你
推荐阅读
- c++ - 调用系统时忽略 SIGINT (3)
- tfs - 即使在使用多个项目时,工作项 ID 在 TFS 中是否唯一?
- reporting-services - SSRS 子报表重复
- c# - 我正在尝试为 D&D 循环随机数生成器,但我的代码不起作用
- delphi - 如何在保留每个数字的原始位置的同时对一列数字进行排名
- python - 动态地从字典列表中创建嵌套的 json 对象
- swift - 了解警告:此类“SwiftUI.AccessibilityNode”不是已知的可序列化元素
- python - 使用python将图表添加到Excel中,其中包含来自多个工作表的数据
- javascript - 如何更改 PHP 回显中元素的可见性?
- mongodb - MongoDB:在多个数据库上运行聚合