@@ -20,6 +20,7 @@ package org.apache.spark
2020import java .io .{File , FileWriter }
2121
2222import org .apache .spark .input .PortableDataStream
23+ import org .apache .spark .storage .StorageLevel
2324
2425import scala .io .Source
2526
@@ -280,6 +281,37 @@ class FileSuite extends FunSuite with LocalSparkContext {
280281 assert(indata.toArray === testOutput)
281282 }
282283
284+ test(" portabledatastream persist disk storage" ) {
285+ sc = new SparkContext (" local" , " test" )
286+ val outFile = new File (tempDir, " record-bytestream-00000.bin" )
287+ val outFileName = outFile.getAbsolutePath()
288+
289+ // create file
290+ val testOutput = Array [Byte ](1 ,2 ,3 ,4 ,5 ,6 )
291+ val bbuf = java.nio.ByteBuffer .wrap(testOutput)
292+ // write data to file
293+ val file = new java.io.FileOutputStream (outFile)
294+ val channel = file.getChannel
295+ channel.write(bbuf)
296+ channel.close()
297+ file.close()
298+
299+ val inRdd = sc.binaryFiles(outFileName).persist(StorageLevel .DISK_ONLY )
300+ inRdd.foreach{
301+ curData : (String , PortableDataStream ) =>
302+ curData._2.toArray() // force the file to read
303+ }
304+ val mappedRdd = inRdd.map{
305+ curData : (String , PortableDataStream ) =>
306+ (curData._2.getPath(),curData._2)
307+ }
308+ val (infile : String , indata : PortableDataStream ) = mappedRdd.first
309+
310+ // Try reading the output back as an object file
311+
312+ assert(indata.toArray === testOutput)
313+ }
314+
283315 test(" portabledatastream flatmap tests" ) {
284316 sc = new SparkContext (" local" , " test" )
285317 val outFile = new File (tempDir, " record-bytestream-00000.bin" )
@@ -348,6 +380,34 @@ class FileSuite extends FunSuite with LocalSparkContext {
348380 assert(indata === testOutput)
349381 }
350382
383+ test (" negative binary record length should raise an exception" ) {
384+ // a fixed length of 6 bytes
385+ sc = new SparkContext (" local" , " test" )
386+
387+ val outFile = new File (tempDir, " record-bytestream-00000.bin" )
388+ val outFileName = outFile.getAbsolutePath()
389+
390+ // create file
391+ val testOutput = Array [Byte ](1 ,2 ,3 ,4 ,5 ,6 )
392+ val testOutputCopies = 10
393+
394+ // write data to file
395+ val file = new java.io.FileOutputStream (outFile)
396+ val channel = file.getChannel
397+ for (i <- 1 to testOutputCopies) {
398+ val bbuf = java.nio.ByteBuffer .wrap(testOutput)
399+ channel.write(bbuf)
400+ }
401+ channel.close()
402+ file.close()
403+
404+ val inRdd = sc.binaryRecords(outFileName, - 1 )
405+
406+ intercept[SparkException ] {
407+ inRdd.count
408+ }
409+ }
410+
351411 test(" file caching" ) {
352412 sc = new SparkContext (" local" , " test" )
353413 val out = new FileWriter (tempDir + " /input" )
0 commit comments