Livy Server:将数据帧作为JSON返回?

mat*_*usr 8 json cloudera apache-spark apache-spark-2.0 livy

我正在使用HTTP POST调用在Livy Server中执行语句localhost:8998/sessions/0/statements,具有以下正文

{
  "code": "spark.sql(\"select * from test_table limit 10\")"
}
Run Code Online (Sandbox Code Playgroud)

我想以下列格式给出答案

(...)
"data": {
  "application/json": "[
    {"id": "123", "init_date": 1481649345, ...},
    {"id": "133", "init_date": 1481649333, ...},
    {"id": "155", "init_date": 1481642153, ...},
  ]"
}
(...)
Run Code Online (Sandbox Code Playgroud)

但我得到的是

(...)
"data": {
  "text/plain": "res0: org.apache.spark.sql.DataFrame = [id: string, init_date: timestamp ... 64 more fields]"
}
(...)
Run Code Online (Sandbox Code Playgroud)

哪个是toString()数据帧的版本.

有没有办法使用Livy Server将数据帧作为JSON返回?

编辑

找到解决问题的JIRA问题:https://issues.cloudera.org/browse/LIVY-72

根据评论,可以说Livy不会也不会支持这样的功能?

Dan*_*ula 3

我对 Livy 没有太多经验,但据我所知,此端点用作交互式 shell,输出将是一个字符串,其中包含 shell 显示的实际结果。因此,考虑到这一点,我可以想出一种方法来模拟您想要的结果,但这可能不是最好的方法:

{
  "code": "println(spark.sql(\"select * from test_table limit 10\").toJSON.collect.mkString(\"[\", \",\", \"]\"))"
}
Run Code Online (Sandbox Code Playgroud)

然后,您将得到一个封装在字符串中的 JSON,以便您的客户端可以解析它。