scala - 使用 Pickling 序列化到磁盘并反序列化 Scala 对象

标签 scala serialization binary deserialization scala-pickling

给定同构类型对象流,我如何将它们序列化为二进制,将它们写入磁盘,从磁盘读取它们,然后使用 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
} 

最佳答案

我找到了一种方法来处理标准文件:

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()
}

关于scala - 使用 Pickling 序列化到磁盘并反序列化 Scala 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30709896/

相关文章:

c# - 在 C# 中序列化对象的一部分

memory - 技嘉还是 Gibibyte(1000 或 1024)?

ruby-on-rails - 如何在 Rails 5 中获取上传图像的二进制数据?

string - 检查 unicode 值是否在 Erlang 二进制字符串中?

Scala:编译器找不到 FromRepr、HasElem 或implicitConversions

scala - null 作为类型参数的实例

c# - 类中的 WCF 功能

list - Scala模式匹配: How to match on an element inside a list?

Scala:运算符 foldl 是中缀吗?

c++ - 如何在 C++ 中使用对象序列化保存和加载 std::string?