是否有可能在scala / Akka中读取.xls文件和.xlsx作为块?

时间:2017-11-22 10:47:37

标签: scala

将块中的文件上传到包含其他字段的服务器

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>If you hover over this Thumbnail the Video will start but once you move the mouse away from the image the Thumbnail (Poster) should appear again with an Overlay this is what I try to do.</p>
<div class="video">
    <video class="thevideo" loop muted preload="auto" poster="https://peach.blender.org/wp-content/uploads/bbb-splash.png?x11217">
        <source src="http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4" type="video/mp4">
    </video>
</div>

上传路线,

 def readFile(): Seq[ExcelFile] = {
    logger.info(" readSales method initiated: ")
    val source_test = source("E:/dd.xlsx")
    println( "  source_test "+source_test)
   val source_test2 = Source.fromFile(source_test)
    println( "  source_test2 "+source_test)
    //logger.info(" source: "+source)
    for {
      line <- source_test2.getLines().drop(1).toVector
      values = line.split(",").map(_.trim)
     // logger.info(" values are the: "+values)
    } yield ExcelFile(Option(values(0)), Option(values(1)), Option(values(2)), Option(values(3)))
  }

  def source(filePath: String): String = {
    implicit val codec = Codec("UTF-8")
    codec.onMalformedInput(CodingErrorAction.REPLACE)
    codec.onUnmappableCharacter(CodingErrorAction.REPLACE)
    Source.fromFile(filePath).mkString
  }

使用上面的代码,是否可以在scala或Akka中读取像文件一样的excel文件

1 个答案:

答案 0 :(得分:1)

查看您的代码后,就像您遇到文件的后处理(上传后)问题一样。

如果上传一个3GB文件即使对一个用户也有效,那么我认为它已经是chunkedmultipart

第一个问题就在这里 - source_test2.getLines().drop(1).toVector创建了一个Vector(> 3GB)并且文件中包含所有行。

另一个问题是你将整个Seq[ExcelFile]保留在内存中应该大于3 GB(因为Java对象开销)。

因此,无论何时调用此readFile函数,您使用的内存都超过6 GB

您应该尽量避免在应用中创建如此大的对象并使用Iterator而不是Seq

之类的内容
def readFile(): Iterator[ExcelFile] = {
  val lineIterator = Source.fromFile("your_file_path").getLines
  lineIterator.drop(1).map(line => {
    val values = line.split(",").map(_.trim)
    ExcelFile(
        Option(values(0)),
        Option(values(1)),
        Option(values(2)),
        Option(values(3))
    )
  })
}

Iterator的优势在于它不会立即加载内存中的所有内容。您可以继续使用迭代器进行进一步的操作。