Spark Dataframe - How to get a particular field from a column in a row
我有一个具有如下结构的数据框:
1 2 3 4 5 6 7 8 9 | root |-- npaDetails: struct (nullable = true) | |-- additionalInformation: struct (nullable = true) | |-- npaStatus: struct (nullable = true) | |-- npaDetails: struct (nullable = true) |-- npaHeaderData: struct (nullable = true) | |-- npaNumber: string (nullable = true) | |-- npaDownloadDate: string (nullable = true) | |-- npaDownloadTime: string (nullable = true) |
我想从数据框中的所有行中检索所有
我的方法是遍历数据帧中的所有行,以提取每个存储在字段
1 2 3 4 5 6 7 8 9 10 11 | parquetFileDF.foreach { newRow => //To retrieve the second column val column = newRow.get(1) //The following line is not allowed //val npaNumber= column.getAs[String]("npaNumber") println(column) } |
每次迭代中打印的列内容如下:
[207400956,27FEB17,09.30.00]
但是
谢谢
如果您只想提取
1 | parquetFileDF.select($"npaHeaderData.npaNumber".as("npaNumber")) |
您应该只将
您可以在数据框上调用
1 |
您可以执行以下操作,从数据帧读取数据时将避免使用[]。
ids [DataFrame]:{id,name}
以上方法将解决您的问题。