【问题标题】:Sparklyr: Use group_by and then concatenate strings from rows in a groupSparklyr:使用 group_by,然后连接组中行中的字符串
【发布时间】:2017-11-08 01:22:53
【问题描述】:

我正在尝试使用 sparklyr 中的 group_by() 和 mutate() 函数来连接组中的行。

这是一个我认为应该可行但不可行的简单示例:

library(sparkylr)
d <- data.frame(id=c("1", "1", "2", "2", "1", "2"), 
             x=c("200", "200", "200", "201", "201", "201"), 
             y=c("This", "That", "The", "Other", "End", "End"))
d_sdf <- copy_to(sc, d, "d")
d_sdf %>% group_by(id, x) %>% mutate( y = paste(y, collapse = " "))

我希望它产生的是:

Source: local data frame [6 x 3]
Groups: id, x [4]

# A tibble: 6 x 3
      id      x         y
  <fctr> <fctr>     <chr>
1      1    200 This That
2      1    200 This That
3      2    200       The
4      2    201 Other End
5      1    201       End
6      2    201 Other End

我收到以下错误:

Error: org.apache.spark.sql.AnalysisException: missing ) at 'AS' near '' '' in selection target; line 1 pos 42

请注意,在 data.frame 上使用相同的代码可以正常工作:

d %>% group_by(id, x) %>% mutate( y = paste(y, collapse = " "))

【问题讨论】:

    标签: r data-science sparklyr


    【解决方案1】:

    Spark sql 不喜欢在不聚合的情况下使用聚合函数,因此这在 dplyr 和普通 dataframe 中有效,但在 SparkDataFrame 中无效 - sparklyr 将您的命令转换为sql 声明。如果您查看错误消息中的第二个位,您会发现这是错误的:

    == SQL ==
    SELECT `id`, `x`, CONCAT_WS(' ', `y`, ' ' AS "collapse") AS `y`
    

    paste 被翻译成CONCAT_WS。 concat 但是会将列粘贴在一起。

    更好的等价物是collect_list 和collect_set,但它们会产生list 输出。

    但您可以在此基础上继续:

    如果您不希望在结果中复制同一行,您可以使用summarise、collect_list 和paste:

    res <- d_sdf %>% 
          group_by(id, x) %>% 
          summarise( yconcat =paste(collect_list(y)))
    

    结果:

    Source:     lazy query [?? x 3]
    Database:   spark connection master=local[8] app=sparklyr local=TRUE
    Grouped by: id
    
         id     x         y
      <chr> <chr>     <chr>
    1     1   201       End
    2     2   201 Other End
    3     1   200 This That
    4     2   200       The
    

    如果您确实想要复制您的行,您可以将其加入到您的原始数据中:

    d_sdf %>% left_join(res)
    

    结果:

    Source:     lazy query [?? x 4]
    Database:   spark connection master=local[8] app=sparklyr local=TRUE
    
         id     x     y   yconcat
      <chr> <chr> <chr>     <chr>
    1     1   200  This This That
    2     1   200  That This That
    3     2   200   The       The
    4     2   201 Other Other End
    5     1   201   End       End
    6     2   201   End Other End
    

    【讨论】:

    • 谢谢,非常有用的回答
    猜你喜欢
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多