diff --git a/R/pkg/NAMESPACE b/R/pkg/NAMESPACE index 8db4d5ca1ee532c5cbc65b3a22e2234a0aa7588a..5db43ae6498dbf035dff83de049d63a2a145f8b6 100644 --- a/R/pkg/NAMESPACE +++ b/R/pkg/NAMESPACE @@ -82,6 +82,7 @@ exportMethods("arrange", "persist", "printSchema", "rbind", + "registerTempTable", "rename", "repartition", "sample", diff --git a/R/pkg/R/DataFrame.R b/R/pkg/R/DataFrame.R index c710bffa2c407cb56e76be8fbd181ac5746d0907..231e4f0f4e9ce544a5ee4b1f0885cadcbed52038 100644 --- a/R/pkg/R/DataFrame.R +++ b/R/pkg/R/DataFrame.R @@ -457,6 +457,32 @@ setMethod("createOrReplaceTempView", invisible(callJMethod(x@sdf, "createOrReplaceTempView", viewName)) }) +#' (Deprecated) Register Temporary Table +#' Registers a SparkDataFrame as a Temporary Table in the SQLContext +#' @param x A SparkDataFrame +#' @param tableName A character vector containing the name of the table +#' +#' @family SparkDataFrame functions +#' @seealso \link{createOrReplaceTempView} +#' @rdname registerTempTable-deprecated +#' @name registerTempTable +#' @export +#' @examples +#'\dontrun{ +#' sc <- sparkR.init() +#' sqlContext <- sparkRSQL.init(sc) +#' path <- "path/to/file.json" +#' df <- read.json(path) +#' registerTempTable(df, "json_df") +#' new_df <- sql("SELECT * FROM json_df") +#'} +setMethod("registerTempTable", + signature(x = "SparkDataFrame", tableName = "character"), + function(x, tableName) { + .Deprecated("createOrReplaceTempView") + invisible(callJMethod(x@sdf, "createOrReplaceTempView", tableName)) + }) + #' insertInto #' #' Insert the contents of a SparkDataFrame into a table registered in the current SQL Context. @@ -1286,7 +1312,7 @@ setMethod("dapplyCollect", #' @name gapply #' @export #' @examples -#' +#' #' \dontrun{ #' Computes the arithmetic mean of the second column by grouping #' on the first and third columns. Output the grouping values and the average. @@ -1317,7 +1343,7 @@ setMethod("dapplyCollect", #' Fits linear models on iris dataset by grouping on the 'Species' column and #' using 'Sepal_Length' as a target variable, 'Sepal_Width', 'Petal_Length' #' and 'Petal_Width' as training features. -#' +#' #' df <- createDataFrame (iris) #' schema <- structType(structField("(Intercept)", "double"), #' structField("Sepal_Width", "double"),structField("Petal_Length", "double"), diff --git a/R/pkg/R/generics.R b/R/pkg/R/generics.R index 8164e7731a15f869cff3bc9d76eef7f2cde8896d..594bf2eadc5a22d9076f06a8695e6e887aad74e7 100644 --- a/R/pkg/R/generics.R +++ b/R/pkg/R/generics.R @@ -446,6 +446,13 @@ setGeneric("covar_samp", function(col1, col2) {standardGeneric("covar_samp") }) #' @export setGeneric("covar_pop", function(col1, col2) {standardGeneric("covar_pop") }) +#' @rdname createOrReplaceTempView +#' @export +setGeneric("createOrReplaceTempView", + function(x, viewName) { + standardGeneric("createOrReplaceTempView") + }) + #' @rdname dapply #' @export setGeneric("dapply", function(x, func, schema) { standardGeneric("dapply") }) @@ -548,12 +555,9 @@ setGeneric("printSchema", function(x) { standardGeneric("printSchema") }) #' @export setGeneric("rename", function(x, ...) { standardGeneric("rename") }) -#' @rdname createOrReplaceTempView +#' @rdname registerTempTable-deprecated #' @export -setGeneric("createOrReplaceTempView", - function(x, viewName) { - standardGeneric("createOrReplaceTempView") - }) +setGeneric("registerTempTable", function(x, tableName) { standardGeneric("registerTempTable") }) #' @rdname sample #' @export diff --git a/R/pkg/inst/tests/testthat/test_sparkSQL.R b/R/pkg/inst/tests/testthat/test_sparkSQL.R index 11d69366df3201d38eb63d864b34bc501024d9b2..7aa03a9048a80b547d70d6077081ef5ef0a2bd88 100644 --- a/R/pkg/inst/tests/testthat/test_sparkSQL.R +++ b/R/pkg/inst/tests/testthat/test_sparkSQL.R @@ -443,22 +443,21 @@ test_that("jsonRDD() on a RDD with json string", { expect_equal(count(df), 6) }) -test_that("test cache, uncache and clearCache", { - df <- read.json(jsonPath) - createOrReplaceTempView(df, "table1") - cacheTable("table1") - uncacheTable("table1") - clearCache() - dropTempTable("table1") -}) - test_that("test tableNames and tables", { df <- read.json(jsonPath) createOrReplaceTempView(df, "table1") expect_equal(length(tableNames()), 1) - df <- tables() - expect_equal(count(df), 1) + tables <- tables() + expect_equal(count(tables), 1) + + suppressWarnings(registerTempTable(df, "table2")) + tables <- tables() + expect_equal(count(tables), 2) dropTempTable("table1") + dropTempTable("table2") + + tables <- tables() + expect_equal(count(tables), 0) }) test_that( @@ -471,6 +470,15 @@ test_that( dropTempTable("table1") }) +test_that("test cache, uncache and clearCache", { + df <- read.json(jsonPath) + createOrReplaceTempView(df, "table1") + cacheTable("table1") + uncacheTable("table1") + clearCache() + dropTempTable("table1") +}) + test_that("insertInto() on a registered table", { df <- read.df(jsonPath, "json") write.df(df, parquetPath, "parquet", "overwrite")