简体   繁体   English

spark scala:添加一个以列表为值的新列; 该列表应验证特定条件

[英]spark scala: add a new column that has a list as a value; the list should verify a specific condition

I have the query bellow:我有以下查询:

finvResultOutput1
      .withColumn("offersAtSite",when(col("activeIOsAtSite")==="Y",
        finvResultOutput1.select(col("OfferSpecification_displayLabel"),col("Site_siteId").alias("siteid"))
          .where(col("Site_siteId")===col("siteid"))
          .drop("siteid")
          .collect()
      ))

I want to add a new column to my data frame called offersAtSite , this column should be added when another column called activeIOsAtSite equals "Y" .我想在我的数据框中添加一个名为offersAtSite的新列,当另一个名为activeIOsAtSite列等于"Y"时,应该添加此列。

The value of this new column should be a list without duplicates of the elements of column OfferSpecification_displayLabel where the Site_siteId equals the current Site_siteId of the row we are iterating over.这个新列的值应该是一个列表,没有重复列OfferSpecification_displayLabel的元素,其中Site_siteId等于我们正在迭代的行的当前Site_siteId

Could anyone give me a better approach to do that, because the query that I made is taking too much time and I don't know if it is working谁能给我一个更好的方法来做到这一点,因为我所做的查询花费了太多时间,我不知道它是否有效

Bellow is an example of what I want to achieve:贝娄是我想要实现的一个例子:

在此处输入图像描述

First, you can groupBy then collect_set (set does not contain duplicates) on your main table:首先,您可以在主表上进行groupBy然后collect_set (集合不包含重复项):

val grouped = df.groupBy("Site_siteId").agg(collect_set("OfferSpecifications_displayLabel").as("offerAtSite"))

We get:我们得到:

+-----------+------------------------+
|Site_siteId|offerAtSite             |
+-----------+------------------------+
|site_id_3  |[site3_DL_1, site3_DL_2]|
|site_id_4  |[site4_DL_1]            |
|site_id_2  |[site2_DL_1]            |
|site_id_1  |[site1_DL_1, site1_DL_2]|
+-----------+------------------------+

Then, we join our df table with grouped and overwrite offerAtSite to only have values for Y value:然后,我们将我们的df表加入grouped并覆盖offerAtSite以仅具有Y值的值:

df.join(grouped, Seq("Site_siteId"), "left")
  .withColumn("offerAtSite", when(col("activeIOsAtSite").equalTo("Y"), col("offerAtSite")))

Final result:最后结果:

+-----------+---------------+--------------------------------+------------------------+
|Site_siteId|activeIOsAtSite|OfferSpecifications_displayLabel|offerAtSite             |
+-----------+---------------+--------------------------------+------------------------+
|site_id_3  |Y              |site3_DL_1                      |[site3_DL_1, site3_DL_2]|
|site_id_3  |Y              |site3_DL_2                      |[site3_DL_1, site3_DL_2]|
|site_id_4  |N              |site4_DL_1                      |null                    |
|site_id_2  |N              |site2_DL_1                      |null                    |
|site_id_1  |Y              |site1_DL_1                      |[site1_DL_1, site1_DL_2]|
|site_id_1  |Y              |site1_DL_2                      |[site1_DL_1, site1_DL_2]|
+-----------+---------------+--------------------------------+------------------------+

This should work better, good luck!这应该会更好,祝你好运!

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

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