以下是R代码示例。我想在sparklyr中做同样的事。
custTrans1 <- Pdt_table %>%
group_by(Main_CustomerID) %>%
summarise(Invoice = as.vector(list(Invoice_ID)),Industry = as.vector(list(Industry)))
其中Pdt_table是火花数据框,Main_CustomerID,Invoice_ID和Industry是变量。
我想创建上述变量的列表并将其转换为vector。我怎样才能在sparklyr
中完成?
答案 0 :(得分:0)
您可以使用collect_list
或collect_set
:
set.seed(1)
df <- copy_to(
sc, tibble(group = rep(c("a", "b"), 3), value = runif(6)),
name = "df"
)
result <- df %>% group_by(group) %>% summarise(values = collect_list(value))
result
# Source: lazy query [?? x 2]
# Database: spark_connection
group values
<chr> <list>
1 b <list [3]>
2 a <list [3]>
is translated to以下查询:
result %>% show_query()
<SQL>
SELECT `group`, COLLECT_LIST(`value`) AS `values`
FROM `df`
GROUP BY `group`
与相应的execution plan:
result %>% optimizedPlan()
<jobj[213]>
org.apache.spark.sql.catalyst.plans.logical.Aggregate
Aggregate [group#259], [group#259, collect_list(value#260, 0, 0) AS values#345]
+- InMemoryRelation [group#259, value#260], true, 10000, StorageLevel(disk, memory, deserialized, 1 replicas), `df`
+- Scan ExistingRDD[group#259,value#260]
和架构(带array<...>
列):
root
|-- group: string (nullable = true)
|-- values: array (nullable = true)
| |-- element: double (containsNull = true)
请记住:
sparklyr
的数据集中整齐,但不会让事情变得简单。要有效地处理结果,您可能需要Scala扩展名。