加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在SCALA中的Row RDD中访问elemens

发布时间:2020-12-16 18:39:46 所属栏目:安全 来源:网络整理
导读:我的行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 =
我的行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}

我希望这有帮助 :)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读