@@ -517,6 +517,23 @@ createExternalTable <- function(sqlCtx, tableName, path = NULL, source = NULL, .
517
517
dataFrame(sdf )
518
518
}
519
519
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
+ # ' }
520
537
buildSchema <- function (field , ... ) {
521
538
fields <- list (field , ... )
522
539
if (! all(sapply(fields , inherits , " field" ))) {
@@ -526,6 +543,7 @@ buildSchema <- function(field, ...) {
526
543
structure(fields , class = " struct" )
527
544
}
528
545
546
+ # print method for "struct" object
529
547
print.struct <- function (x , ... ) {
530
548
cat(sapply(x , function (field ) { paste(" |-" , " name = \" " , field $ name ,
531
549
" \" , type = \" " , field $ type ,
@@ -534,6 +552,25 @@ print.struct <- function(x, ...) {
534
552
, sep = " " )
535
553
}
536
554
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
+ # ' }
537
574
field <- function (name , type , nullable = TRUE ) {
538
575
if (class(name ) != " character" ) {
539
576
stop(" Field name must be a string." )
@@ -547,6 +584,7 @@ field <- function(name, type, nullable = TRUE) {
547
584
structure(list (" name" = name , " type" = type , " nullable" = nullable ), class = " field" )
548
585
}
549
586
587
+ # print method for Field objects
550
588
print.field <- function (x , ... ) {
551
589
cat(" name = \" " , x $ name , " \" , type = \" " , x $ type , " \" , nullable = " , x $ nullable , sep = " " )
552
590
}
0 commit comments