1+ from IPython .core .magic import Magics , cell_magic , magics_class
2+ import time , json
3+ from .session import RumbleSession
4+ from py4j .protocol import Py4JJavaError
5+
6+ @magics_class
7+ class JSONiqMagic (Magics ):
8+ def run (self , line , cell = None , timed = False ):
9+ if cell is None :
10+ data = line
11+ else :
12+ data = cell
13+
14+ start = time .time ()
15+ try :
16+ rumble = RumbleSession .builder .getOrCreate ();
17+ response = rumble .jsoniq (data );
18+ except Py4JJavaError as e :
19+ print (e .java_exception .getMessage ())
20+ return
21+ except Exception as e :
22+ print ("Query unsuccessful." )
23+ print ("Usual reasons: firewall, misconfigured proxy." )
24+ print ("Error message:" )
25+ print (e .args [0 ])
26+ return
27+ except :
28+ print ("Query unsuccessful." )
29+ print ("Usual reasons: firewall, misconfigured proxy." )
30+ return
31+ end = time .time ()
32+ if (timed ):
33+ print ("Response time: %s ms" % (end - start ))
34+
35+ if ("DataFrame" in response .availableOutputs ()):
36+ print (response .pdf ())
37+ elif ("Local" in response .availableOutputs ()):
38+ count = response .getAsRDD ().count ()
39+ if count > 200 :
40+ print ("The query output %s items, which is too many to display. Displaying the first 200 items:" % count )
41+ for e in response .first ():
42+ print (json .dumps (json .loads (e .serializeAsJSON ()), indent = 2 ))
43+ else :
44+ for e in response .json ():
45+ print (json .dumps (e , indent = 2 ))
46+ elif ("PUL" in response .availableOutputs ()):
47+ print ("The query output a Pending Update List." )
48+ else :
49+ print ("No output available." )
50+
51+ @cell_magic
52+ def jsoniq (self , line , cell = None ):
53+ return self .run (line , cell , False )
54+
55+ @cell_magic
56+ def timedjsoniq (self , line , cell = None ):
57+ return self .run (line , cell , True )
0 commit comments