简体   繁体   English

Spark Java API,数据集操作?

[英]Spark Java API, Dataset Manipulation?

I'm new spark Java API. 我是新的Spark Java API。 My dataset contains two columns (account, Lib) . 我的数据集包含两列(帐户,库)。 I want to display accounts having differents lib. 我想显示具有不同lib的帐户。 In fact my dataset is something like this. 实际上我的数据集是这样的。 ds1 DS1

 +---------+------------+
    |  account|    Lib     |
    +---------+------------+
    | 222222  |  bbbb      |
    | 222222  |  bbbb      |
    | 222222  |  bbbb      |
    |         |            |
    | 333333  |  aaaa      |
    | 333333  |  bbbb      |
    | 333333  |  cccc      |
    |         |            |
    | 444444  |  dddd      |
    | 444444  |  dddd      |
    | 444444  |  dddd      |
    |         |            |
    | 555555  |  vvvv      |
    | 555555  |  hhhh      |
    | 555555  |  vvvv      |

I want to get ds2 like this: 我想要这样的ds2:

+---------+------------+
|  account|    Lib     |
+---------+------------+
|         |            |
| 333333  |  aaaa      |
| 333333  |  bbbb      |
| 333333  |  cccc      |
|         |            |
| 555555  |  vvvv      |
| 555555  |  hhhh      |

If groups are small you can use window functions: 如果组很小,则可以使用窗口功能:

import org.apache.spark.sql.functions._
import org.apache.spark.sql.expressions.Window


df
  .withColumn("cnt", approx_count_distinct("Lib").over(Window.partitionBy("account")).alias("cnt"))
  .where(col("cnt") > 1)

If groups are large: 如果组很大:

df.join(
  df
   .groupBy("account")
   .agg(countDistinct("Lib").alias("cnt")).where(col("cnt") > 1),
  Seq("account"),
  "leftsemi"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM