我之前写的代码大体功能如下:
val map= new mutable.HashMap[String, String]
val df: DataFrame = DbDataApi.requestColMetaInfo(dataId)
df.foreach(row =>{
map.put(row.getAs[String](fieldName = "colName"),row.getAs[String](fieldName = "col1"))
})
运行后发现,虽然每一行的数据都遍历得到了,但注入map时,map只能暂时储存当前行,遍历结束后会为空。
调试后发现,需要先将dataframe的数据collect()出来,再进行操作,如下
val map= new mutable.HashMap[String, String]
val df: DataFrame = DbDataApi.requestColMetaInfo(dataId)
df.collect().foreach(row =>{
map.put(row.getAs[String](fieldName = "colName"),row.getAs[String](fieldName = "col1"))
这样数据就会注入map中了。