微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SparkSQL并在Java中的DataFrame上爆炸

有没有一种简单的方法如何在Sparksql DataFrame上使用数组列爆炸?它在 Scala中相对简单,但是这个函数似乎在 Java中不可用(如javadoc中所述).

一个选项是使用sqlContext.sql(…)并在查询中爆炸功能,但我正在寻找更好,更清洁的方式. DataFrames是从镶木地板文件加载的.

解决方法

我以这种方式解决了这个问题:假设你有一个包含名为“positions”的职位描述的数组列,对于每个拥有“fullName”的人.

然后你从初始架构得到:

root
|-- fullName: string (nullable = true)
|-- positions: array (nullable = true)
    |    |-- element: struct (containsNull = true)
    |    |    |-- companyName: string (nullable = true)
    |    |    |-- title: string (nullable = true)
...

到架构:

root
 |-- personName: string (nullable = true)
 |-- companyName: string (nullable = true)
 |-- positionTitle: string (nullable = true)

通过做:

DataFrame personPositions = persons.select(persons.col("fullName").as("personName"),org.apache.spark.sql.functions.explode(persons.col("positions")).as("pos"));

    DataFrame test = personPositions.select(personPositions.col("personName"),personPositions.col("pos").getField("companyName").as("companyName"),personPositions.col("pos").getField("title").as("positionTitle"));

原文地址:https://www.jb51.cc/java/123355.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐