簡體   English   中英

如何在 scala spark 中獲取結果體的值而不是完整的結果體

[英]How do I get the value of the result body rather than full result body in scala spark

我有一個 flask api 端點,當您查詢它時,響應如下:

{
    "embeddings": [
        [
            0.09660455584526062,
            0.020427927374839783,
            -0.006049016956239939
        ]
    ]
}

我在 scala 中有一些代碼可以查詢它並提取響應,但它目前得到的整個主體是這樣的:

{“嵌入”:[[-0.09660455584526062,0.020427927374839783,-0.006049016956239939]]}

我只想提取“emdeddings”中的主體,例如:

    [
        [
            0.09660455584526062,
            0.020427927374839783,
            -0.006049016956239939
        ]
    ]

問題是我該怎么做:

到目前為止的代碼:

  def callFlaskApiEndPoint(url: String): String => String = (requestBody: String) => {
    implicit val formats = DefaultFormats

    val formatted_content = "text" -> requestBody
    val jsonData = compact(render(formatted_content))
    try {
      val result = Http(url)
        .postData(jsonData)
        .header("Content-Type", "application/json")
        .header("Charset", "UTF-8")
        .option(HttpOptions.connTimeout(10000))
        .option(HttpOptions.readTimeout(5000))
        .asString

      result.body
      //tried following but not working so commented out
      //(parse(result.body) \\ "embeddings").extract[String]
    } catch {
      case e: Exception => {
        "{}"
      }
    }
  }

嘗試這個:

pretty(render(parse(str) \\ "embeddings"))

這給出了:

[ [ -0.09660455584526062, 0.020427927374839783, -0.006049016956239939 ] ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM