-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add methods * Add WriteStreamTests * Remove withWatermark
- Loading branch information
1 parent
4e1b90b
commit 45764e8
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
dataset/src/test/scala/frameless/forward/WriteStreamTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package frameless | ||
|
||
import java.util.UUID | ||
|
||
import org.apache.spark.sql.Encoder | ||
import org.apache.spark.sql.execution.streaming.MemoryStream | ||
import org.scalacheck.Prop._ | ||
import org.scalacheck.{Arbitrary, Gen, Prop} | ||
|
||
class WriteStreamTests extends TypedDatasetSuite { | ||
|
||
val genNested = for { | ||
d <- Arbitrary.arbitrary[Double] | ||
as <- Arbitrary.arbitrary[String] | ||
} yield Nested(d, as) | ||
|
||
val genOptionFieldsOnly = for { | ||
o1 <- Gen.option(Arbitrary.arbitrary[Int]) | ||
o2 <- Gen.option(genNested) | ||
} yield OptionFieldsOnly(o1, o2) | ||
|
||
val genWriteExample = for { | ||
i <- Arbitrary.arbitrary[Int] | ||
s <- Arbitrary.arbitrary[String] | ||
on <- Gen.option(genNested) | ||
ooo <- Gen.option(genOptionFieldsOnly) | ||
} yield WriteExample(i, s, on, ooo) | ||
|
||
test("write csv") { | ||
val spark = session | ||
import spark.implicits._ | ||
def prop[A: TypedEncoder: Encoder](data: List[A]): Prop = { | ||
val uid = UUID.randomUUID() | ||
val uidNoHyphens = uid.toString.replace("-", "") | ||
val filePath = s"$TEST_OUTPUT_DIR/$uid}" | ||
val checkpointPath = s"$TEST_OUTPUT_DIR/checkpoint/$uid" | ||
val inputStream = MemoryStream[A] | ||
val input = TypedDataset.create(inputStream.toDS()) | ||
val inputter = input.writeStream.format("csv").option("checkpointLocation", s"$checkpointPath/input").start(filePath) | ||
inputStream.addData(data) | ||
inputter.processAllAvailable() | ||
val dataset = TypedDataset.createUnsafe(sqlContext.readStream.schema(input.schema).csv(filePath)) | ||
|
||
val tester = dataset | ||
.writeStream | ||
.option("checkpointLocation", s"$checkpointPath/tester") | ||
.format("memory") | ||
.queryName(s"testCsv_$uidNoHyphens") | ||
.start() | ||
tester.processAllAvailable() | ||
val output = spark.table(s"testCsv_$uidNoHyphens").as[A] | ||
TypedDataset.create(data).collect().run().groupBy(identity) ?= output.collect().groupBy(identity).map { case (k, arr) => (k, arr.toSeq) } | ||
} | ||
|
||
check(forAll(Gen.nonEmptyListOf(Gen.alphaNumStr.suchThat(_.nonEmpty)))(prop[String])) | ||
check(forAll(Gen.nonEmptyListOf(Arbitrary.arbitrary[Int]))(prop[Int])) | ||
} | ||
|
||
test("write parquet") { | ||
val spark = session | ||
import spark.implicits._ | ||
def prop[A: TypedEncoder: Encoder](data: List[A]): Prop = { | ||
val uid = UUID.randomUUID() | ||
val uidNoHyphens = uid.toString.replace("-", "") | ||
val filePath = s"$TEST_OUTPUT_DIR/$uid}" | ||
val checkpointPath = s"$TEST_OUTPUT_DIR/checkpoint/$uid" | ||
val inputStream = MemoryStream[A] | ||
val input = TypedDataset.create(inputStream.toDS()) | ||
val inputter = input.writeStream.format("parquet").option("checkpointLocation", s"$checkpointPath/input").start(filePath) | ||
inputStream.addData(data) | ||
inputter.processAllAvailable() | ||
val dataset = TypedDataset.createUnsafe(sqlContext.readStream.schema(input.schema).parquet(filePath)) | ||
|
||
val tester = dataset | ||
.writeStream | ||
.option("checkpointLocation", s"$checkpointPath/tester") | ||
.format("memory") | ||
.queryName(s"testParquet_$uidNoHyphens") | ||
.start() | ||
tester.processAllAvailable() | ||
val output = spark.table(s"testParquet_$uidNoHyphens").as[A] | ||
TypedDataset.create(data).collect().run().groupBy(identity) ?= output.collect().groupBy(identity).map { case (k, arr) => (k, arr.toSeq) } | ||
} | ||
|
||
check(forAll(Gen.nonEmptyListOf(genWriteExample))(prop[WriteExample])) | ||
} | ||
} |