[英]Spark-scala : withColumn is not a member of Unit
我正在嘗試使用 spark df 在 spark 中讀取 CSV 文件。 該文件沒有 header 列,但我想要 header 列。 怎么做? 不知道對不對,我寫了這個命令-> val df = spark.read.format("csv").load("/path/genchan1.txt").show()
並將列名作為 _c0 和 _c1 作為列。 然后我嘗試使用以下方法將列名更改為所需的名稱: val df1 = df.withColumnRenamed("_c0","Series") ,但我得到“withColumnRenamed”不是單元上的成員。
PS:我已經導入了 spark.implicits._ 和 spark.sql.functions 。
請幫助我知道是否有任何方法可以將列 header 添加到數據集以及為什么我會遇到這個問題。
show
的返回類型是Unit
。 請從最后刪除show
。
val df = spark.read.format("csv").load("/path/genchan1.txt")
df.show()
然后您可以使用所有 df 功能-
val df1 = df.withColumnRenamed("_c0","Series")
如果您事先知道 CSV 文件的結構,那么在加載數據時定義一個模式並將其附加到 df 是一個更好的解決方案。
快速參考的示例代碼 -
import org.apache.spark.sql.types._
val customSchema = StructType(Array(
StructField("Series", StringType, true),
StructField("Column2", StringType, true),
StructField("Column3", IntegerType, true),
StructField("Column4", DoubleType, true))
)
val df = spark.read.format("csv")
.option("header", "false") #since your file does not have header
.schema(customSchema)
.load("/path/genchan1.txt")
df.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.