Browse Source

[livy] Add support for the %json magic for PySpark

Erick Tryzelaar 10 năm trước cách đây
mục cha
commit
9cd9d59640

+ 13 - 0
apps/spark/java/livy-repl/src/main/resources/fake_shell.py

@@ -268,12 +268,25 @@ def magic_table(name):
     })
 
 
+def magic_json(name):
+    try:
+        value = global_dict[name]
+    except KeyError:
+        exc_type, exc_value, tb = sys.exc_info()
+        return execute_reply_error(exc_type, exc_value, [])
+
+    return execute_reply_ok({
+        'application/json': value,
+    })
+
+
 def shutdown_request(content):
     sys.exit()
 
 
 magic_router = {
     'table': magic_table,
+    'json': magic_json,
 }
 
 

+ 14 - 0
apps/spark/java/livy-repl/src/test/scala/com/cloudera/hue/livy/repl/PythonInterpreterSpec.scala

@@ -67,6 +67,20 @@ class PythonInterpreterSpec extends BaseInterpreterSpec {
     ))
   }
 
+  it should "do json magic" in withInterpreter { interpreter =>
+    val response = interpreter.execute(
+      """x = [[1, 'a'], [3, 'b']]
+        |%json x
+      """.stripMargin)
+
+    response should equal(Interpreter.ExecuteSuccess(
+      repl.APPLICATION_JSON -> List[JValue](
+        List[JValue](1, "a"),
+        List[JValue](3, "b")
+      )
+    ))
+  }
+
   it should "do table magic" in withInterpreter { interpreter =>
     val response = interpreter.execute(
       """x = [[1, 'a'], [3, 'b']]