【问题标题】:Serializing to disk and deserializing Scala objects using Pickling使用 Pickling 序列化到磁盘和反序列化 Scala 对象
【发布时间】:2015-06-08 13:03:31
【问题描述】:

给定一个同构类型对象流,我将如何将它们序列化为二进制,将它们写入磁盘,从磁盘读取它们,然后使用 Scala Pickling 反序列化它们?

例如:

object PicklingIteratorExample extends App {

    import scala.pickling.Defaults._
    import scala.pickling.binary._
    import scala.pickling.static._

    case class Person(name: String, age: Int)

    val personsIt = Iterator.from(0).take(10).map(i => Person(i.toString, i))
    val pklsIt = personsIt.map(_.pickle)

    ??? // Write to disk
    val readIt: Iterator[Person] = ??? // Read from disk and unpickle
} 

【问题讨论】:

    标签: scala serialization binary deserialization scala-pickling


    【解决方案1】:

    我为标准文件找到了一种方法:

    object PickleIOExample extends App {
        import scala.pickling.Defaults._
        import scala.pickling.binary._
        import scala.pickling.static._
    
        val tempPath = File.createTempFile("pickling", ".gz").getAbsolutePath
        val outputStream = new FileOutputStream(tempPath) 
        val inputStream = new FileInputStream(tempPath) 
    
        val persons = for{
            i <- 1 to 100
        } yield Person(i.toString, i)
    
        val output = new StreamOutput(outputStream)
        persons.foreach(_.pickleTo(output))
        outputStream.close()
    
        val personsIt = new Iterator[Person]{
            val streamPickle = BinaryPickle(inputStream)
    
            override def hasNext: Boolean = inputStream.available > 0
    
            override def next(): Person = streamPickle.unpickle[Person]
        }
    
        println(personsIt.mkString(", "))
        inputStream.close()
    }
    

    但我仍然无法找到适用于 gzip 文件的解决方案。既然不知道怎么检测EOF呢?由于 GZIPInputStream 可用方法不指示 EOF,因此以下引发 EOF 异常:

    object PickleIOExample extends App {
        import scala.pickling.Defaults._
        import scala.pickling.binary._
        import scala.pickling.static._
    
        val tempPath = File.createTempFile("pickling", ".gz").getAbsolutePath
        val outputStream = new GZIPOutputStream(new FileOutputStream(tempPath))
        val inputStream = new GZIPInputStream(new FileInputStream(tempPath))
    
        val persons = for{
            i <- 1 to 100
        } yield Person(i.toString, i)
    
        val output = new StreamOutput(outputStream)
        persons.foreach(_.pickleTo(output))
        outputStream.close()
    
        val personsIt = new Iterator[Person]{
            val streamPickle = BinaryPickle(inputStream)
    
            override def hasNext: Boolean = inputStream.available > 0
    
            override def next(): Person = streamPickle.unpickle[Person]
        }
    
        println(personsIt.mkString(", "))
        inputStream.close()
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 2012-11-23
      • 2017-08-02
      • 1970-01-01
      • 2014-08-18
      • 2015-11-14
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      相关资源
      最近更新 更多