简体   繁体   English

将 String 转换为类型列 Spark

[英]Convert String to type column Spark

Code:代码:

import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.Column

def func(rawDF: DataFrame,primaryKey: Column, orderKey: Column): DataFrame = {

     //some process
    return newDf
} 

I am trying to create a new processed DF from existing raw DF with the function above.我正在尝试使用上述函数从现有的原始 DF 创建一个新的处理过的 DF。

Code:代码:

var processedDF  = func(rawDF,"col1","col2")

Error:错误:

<console>:73: error: type mismatch;
found   : String("col1")
required: org.apache.spark.sql.Column
   var processedDF  = func(rawDF,"col1","col2")
                                     ^

Any suggestions on how to change the type of the function parameter from String to org.apache.spark.sql.Column关于如何将函数参数的类型从 String 更改为 org.apache.spark.sql.Column 的任何建议

Either任何一个

import org.apache.spark.sql.functions.col

func(rawDF, col("col1"), col("col2"))

or或者

func(rawDF, rawDF("col1"), rawDF("col2"))

or provide Column directly through $ (where spark is SparkSession object)或直接通过$提供Column (其中sparkSparkSession对象)

import spark.implicits.StringToColumn

func(rawDF, $"col1", $"col2")

or SymbolSymbol

import spark.implicits.symbolToColumn

func(rawDF, 'col1, 'col2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spark 中将 dataframe 列类型从字符串转换为(数组和结构) - How to convert the dataframe column type from string to (array and struct) in spark Spark:将字符串列转换为数组 - Spark: Convert column of string to an array 在火花数据框中,如何使用 scala 将字符串类型的日期列转换为日期类型的日期列 - In spark Data frame how to convert Date column of type string to Date column of type Date using scala 如何读取 csv 文件并将一列转换为 Scala+Spark 中的 Map[String, String] 类型? - How to read csv file and convert one column to Map[String, String] type in Scala+Spark? 在火花中将Array [(String,String)]类型转换为RDD [(String,String)]类型 - Convert Array[(String,String)] type to RDD[(String,String)] type in spark spark scala:将字符串列转换为双精度 - spark scala: convert string column into double 将嵌套json的spark列转换为字符串 - convert spark column of nested json into string Spark将列转换为存储在字符串中的SQL类型 - Spark cast column to sql type stored in string Spark DataFrame String类型列到Timestamp / Date类型列的转换 - Spark DataFrame String type column to Timestamp/Date type column conversion spark scala:将Struct列的Array转换为String列 - spark scala : Convert Array of Struct column to String column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM