代码之家  ›  专栏  ›  技术社区  ›  vkt

将Akka http实体字节字符串转换为长字符串

  •  0
  • vkt  · 技术社区  · 7 年前

              val futureResponse = Http(system).singleRequest(
                HttpRequest(
                  HttpMethods.POST,
                  "url",
                  entity = HttpEntity(ContentTypes.`application/json`, "somequery".getBytes())
                ).withHeaders(RawHeader("X-Access-Token", "access token"))
              )
    
              futureResponse.map {
                res =>
                  res.entity.dataBytes
                    .map(convertToLong) // convert to long/int
                    .grouped(2) // group two elments together
                    .map(getRelation)// do some transform
                    .runWith(someSink) // write to sink
    
              }
    
    

    我们如何转变 ByteString Long

    1 回复  |  直到 7 年前
        1
  •  1
  •   binkabir    7 年前

    编写一个接受ByteString并返回Long ByteString=>选项的函数;选项[长]

    def toLong(x: ByteString): Option[Long] = {
     try{
         Some(x.decodeString("UTF-8").toLong)
         } catch {
          case e: Exception => None 
        }
    }
    
    推荐文章