Skip to content

Commit 7dd81b7

Browse files
cafreemanDavies Liu
authored andcommitted
Documentation
1 parent 0e2a94f commit 7dd81b7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

R/pkg/R/SQLContext.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,23 @@ createExternalTable <- function(sqlCtx, tableName, path = NULL, source = NULL, .
517517
dataFrame(sdf)
518518
}
519519

520+
#' Create a Schema object
521+
#'
522+
#' Create an object of type "struct" that contains the metadata for a DataFrame. Intended for
523+
#' use with createDataFrame and toDF.
524+
#'
525+
#' @param field a Field object (created with the field() function)
526+
#' @param ... additional Field objects
527+
#' @return a Schema object
528+
#' @export
529+
#' @examples
530+
#'\dontrun{
531+
#' sc <- sparkR.init()
532+
#' sqlCtx <- sparkRSQL.init(sc)
533+
#' rdd <- lapply(parallelize(sc, 1:10), function(x) { list(x, as.character(x)) })
534+
#' schema <- buildSchema(field("a", "integer"), field("b", "string"))
535+
#' df <- createDataFrame(sqlCtx, rdd, schema)
536+
#' }
520537
buildSchema <- function(field, ...) {
521538
fields <- list(field, ...)
522539
if (!all(sapply(fields, inherits, "field"))) {
@@ -526,6 +543,7 @@ buildSchema <- function(field, ...) {
526543
structure(fields, class = "struct")
527544
}
528545

546+
# print method for "struct" object
529547
print.struct <- function(x, ...) {
530548
cat(sapply(x, function(field) { paste("|-", "name = \"", field$name,
531549
"\", type = \"", field$type,
@@ -534,6 +552,25 @@ print.struct <- function(x, ...) {
534552
, sep = "")
535553
}
536554

555+
#' Create a Field object
556+
#'
557+
#' Create a Field object that contains the metadata for a single field in a schema.
558+
#'
559+
#' @param name The name of the field
560+
#' @param type The data type of the field
561+
#' @param nullable A logical vector indicating whether or not the field is nullable
562+
#' @return a Field object
563+
#' @export
564+
#' @examples
565+
#'\dontrun{
566+
#' sc <- sparkR.init()
567+
#' sqlCtx <- sparkRSQL.init(sc)
568+
#' rdd <- lapply(parallelize(sc, 1:10), function(x) { list(x, as.character(x)) })
569+
#' field1 <- field("a", "integer", TRUE)
570+
#' field2 <- field("b", "string", TRUE)
571+
#' schema <- buildSchema(field1, field2)
572+
#' df <- createDataFrame(sqlCtx, rdd, schema)
573+
#' }
537574
field <- function(name, type, nullable = TRUE) {
538575
if (class(name) != "character") {
539576
stop("Field name must be a string.")
@@ -547,6 +584,7 @@ field <- function(name, type, nullable = TRUE) {
547584
structure(list("name" = name, "type" = type, "nullable" = nullable), class = "field")
548585
}
549586

587+
# print method for Field objects
550588
print.field <- function(x, ...) {
551589
cat("name = \"", x$name, "\", type = \"", x$type, "\", nullable = ", x$nullable, sep = "")
552590
}

0 commit comments

Comments
 (0)