Skip to content

Commit a95823e

Browse files
committed
modified: pkg/R/RDD.R
1 parent 554bda0 commit a95823e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

pkg/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ exportMethods(
66
"checkpoint",
77
"cogroup",
88
"collect",
9+
"collectAsMap",
910
"collectPartition",
1011
"combineByKey",
1112
"count",

pkg/R/RDD.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ setMethod("collect",
358358
convertJListToRList(collected, flatten)
359359
})
360360

361+
361362
#' @rdname collect-methods
362363
#' @export
363364
#' @description
@@ -382,6 +383,29 @@ setMethod("collectPartition",
382383
convertJListToRList(jList, flatten = TRUE)
383384
})
384385

386+
#' @rdname collect-methods
387+
#' @export
388+
#' @description
389+
#' \code{collectAsMap} returns a named list as a map that contains all of the elements
390+
#' in a key-value pair RDD.
391+
#' @examples
392+
#'\dontrun{
393+
#' sc <- sparkR.init()
394+
#' rdd <- parallelize(sc, list(list(1, 2),list(3, 4)), 2L)
395+
#' collectAsMap(rdd) # list(`1` = 2, `3` = 4)
396+
#'}
397+
setGeneric("collectAsMap", function(rdd) { standardGeneric("collectAsMap") })
398+
399+
#' @rdname collect-methods
400+
#' @aliases collectAsMap,RDD-method
401+
setMethod("collectAsMap",
402+
signature(rdd = "RDD"),
403+
function(rdd) {
404+
pairList <- collect(rdd)
405+
map <- new.env()
406+
lapply(pairList, function(x) { assign(as.character(x[[1]]), x[[2]], envir = map) })
407+
as.list(map)
408+
})
385409

386410
#' Look up elements of a key in an RDD
387411
#'

pkg/man/collect-methods.Rd

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
% Generated by roxygen2 (4.0.2): do not edit by hand
1+
% Generated by roxygen2 (4.1.0): do not edit by hand
2+
% Please edit documentation in R/RDD.R
23
\docType{methods}
34
\name{collect}
45
\alias{collect}
56
\alias{collect,RDD-method}
7+
\alias{collectAsMap}
8+
\alias{collectAsMap,RDD-method}
69
\alias{collectPartition}
710
\alias{collectPartition,RDD,integer-method}
811
\alias{collectPartition,integer,RDD-method}
@@ -15,6 +18,10 @@ collect(rdd, ...)
1518
collectPartition(rdd, partitionId)
1619

1720
\S4method{collectPartition}{RDD,integer}(rdd, partitionId)
21+
22+
collectAsMap(rdd)
23+
24+
\S4method{collectAsMap}{RDD}(rdd)
1825
}
1926
\arguments{
2027
\item{rdd}{The RDD to collect}
@@ -24,15 +31,22 @@ collectPartition(rdd, partitionId)
2431
\item{flatten}{FALSE if the list should not flattened}
2532

2633
\item{partitionId}{the partition to collect (starts from 0)}
34+
35+
\item{rdd}{The RDD to collect as a map}
2736
}
2837
\value{
2938
a list containing elements in the RDD
39+
40+
a named list containing elements in the RDD
3041
}
3142
\description{
3243
\code{collect} returns a list that contains all of the elements in this RDD.
3344

3445
\code{collectPartition} returns a list that contains all of the elements
3546
in the specified partition of the RDD.
47+
48+
\code{collectAsMap} returns a named list as a map that contains all of the elements
49+
in this key-value pair RDD.
3650
}
3751
\examples{
3852
\dontrun{
@@ -41,5 +55,10 @@ rdd <- parallelize(sc, 1:10, 2L)
4155
collect(rdd) # list from 1 to 10
4256
collectPartition(rdd, 0L) # list from 1 to 5
4357
}
58+
\dontrun{
59+
sc <- sparkR.init()
60+
rdd <- parallelize(sc, list(list(1, 2),list(3, 4)), 2L)
61+
collectAsMap(rdd) # list(`1` = 2, `3` = 4)
62+
}
4463
}
4564

0 commit comments

Comments
 (0)