![](/img/trans.png)
[英]About how to add a new column to an existing DataFrame with random values in Scala
[英]How to add a new column to my DataFrame such that values of new column are populated by some other function in scala?
myFunc(Row): String = {
//process row
//returns string
}
appendNewCol(inputDF : DataFrame) : DataFrame ={
inputDF.withColumn("newcol",myFunc(Row))
inputDF
}
但是在我的案例中沒有創建新的列。 我的myFunc
將此行傳遞給knowledgebasesession
會話 object 並在觸發規則后返回一個字符串。 我可以這樣做嗎? 如果沒有,正確的方法是什么? 提前致謝。
我看到許多使用expr() sqlfunc(col(udf(x))
和其他技術的 StackOverflow 解決方案,但這里我的newcol
不是直接從現有列派生的。
Dataframe:
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{StringType, StructField, StructType}
val myFunc = (r: Row) => {r.getAs[String]("col1") + "xyz"} // example transformation
val testDf = spark.sparkContext.parallelize(Seq(
(1, "abc"), (2, "def"), (3, "ghi"))).toDF("id", "col1")
testDf.show
val rddRes = testDf
.rdd
.map{x =>
val y = myFunc (x)
Row.fromSeq (x.toSeq ++ Seq(y) )
}
val newSchema = StructType(testDf.schema.fields ++ Array(StructField("col2", dataType =StringType, nullable =false)))
spark.sqlContext.createDataFrame(rddRes, newSchema).show
結果:
+---+----+
| id|col1|
+---+----+
| 1| abc|
| 2| def|
| 3| ghi|
+---+----+
+---+----+------+
| id|col1| col2|
+---+----+------+
| 1| abc|abcxyz|
| 2| def|defxyz|
| 3| ghi|ghixyz|
+---+----+------+
使用數據集:
case class testData(id: Int, col1: String)
case class transformedData(id: Int, col1: String, col2: String)
val test: Dataset[testData] = List(testData(1, "abc"), testData(2, "def"), testData(3, "ghi")).toDS
val transformedData: Dataset[transformedData] = test
.map { x: testData =>
val newCol = x.col1 + "xyz"
transformedData(x.id, x.col1, newCol)
}
transformedData.show
如您所見,數據集更具可讀性,並且提供了強大的類型轉換。 由於我不知道您的 spark 版本,因此在此處提供兩種解決方案。 但是,如果您使用的是 spark v>=1.6,則應該查看 Datasets。 玩 rdd 很有趣,但很快就會演變為更長的工作運行和許多您無法預見的其他問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.