Aay*_*pal 5 scala apache-spark
我的行RDD看起来像这样:
Array[org.apache.spark.sql.Row] = Array([1,[example1,WrappedArray([**Standford,Organisation,NNP], [is,O,VP], [good,LOCATION,ADP**])]])
Run Code Online (Sandbox Code Playgroud)
我从转换数据帧到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)
Run Code Online (Sandbox Code Playgroud)
现在如何在行rdd中访问元素,在数据帧中我可以使用df.select("sentence").我期待访问像stanford /其他嵌套元素的元素.
正如@SarveshKumarSingh在评论中写道,你可以RDD[Row]像访问RDD中的任何其他元素一样访问行 .访问行中的元素可以通过几种方式完成.要么get像这样调用:
rowRDD.map(row => row.get(2).asInstanceOf[MyType])
Run Code Online (Sandbox Code Playgroud)
或者如果它是内置类型,您可以避免类型转换:
rowRDD.map(row => row.getList(4))
Run Code Online (Sandbox Code Playgroud)
或者您可能只想使用模式匹配,例如:
rowRDD.map{case Row(field1: Long, field2: MyType) => field2}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助 :)
| 归档时间: |
|
| 查看次数: |
5271 次 |
| 最近记录: |