我的行 RDD 如下所示:
Array[org.apache.spark.sql.Row] = Array([1,[example1,WrappedArray([**Standford,Organisation,NNP], [is,O,VP], [good,LOCATION,ADP**])]])
我通过将数据帧转换为 rdd 得到了这个,数据帧模式是:
root
|-- article_id: long (nullable = true)
|-- sentence: struct (nullable = true)
| |-- sentence: string (nullable = true)
| |-- attributes: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- tokens: string (nullable = true)
| | | |-- ner: string (nullable = true)
| | | |-- pos: string (nullable = true)
现在如何访问行rdd中的元素,在数据框中我可以使用df.select(“sentence”)。我期待访问 stanford/其他嵌套元素等元素。
最佳答案
正如 @SarveshKumarSingh 在评论中所写,您可以访问 RDD[Row]
中的行。就像访问 RDD 中的任何其他元素一样。可以通过多种方式访问行中的元素。要么直接调用 get
像这样:
rowRDD.map(row => row.get(2).asInstanceOf[MyType])
或者如果它是内置类型,您可以避免类型转换:
rowRDD.map(row => row.getList(4))
或者您可能只想使用模式匹配,例如:
rowRDD.map{case Row(field1: Long, field2: MyType) => field2}
我希望这有帮助:)
关于scala - 如何在SCALA中访问Row RDD中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39010473/