如何将ByteString转换为类的实例?

时间:2016-07-05 22:43:03

标签: scala akka

我使用akka流来处理事件并将它们保存在文件中。代码看起来像

def process(file: File, resultFile: File): Future[IOResult] = {
    val source: Source[ByteString, Future[IOResult]] = FileIO.fromPath(Paths.get(file.toURI))
    val sink: Sink[ByteString, Future[IOResult]] = FileIO.toPath(Paths.get(resultFile.toURI))
    source
      .via(Framing.delimiter(ByteString(System.lineSeparator), maximumFrameLength = 82, allowTruncation = true))
      .map(_.utf8String.split(","))
      .map(LineEvent(_))
      .map(lineEvent => ByteString(lineEvent.toString.getBytes))
      .runWith(sink)
  }

LineEvent看起来像

case object LineEvent {
  def apply(lineElements: Array[String]): LineEvent = {
    print(s"received: $lineElements")
    // TODO: validate the lineElements to verify the length correctness
    val eventUtcTime: ZonedDateTime = ZonedDateTime.of(LocalDateTime.parse(lineElements(1)), ZoneId.of("UTC"))
    val event: LineEvent = new LineEvent(lineElements(0), eventUtcTime, lineElements(2), lineElements(3).toLong, lineElements(4).toLong, lineElements(5).toInt, lineElements(6))
    event
  }

  def main(args: Array[String]) {
    val line = "219.108.78.45,2016-07-01T16:28:47.374,GET,0,500409,403,https://www.HkwLhe.gov"
    LineEvent(line.split(","))
  }
}

case class LineEvent(ipAddress: String, eventTime: ZonedDateTime, httpMethod: String, uploadedBytes: Long, downloadedBytes: Long, httpStatus: Int, url: String)

当我运行process(csvFile, resultFile)时,它会序列化文件中的数据。

现在我想将它们从serialized版本读回LineEvent个实例。我拥有的是

def processSerialized(file: File): Future[Done] = {
    val source: Source[ByteString, Future[IOResult]] = FileIO.fromPath(Paths.get(file.toURI))
    source
      .via(Framing.delimiter(ByteString(System.lineSeparator), maximumFrameLength = 82, allowTruncation = true))
      .map(_.utf8String)
      .runForeach(println)
  }

我跑的时候

  processSerialized(new File("/Users/harit/Downloads/tmp/input-0.ser"))

我看到了

[B@461c868e
[B@43904f4e
[B@37808d5a
[B@1c259445
[B@3e2f59b9
[B@7f849b0d

如何将它们转换回LineEvent

0 个答案:

没有答案