Skip to content

Commit 97f293d

Browse files
author
Ghislain Fourny
committed
Fine-tune user experience.
1 parent d0d85b1 commit 97f293d

5 files changed

Lines changed: 29 additions & 11 deletions

File tree

316 Bytes
Binary file not shown.

src/jsoniq/sequence.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44
import json
55

66
class SequenceOfItems:
7-
def __init__(self, sequence, sparksession):
7+
def __init__(self, sequence, rumblesession):
88
self._jsequence = sequence
9-
self._sparkcontext = sparksession.sparkContext
10-
self._sparksession = sparksession
9+
self._rumblesession = rumblesession
10+
self._sparksession = rumblesession._sparksession
11+
self._sparkcontext = self._sparksession.sparkContext
12+
13+
def items(self):
14+
return self.getAsList()
15+
16+
def take(self, n):
17+
return tuple(self.getFirstItemsAsList(n))
18+
19+
def first(self):
20+
return tuple(self.getFirstItemsAsList(self._rumblesession.getRumbleConf().getResultSizeCap()))
1121

1222
def json(self):
13-
return tuple([json.loads(l.serializeAsJSON()) for l in self._jsequence.items()])
23+
return tuple([json.loads(l.serializeAsJSON()) for l in self._jsequence.getAsList()])
1424

1525
def rdd(self):
16-
rdd = self._jsequence.getAsPickledStringRDD();
26+
rdd = self._jsequence.getAsPickledStringRDD()
1727
rdd = RDD(rdd, self._sparkcontext)
1828
return rdd.map(lambda l: json.loads(l))
1929

@@ -22,7 +32,10 @@ def df(self):
2232

2333
def pdf(self):
2434
return self.df().toPandas()
25-
35+
36+
def count(self):
37+
return self._jsequence.count()
38+
2639
def nextJSON(self):
2740
return self._jsequence.next().serializeAsJSON()
2841

src/jsoniq/session.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def __init__(self, spark_session: SparkSession):
2222
self._sparksession = spark_session
2323
self._jrumblesession = spark_session._jvm.org.rumbledb.api.Rumble(spark_session._jsparkSession)
2424

25+
def getRumbleConf(self):
26+
return self._jrumblesession.getConfiguration()
27+
2528
class Builder:
2629
def __init__(self):
2730

@@ -166,7 +169,7 @@ def bindDataFrameAsVariable(self, name: str, df):
166169

167170
def jsoniq(self, str):
168171
sequence = self._jrumblesession.runQuery(str);
169-
return SequenceOfItems(sequence, self._sparksession);
172+
return SequenceOfItems(sequence, self);
170173

171174
def __getattr__(self, item):
172175
return getattr(self._sparksession, item)

src/jsoniqmagic/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55

66
def load_ipython_extension(ipython):
77
rumble = RumbleSession.builder.getOrCreate();
8+
rumble.getRumbleConf().setResultSizeCap(10);
89
ipython.register_magics(JSONiqMagic)

src/jsoniqmagic/magic.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ def run(self, line, cell=None, timed=False):
3535
if ("DataFrame" in response.availableOutputs()):
3636
print(response.pdf())
3737
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():
38+
capplusone = response.take(rumble.getRumbleConf().getResultSizeCap() + 1)
39+
if len(capplusone) > rumble.getRumbleConf().getResultSizeCap():
40+
count = response.count()
41+
print("The query output %s items, which is too many to display. Displaying the first %s items:" % (count, rumble.getRumbleConf().getResultSizeCap()))
42+
for e in capplusone[:-1]:
4243
print(json.dumps(json.loads(e.serializeAsJSON()), indent=2))
4344
else:
4445
for e in response.json():

0 commit comments

Comments
 (0)