2016-10-27 88 views
0

我对akka-http非常陌生,我想用任意数量的行传输csv。在scala中使用akka-http流式传输CSV

举例来说,我想回:

a,1 
b,2 
c,3 

用下面的代码

implicit val actorSystem = ActorSystem("system") 
implicit val actorMaterializer = ActorMaterializer() 

val map = new mutable.HashMap[String, Int]() 
map.put("a", 1) 
map.put("b", 2) 
map.put("c", 3) 
val `text/csv` = ContentType(MediaTypes.`text/csv`, `UTF-8`) 
val route = 
    path("test") { 
    complete { 
     HttpEntity(`text/csv`, ??? using map) 
    } 
    } 
Http().bindAndHandle(route,"localhost",8080) 

感谢您的帮助

编辑:多亏了拉蒙Ĵ罗梅罗ÿ守夜

package test 


import akka.actor.ActorSystem 
import akka.http.scaladsl.Http 
import akka.http.scaladsl.model.HttpCharsets.`UTF-8` 
import akka.http.scaladsl.model._ 
import akka.http.scaladsl.server.Directives._ 
import akka.stream._ 
import akka.util.ByteString 

import scala.collection.mutable 

object Test{ 

    def main(args: Array[String]) { 

    implicit val actorSystem = ActorSystem("system") 
    implicit val actorMaterializer = ActorMaterializer() 

    val map = new mutable.HashMap[String, Int]() 
    map.put("a", 1) 
    map.put("b", 2) 
    map.put("c", 3) 

    val mapStream = Stream.fromIterator(() => map.toIterator) 
     .map((k: String, v: Int) => s"$k,$v") 
     .map(ByteString.apply) 
    val `text/csv` = ContentType(MediaTypes.`text/csv`, `UTF-8`) 
    val route = 
     path("test") { 
     complete { 
      HttpEntity(`text/csv`, mapStream) 
     } 
     } 
    Http().bindAndHandle(route, "localhost", 8080) 

    } 
} 

有了这个代码,我有两个编译错误:

Error:(29, 28) value fromIterator is not a member of object scala.collection.immutable.Stream 
val mapStream = Stream.fromIterator(() => map.toIterator) 

Error:(38, 11) overloaded method value apply with alternatives: 
    (contentType: akka.http.scaladsl.model.ContentType,file: java.io.File,chunkSize: Int)akka.http.scaladsl.model.UniversalEntity <and> 
    (contentType: akka.http.scaladsl.model.ContentType,data: akka.stream.scaladsl.Source[akka.util.ByteString,Any])akka.http.scaladsl.model.HttpEntity.Chunked <and> 
    (contentType: akka.http.scaladsl.model.ContentType,data: akka.util.ByteString)akka.http.scaladsl.model.HttpEntity.Strict <and> 
    (contentType: akka.http.scaladsl.model.ContentType,bytes: Array[Byte])akka.http.scaladsl.model.HttpEntity.Strict <and> 
    (contentType: akka.http.scaladsl.model.ContentType.NonBinary,string: String)akka.http.scaladsl.model.HttpEntity.Strict 
cannot be applied to (akka.http.scaladsl.model.ContentType.WithCharset, List[akka.util.ByteString]) 
      HttpEntity(`text/csv`, mapStream) 

我用一个元组列表,以获得约于第一个问题(豪尔我不知道该怎么流在斯卡拉地图) 不知道的第二个 感谢您的帮助。

(我使用Scala的2.11.8)

回答

3

使用apply功能HttpEntity这需要在Source[ByteString,Any]。申请创建一个Chunked实体。您可以使用基于documentation代码使用阿卡流Source流文件IO读取文件:

import akka.stream.scaladsl._ 

val file = Paths.get("yourFile.csv") 

val entity = HttpEntity(`txt/csv`, FileIO.fromPath(file)) 

流将文件分解成块大小,default is currently set to 8192

要流,你已经创造了你可以使用类似伎俩地图:

val mapStream = Source.fromIterator(() => map.toIterator) 
         .map((k : String, v : Int) => s"$k,$v") 
         .map(ByteString.apply) 

val mapEntity = HttpEntity(`test/csv`, mapStream) 
+0

谢谢您的回答,我应该怎么用这个流我的地图,而不是一个文件? – ogen

+0

@ogen看到更新的答案... –

+0

感谢您的回答,但我有一个类型不匹配编译错误,预计ToResponseMarshallable,实际任何。感谢您的帮助 – ogen