簡體   English   中英

在 Spark 中工作時替代 ``stringr::str_detect``

[英]Alternative for ``stringr::str_detect`` when working in Spark

我已經在本地設備上的 RStudio 工作了幾年,最近我開始使用 Spark(版本 3.0.1)。 當我嘗試在 Spark 中運行stringr::str_detect()時遇到了一個意想不到的問題。 顯然str_detect()在 SQL 中沒有等效項。 我正在尋找替代方案,最好在 R 中。

這是我在本地運行str_detect()與在 Spark 中運行時的預期結果示例。

# Load packages
library(dplyr)
library(stringr)
library(sparklyr)

# Example tibble
df <- tibble(foodtype = c("potatosalad", "potato", "salad"))
df

---
# A tibble: 3 x 1
  foodtype   
  <chr>      
1 potatosalad
2 potato     
3 salad 
---

# Expected result when using R
df %>% 
  mutate(contains_potato = str_detect(foodtype, "potato"))

---
# A tibble: 3 x 2
  foodtype    contains_potato
  <chr>       <lgl>          
1 potatosalad TRUE           
2 potato      TRUE           
3 salad       FALSE  
---

但是,當我在 Spark dataframe 上運行此代碼時,它返回以下錯誤消息:“錯誤:str_detect() 在此 SQL 變體中不可用”。

# Connect to local Spark cluster
sc <- spark_connect(master = "local", version = "3.0")

# Copy tibble to Spark cluster
df_spark <- copy_to(sc, df)
df_spark

# Error when using str_detect with Spark
df_spark %>% 
  mutate(contains_potato = str_detect(foodtype, "potato"))

---
Error: str_detect() is not available in this SQL variant
---

str_detect()等價於 Spark 的rlike function。 我不使用 R 的火花,但這樣的事情應該可以工作:

df_spark %>% mutate(contains_potato = foodtype %rlike% "potato")

當沒有 dplyr 等效項時, dplyr接受編寫為 R 函數的 Spark 函數:

df_spark %>% mutate(contains_potato = rlike(foodtype, "potato"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM