【问题标题】:Identifying values that revert in Spark识别在 Spark 中恢复的值
【发布时间】:2017-07-13 19:51:47
【问题描述】:

我有一个客户的 Spark DataFrame,如下所示。

#SparkR code
customers <- data.frame(custID = c("001", "001", "001", "002", "002", "002", "002"),
  date = c("2017-02-01", "2017-03-01", "2017-04-01", "2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01"),
  value = c('new', 'good', 'good', 'new', 'good', 'new', 'bad'))
customers <- createDataFrame(customers)
display(customers)

custID|  date     | value
--------------------------
001   | 2017-02-01| new
001   | 2017-03-01| good
001   | 2017-04-01| good
002   | 2017-01-01| new
002   | 2017-02-01| good
002   | 2017-03-01| new
002   | 2017-04-01| bad

在对custID 的第一个月观察中,客户获得了value 的“新”。此后,它们被归类为“好”或“坏”。但是,如果客户开设第二个帐户,则可以从“好”或“坏”恢复为“新”。发生这种情况时,我想用“2”而不是“1”标记客户,以表明他们开设了第二个帐户,如下所示。我怎样才能在 Spark 中做到这一点? SparkR 或 PySpark 命令都可以工作。

#What I want to get 
custID|  date     | value | tag
--------------------------------
001   | 2017-02-01| new   | 1
001   | 2017-03-01| good  | 1
001   | 2017-04-01| good  | 1
002   | 2017-01-01| new   | 1
002   | 2017-02-01| good  | 1
002   | 2017-03-01| new   | 2
002   | 2017-04-01| bad   | 2

【问题讨论】:

    标签: pyspark spark-dataframe sparkr


    【解决方案1】:

    在 pyspark 中:

    from pyspark.sql import functions as f
    
    spark = SparkSession.builder.getOrCreate()
    
    # df is equal to your customers dataframe
    df = spark.read.load('file:///home/zht/PycharmProjects/test/text_file.txt', format='csv', header=True, sep='|').cache()
    
    df_new = df.filter(df['value'] == 'new').withColumn('tag', f.rank().over(Window.partitionBy('custID').orderBy('date')))
    df = df_new.union(df.filter(df['value'] != 'new').withColumn('tag', f.lit(None)))
    df = df.withColumn('tag', f.collect_list('tag').over(Window.partitionBy('custID').orderBy('date'))) \
        .withColumn('tag', f.UserDefinedFunction(lambda x: x.pop(), IntegerType())('tag'))
    
    df.show()
    

    然后输出:

    +------+----------+-----+---+                                                   
    |custID|      date|value|tag|
    +------+----------+-----+---+
    |   001|2017-02-01|  new|  1|
    |   001|2017-03-01| good|  1|
    |   001|2017-04-01| good|  1|
    |   002|2017-01-01|  new|  1|
    |   002|2017-02-01| good|  1|
    |   002|2017-03-01|  new|  2|
    |   002|2017-04-01|  bad|  2|
    +------+----------+-----+---+
    

    顺便说一句,熊猫可以轻松做到这一点。

    【讨论】:

    • 谢谢,我可以在 R 或 pandas 中执行此操作,但我有一个非常大的数据框,我需要 Spark。
    【解决方案2】:

    这可以使用以下代码来完成:

    过滤掉所有带有“new”的记录

    df_new<-sql("select * from df where value="new")
    createOrReplaceTempView(df_new,"df_new")
    
    df_new<-sql("select *,row_number() over(partiting by custID order by date) 
    tag from df_new")
    createOrReplaceTempView(df_new,"df_new")
    
    df<-sql("select custID,date,value,min(tag) as tag from 
    (select t1.*,t2.tag from df t1 left outer join df_new t2 on 
    t1.custID=t2.custID and t1.date>=t2.date) group by 1,2,3")
    

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 2015-01-23
      • 2015-10-25
      相关资源
      最近更新 更多