Spark-shell 运行 a SELECT 用于数据帧

Spark-shell running a SELECT for a dataframe

我创建了一个 spark scala 脚本来加载包含客户信息的文件。然后我创建了一个案例 class 来映射记录并将它们显示为 table,我的脚本如下:

//spark context
sc
val sqlContext = new org.apache.spark.sql.SQLContext(sc)

//Define the class to map customers coming from the data inputh
case class customer (cusid: Int, name: String, city : String, province: String, postalcode: String)

//load the file info
val customer_file = sc.textFile("file:////home/ingenieroandresangel/scalascripts/customer.txt")
val customer_rdd = customer_file.map(_.split(",")).map(p => customer(p(0).toInt,p(1),p(2),p(3),p(4)))

val cusstomerdf = customer_rdd.toDF() 

当前结果:

现在,我需要执行 spark sql 查询以仅取回来自我的数据框的列,例如列名:

打印(cusstomerdf.select("name"))

然而,结果并不如预期。我需要取回列名的行,但我得到了这个结果:

问题:我应该如何 运行 正确 select 来取回数据框上的列名??谢谢

结果正确。您正在进行 转换 ,因为 select 转换

如果将其保存在 parquet 文件或 csv 文件中,您将看到结果并可以确认 column 已被选中。

同时您可以在屏幕上看到结果

val selecteddf = customerdf.select("name")
selecteddf.show(false)

这将显示 name column

20 行