Dataframe Spark scala:withColumn不是单元的成员

Dataframe Spark scala:withColumn不是单元的成员,dataframe,apache-spark,apache-spark-sql,Dataframe,Apache Spark,Apache Spark Sql,我正在尝试使用spark df读取spark中的CSV文件。该文件没有标题列,但我希望有标题列。怎么做? 我不知道我是否正确,我写了这个命令 ->val df=spark.read.format(“csv”).load(“/path/genchan1.txt”).show() 列的列名为_c0和_c1。然后我尝试使用以下命令将列名更改为所需的名称:val df1=df.WithColumnRename(“\u c0”,“Series”),但我发现“WithColumnRename”不是单元上的成

我正在尝试使用spark df读取spark中的CSV文件。该文件没有标题列,但我希望有标题列。怎么做? 我不知道我是否正确,我写了这个命令 ->val df=spark.read.format(“csv”).load(“/path/genchan1.txt”).show()

列的列名为_c0和_c1。然后我尝试使用以下命令将列名更改为所需的名称:val df1=df.WithColumnRename(“\u c0”,“Series”),但我发现“WithColumnRename”不是单元上的成员

PS:我已经导入了spark.implicits.\和spark.sql.functions


请帮助我了解是否有任何方法可以将列标题添加到数据集,以及我为什么会遇到此问题。

显示的返回类型是
单位
。请从末尾删除
show

val df=spark.read.format(“csv”).load(“/path/genchan1.txt”)
df.show()
然后可以使用所有df功能-

val df1=df.withColumnRename(“\u c0”,“Series”)

显示的
返回类型为
单元
。请从末尾删除
show

val df=spark.read.format(“csv”).load(“/path/genchan1.txt”)
df.show()
然后可以使用所有df功能-

val df1=df.withColumnRename(“\u 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()

如果您事先知道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()

非常感谢您的快速回复!:)非常感谢您的快速回复!:)