Apache Livy: query Spark SQL via REST: possible?

后端 未结 1 1187
情歌与酒
情歌与酒 2021-01-14 18:34

The apache Livy documentation is sparse: is it possible to return Spark SQL query resultsets as REST calls using Apache Livy? The calling application is mobile and it cannot

相关标签:
1条回答
  • 2021-01-14 18:51

    Yes, it is possible to submit Spark SQL queries through Livy. However, there is [currently] no support for the queries being submitted on their own. They would need to be wrapped in Python or Scala code.

    Here are two examples of executing Spark SQL queries using Python to interact with Livy via requests lib and Scala code as a string to be executed "in spark":

    1) using %json magic in livy (https://github.com/apache/incubator-livy/blob/412ccc8fcf96854fedbe76af8e5a6fec2c542d25/repl/src/test/scala/org/apache/livy/repl/PythonInterpreterSpec.scala#L91)

    session_url = host + "/sessions/1"
    statements_url = session_url + '/statements'
    data = {
            'code': textwrap.dedent("""\
            val d = spark.sql("SELECT COUNT(DISTINCT food_item) FROM food_item_tbl")
            val e = d.collect
            %json e
            """)}
    r = requests.post(statements_url, data=json.dumps(data), headers=headers)
    print r.json()
    

    2) using %table magic in livy (https://github.com/apache/incubator-livy/blob/412ccc8fcf96854fedbe76af8e5a6fec2c542d25/repl/src/test/scala/org/apache/livy/repl/PythonInterpreterSpec.scala#L105)

    session_url = host + "/sessions/21"
    statements_url = session_url + '/statements'
    data = {
            'code': textwrap.dedent("""\
            val x = List((1, "a", 0.12), (3, "b", 0.63))
            %table x
            """)}
    r = requests.post(statements_url, data=json.dumps(data), headers=headers)
    print r.json()
    
    0 讨论(0)
提交回复
热议问题