Skip to content
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

[SPARK-13896][SQL]Dataset.toJSON should return Dataset[String] #11732

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1987,9 +1987,9 @@ class Dataset[T] private[sql](
* @group rdd
* @since 1.3.0
*/
def toJSON: RDD[String] = {
def toJSON: Dataset[String] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually we'd need to update the documentation above.

val rowSchema = this.schema
queryExecution.toRdd.mapPartitions { iter =>
val rdd = queryExecution.toRdd.mapPartitions { iter =>
val writer = new CharArrayWriter()
// create the Generator without separator inserted between 2 records
val gen = new JsonFactory().createGenerator(writer).setRootValueSeparator(null)
Expand All @@ -2011,6 +2011,8 @@ class Dataset[T] private[sql](
}
}
}
import sqlContext.implicits._
rdd.toDS
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
assert(result2(3) === "{\"f1\":{\"f11\":4,\"f12\":true},\"f2\":{\"D4\":2147483644}}")

val jsonDF = sqlContext.read.json(primitiveFieldAndType)
val primTable = sqlContext.read.json(jsonDF.toJSON)
val primTable = sqlContext.read.json(jsonDF.toJSON.rdd)
primTable.registerTempTable("primitiveTable")
checkAnswer(
sql("select * from primitiveTable"),
Expand All @@ -1109,7 +1109,7 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
)

val complexJsonDF = sqlContext.read.json(complexFieldAndType1)
val compTable = sqlContext.read.json(complexJsonDF.toJSON)
val compTable = sqlContext.read.json(complexJsonDF.toJSON.rdd)
compTable.registerTempTable("complexTable")
// Access elements of a primitive array.
checkAnswer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with TestHiv
test("check change without refresh") {
withTempPath { tempDir =>
withTable("jsonTable") {
(("a", "b") :: Nil).toDF().toJSON.saveAsTextFile(tempDir.getCanonicalPath)
(("a", "b") :: Nil).toDF().toJSON.rdd.saveAsTextFile(tempDir.getCanonicalPath)

sql(
s"""CREATE TABLE jsonTable
Expand All @@ -179,7 +179,7 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with TestHiv
Row("a", "b"))

Utils.deleteRecursively(tempDir)
(("a1", "b1", "c1") :: Nil).toDF().toJSON.saveAsTextFile(tempDir.getCanonicalPath)
(("a1", "b1", "c1") :: Nil).toDF().toJSON.rdd.saveAsTextFile(tempDir.getCanonicalPath)

// Schema is cached so the new column does not show. The updated values in existing columns
// will show.
Expand All @@ -199,7 +199,7 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with TestHiv

test("drop, change, recreate") {
withTempPath { tempDir =>
(("a", "b") :: Nil).toDF().toJSON.saveAsTextFile(tempDir.getCanonicalPath)
(("a", "b") :: Nil).toDF().toJSON.rdd.saveAsTextFile(tempDir.getCanonicalPath)

withTable("jsonTable") {
sql(
Expand All @@ -215,7 +215,7 @@ class MetastoreDataSourcesSuite extends QueryTest with SQLTestUtils with TestHiv
Row("a", "b"))

Utils.deleteRecursively(tempDir)
(("a", "b", "c") :: Nil).toDF().toJSON.saveAsTextFile(tempDir.getCanonicalPath)
(("a", "b", "c") :: Nil).toDF().toJSON.rdd.saveAsTextFile(tempDir.getCanonicalPath)

sql("DROP TABLE jsonTable")

Expand Down