-
Notifications
You must be signed in to change notification settings - Fork 28.7k
[SPARK-12393] [SparkR] Add read.text and write.text for SparkR #10348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -661,6 +661,34 @@ setMethod("saveAsParquetFile", | |
write.parquet(x, path) | ||
}) | ||
|
||
#' write.text | ||
#' | ||
#' Saves the content of the DataFrame in a text file at the specified path. | ||
#' The DataFrame must have only one column that is of string type. | ||
#' Each row becomes a new line in the output file. | ||
#' | ||
#' @param x A SparkSQL DataFrame | ||
#' @param path The directory where the file is saved | ||
#' | ||
#' @family DataFrame functions | ||
#' @rdname write.text | ||
#' @name write.text | ||
#' @export | ||
#' @examples | ||
#'\dontrun{ | ||
#' sc <- sparkR.init() | ||
#' sqlContext <- sparkRSQL.init(sc) | ||
#' path <- "path/to/file.json" | ||
#' df <- read.json(sqlContext, path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a bit odd as an example? it's loading a DF as json and writing it out as text (which requires one string column? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will update it with |
||
#' write.text(df, "/tmp/sparkr-tmp/") | ||
#'} | ||
setMethod("write.text", | ||
signature(x = "DataFrame", path = "character"), | ||
function(x, path) { | ||
write <- callJMethod(x@sdf, "write") | ||
invisible(callJMethod(write, "text", path)) | ||
}) | ||
|
||
#' Distinct | ||
#' | ||
#' Return a new DataFrame containing the distinct rows in this DataFrame. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -549,6 +549,10 @@ setGeneric("write.parquet", function(x, path) { standardGeneric("write.parquet") | |
#' @export | ||
setGeneric("saveAsParquetFile", function(x, path) { standardGeneric("saveAsParquetFile") }) | ||
|
||
#' @rdname write.text | ||
#' @export | ||
setGeneric("write.text", function(x, path) { standardGeneric("write.text") }) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't read.text be added too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found all functions in SQLContext.R did not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is historical legacy:) (you can find same cases in context.R.) I guess that because we did not implement SparkContext and SQLContext as S4 classes. If there is no strong reason, we can keep it as is. |
||
#' @rdname schema | ||
#' @export | ||
setGeneric("schema", function(x) { standardGeneric("schema") }) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1481,6 +1481,25 @@ test_that("read/write Parquet files", { | |
unlink(parquetPath4) | ||
}) | ||
|
||
test_that("read/write text files", { | ||
# Test write.df and read.df | ||
df <- read.df(sqlContext, jsonPath, "text") | ||
expect_is(df, "DataFrame") | ||
expect_equal(colnames(df), c("value")) | ||
expect_equal(count(df), 3) | ||
textPath <- tempfile(pattern = "textPath", fileext = ".txt") | ||
write.df(df, textPath, "text", mode="overwrite") | ||
|
||
# Test write.text and read.text | ||
textPath2 <- tempfile(pattern = "textPath2", fileext = ".txt") | ||
write.text(df, textPath2) | ||
df2 <- read.text(sqlContext, c(textPath, textPath2)) | ||
expect_is(df2, "DataFrame") | ||
expect_equal(colnames(df2), c("value")) | ||
expect_equal(count(df2), count(df) * 2) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should unlink(textPath) and unlink(textPath2) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching this, I will update it. |
||
}) | ||
|
||
test_that("describe() and summarize() on a DataFrame", { | ||
df <- read.json(sqlContext, jsonPath) | ||
stats <- describe(df, "age") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might want to add the DataFrame must have a column with the name "value" I recall there is a similar doc clarification in Scala recently.