从cassandra返回有效的json数据(akka http)

ycggw6v2  于 2021-06-13  发布在  Cassandra
关注(0)|答案(1)|浏览(461)

这个问题在这里已经有了答案

使用datastax java驱动程序以json形式查询行(1个答案)
7个月前关门了。
尽管select语句中有json标志,但来自cassandra数据库的数据返回时仍然是无效的json。
以下是我收到的:
当我的前端收到这个消息时,它当然被认为是无效的json。我也不知道为什么 "[json]" 有。

Future(Success(["[json]":'{"pasta_name": "conchiglie", "id": 2, "description": "description for conchiglie", "image": {"alt": "alt text", "src": "image.jpg"}}']))

以下是我想要的:

{
    "pasta_name": "conchiglie",
    "id": 2, "description":
    "description for conchiglie",
    "image": 
        {
            "alt": "alt text", 
            "src": "image.jpg"
        }
}

这是我的简化代码

object Server extends App {

implicit val system: ActorSystem = ActorSystem("helloworld")
implicit val executor: ExecutionContext = ExecutionContext.global
implicit val materializer: ActorMaterializer = ActorMaterializer()

val sessionSettings = CassandraSessionSettings()
implicit val cassandraSession: CassandraSession =
    CassandraSessionRegistry.get(system).sessionFor(sessionSettings)

val recipes: Future[String] =
    CassandraSource(s"SELECT JSON * FROM danlough.recipe_by_pasta").map(row => row.getFormattedContents()).runWith(Sink.head)

import akka.http.scaladsl.server.Directives._
  def route = path("getRecipe") {
    get {
      respondWithHeaders(RawHeader("Access-Control-Allow-Origin", "http://localhost:3000"), RawHeader("Vary", "Origin")) {
        complete(recipes)
      }
    }
  }

Http().bindAndHandle(route, host, port)
omjgkv6w

omjgkv6w1#

你需要改变 row => row.getFormattedContents()row => row.getString(0) 只提取包含cassandra格式化的json的字段,而不是返回格式化为cql的字符串 row.getFormattedContents() .

相关问题