|
| 1 | +#' structType |
| 2 | +#' |
| 3 | +#' Create a structType object that contains the metadata for a DataFrame. Intended for |
| 4 | +#' use with createDataFrame and toDF. |
| 5 | +#' |
| 6 | +#' @param x a Field object (created with the field() function) |
| 7 | +#' @param ... additional Field objects |
| 8 | +#' @return a structType object |
| 9 | +#' @export |
| 10 | +#' @examples |
| 11 | +#'\dontrun{ |
| 12 | +#' sc <- sparkR.init() |
| 13 | +#' sqlCtx <- sparkRSQL.init(sc) |
| 14 | +#' rdd <- lapply(parallelize(sc, 1:10), function(x) { list(x, as.character(x)) }) |
| 15 | +#' schema <- buildSchema(field("a", "integer"), field("b", "string")) |
| 16 | +#' df <- createDataFrame(sqlCtx, rdd, schema) |
| 17 | +#' } |
| 18 | +structType <- function(x, ...) { |
| 19 | + UseMethod("structType", x) |
| 20 | +} |
| 21 | + |
| 22 | +structType.jobj <- function(x) { |
| 23 | + obj <- structure(list(), class = "structType") |
| 24 | + obj$jobj <- x |
| 25 | + obj$fields <- function() { lapply(callJMethod(x, "fields"), structField) } |
| 26 | + obj |
| 27 | +} |
| 28 | + |
| 29 | +structType.structField <- function(x, ...) { |
| 30 | + fields <- list(x, ...) |
| 31 | + if (!all(sapply(fields, inherits, "structField"))) { |
| 32 | + stop("All arguments must be structField objects.") |
| 33 | + } |
| 34 | + sfObjList <- lapply(fields, function(field) { |
| 35 | + field$jobj |
| 36 | + }) |
| 37 | + stObj <- callJStatic("edu.berkeley.cs.amplab.sparkr.SQLUtils", |
| 38 | + "createStructType", |
| 39 | + listToSeq(sfObjList)) |
| 40 | + structType(stObj) |
| 41 | +} |
| 42 | + |
| 43 | +#' Print a Spark StructType. |
| 44 | +#' |
| 45 | +#' This function prints the contents of a StructType returned from the |
| 46 | +#' SparkR JVM backend. |
| 47 | +#' |
| 48 | +#' @param x A StructType object |
| 49 | +#' @param ... further arguments passed to or from other methods |
| 50 | +print.structType <- function(x, ...) { |
| 51 | + cat("StructType\n", |
| 52 | + sapply(x$fields(), function(field) { paste("|-", "name = \"", field$name(), |
| 53 | + "\", type = \"", field$dataType.toString(), |
| 54 | + "\", nullable = ", field$nullable(), "\n", |
| 55 | + sep = "") }) |
| 56 | + , sep = "") |
| 57 | +} |
| 58 | + |
| 59 | +#' structField |
| 60 | +#' |
| 61 | +#' Create a structField object that contains the metadata for a single field in a schema. |
| 62 | +#' |
| 63 | +#' @param x The name of the field |
| 64 | +#' @param type The data type of the field |
| 65 | +#' @param nullable A logical vector indicating whether or not the field is nullable |
| 66 | +#' @return a Field object |
| 67 | +#' @export |
| 68 | +#' @examples |
| 69 | +#'\dontrun{ |
| 70 | +#' sc <- sparkR.init() |
| 71 | +#' sqlCtx <- sparkRSQL.init(sc) |
| 72 | +#' rdd <- lapply(parallelize(sc, 1:10), function(x) { list(x, as.character(x)) }) |
| 73 | +#' field1 <- field("a", "integer", TRUE) |
| 74 | +#' field2 <- field("b", "string", TRUE) |
| 75 | +#' schema <- buildSchema(field1, field2) |
| 76 | +#' df <- createDataFrame(sqlCtx, rdd, schema) |
| 77 | +#' } |
| 78 | + |
| 79 | +structField <- function(x, ...) { |
| 80 | + UseMethod("structField", x) |
| 81 | +} |
| 82 | + |
| 83 | +structField.jobj <- function(x) { |
| 84 | + obj <- structure(list(), class = "structField") |
| 85 | + obj$jobj <- x |
| 86 | + obj$name <- function() { callJMethod(x, "name") } |
| 87 | + obj$dataType <- function() { callJMethod(x, "dataType") } |
| 88 | + obj$dataType.toString <- function() { callJMethod(obj$dataType(), "toString") } |
| 89 | + obj$dataType.simpleString <- function() { callJMethod(obj$dataType(), "simpleString") } |
| 90 | + obj$nullable <- function() { callJMethod(x, "nullable") } |
| 91 | + obj |
| 92 | +} |
| 93 | + |
| 94 | +structField.character <- function(x, type, nullable = TRUE) { |
| 95 | + if (class(x) != "character") { |
| 96 | + stop("Field name must be a string.") |
| 97 | + } |
| 98 | + if (class(type) != "character") { |
| 99 | + stop("Field type must be a string.") |
| 100 | + } |
| 101 | + if (class(nullable) != "logical") { |
| 102 | + stop("nullable must be either TRUE or FALSE") |
| 103 | + } |
| 104 | + options <- c("byte", |
| 105 | + "integer", |
| 106 | + "double", |
| 107 | + "numeric", |
| 108 | + "character", |
| 109 | + "string", |
| 110 | + "binary", |
| 111 | + "raw", |
| 112 | + "logical", |
| 113 | + "boolean", |
| 114 | + "timestamp", |
| 115 | + "date") |
| 116 | + dataType <- if (type %in% options) { |
| 117 | + type |
| 118 | + } else { |
| 119 | + stop(paste("Unsupported type for Dataframe:", type)) |
| 120 | + } |
| 121 | + sfObj <- callJStatic("edu.berkeley.cs.amplab.sparkr.SQLUtils", |
| 122 | + "createStructField", |
| 123 | + x, |
| 124 | + dataType, |
| 125 | + nullable) |
| 126 | + structField(sfObj) |
| 127 | +} |
| 128 | + |
| 129 | +#' Print a Spark StructField. |
| 130 | +#' |
| 131 | +#' This function prints the contents of a StructField returned from the |
| 132 | +#' SparkR JVM backend. |
| 133 | +#' |
| 134 | +#' @param x A StructField object |
| 135 | +#' @param ... further arguments passed to or from other methods |
| 136 | +print.structField <- function(x, ...) { |
| 137 | + cat("StructField(name = \"", x$name(), |
| 138 | + "\", type = \"", x$dataType.toString(), |
| 139 | + "\", nullable = ", x$nullable(), |
| 140 | + ")", |
| 141 | + sep = "") |
| 142 | +} |
| 143 | + |
| 144 | +# cfreeman: Don't think we need this function since we can create |
| 145 | +# structType in R and pass to createDataFrame |
| 146 | +# |
| 147 | +# #' dump the schema into JSON string |
| 148 | +# tojson <- function(x) { |
| 149 | +# if (inherits(x, "struct")) { |
| 150 | +# # schema object |
| 151 | +# l <- paste(lapply(x, tojson), collapse = ", ") |
| 152 | +# paste('{\"type\":\"struct\", \"fields\":','[', l, ']}', sep = '') |
| 153 | +# } else if (inherits(x, "field")) { |
| 154 | +# # field object |
| 155 | +# names <- names(x) |
| 156 | +# items <- lapply(names, function(n) { |
| 157 | +# safe_n <- gsub('"', '\\"', n) |
| 158 | +# paste(tojson(safe_n), ':', tojson(x[[n]]), sep = '') |
| 159 | +# }) |
| 160 | +# d <- paste(items, collapse = ", ") |
| 161 | +# paste('{', d, '}', sep = '') |
| 162 | +# } else if (is.character(x)) { |
| 163 | +# paste('"', x, '"', sep = '') |
| 164 | +# } else if (is.logical(x)) { |
| 165 | +# if (x) "true" else "false" |
| 166 | +# } else { |
| 167 | +# stop(paste("unexpected type:", class(x))) |
| 168 | +# } |
| 169 | +# } |
0 commit comments