gpt4 book ai didi

scala - 如何在SCALA中访问Row RDD中的元素

转载 作者:行者123 更新时间:2023-12-02 17:59:15 26 4
gpt4 key购买 nike

我的行 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com