Concat_ws() function in Sparklyr is missing

♀尐吖头ヾ 提交于 2019-12-11 20:31:34

问题


I am following a tutorial on web (Adobe) analytics, where I want to build a Markov Chain Model. (http://datafeedtoolbox.com/attribution-theory-the-two-best-models-for-algorithmic-marketing-attribution-implemented-in-apache-spark-and-r/).

In the example they are using the function: concat_ws (from library(sparklyr)). But it looks like the function does not exists (after installing the package, and calling the library, I receive an error that the function does not exists...).

Comment author of the blog: concat_ws is a Spark SQL function: https://spark.apache.org/docs/2.2.0/api/java/org/apache/spark/sql/functions.html So, you’ll have to rely on sparklyr to have that function work.

My question: are there workarounds to get access to the concat_ws() function? I tried:

  • Searched on Github (https://github.com/rstudio/sparklyr) if I could find the function (or the source code).. unfortunately no result..

What is the goal of the function? Concatenates multiple input string columns together into a single string column, using the given separator.


回答1:


You can simply use paste from base R.

library(sparklyr)
library(dplyr)

config <- spark_config()
sc <- spark_connect(master = "local", config = config)

df <- as.data.frame(cbind(c("1", "2", "3"), c("a", "b", "c")))
sdf <- sdf_copy_to(sc, df, overwrite = T)

sdf %>%
  mutate(concat = paste(V1, V2, sep = "-"))



回答2:


You cannot find the function because it doesn't exist in sparklyr package. concat_ws is a Spark SQL function (org.apache.spark.sql.functions.concat_ws).

sparklyr depends on a SQL translation layer - function calls are translated into SQL expressions with dbplyr:

> dbplyr::translate_sql(concat_ws("-", foo, bar))
<SQL> CONCAT_WS('-', "foo", "bar")

This means that the function can be applied only in the sparklyr context:

sc <- spark_connect(master = "local[*]")
df <- copy_to(sc, tibble(x="foo", y="bar"))

df %>% mutate(xy = concat_ws("-", x, y))
# # Source: spark<?> [?? x 3]
#   x     y     xy     
# * <chr> <chr> <chr>  
# 1 foo   bar   foo-bar


来源:https://stackoverflow.com/questions/52781720/concat-ws-function-in-sparklyr-is-missing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!