RumbleDB for Python
This is the Python edition of RumbleDB, which brings JSONiq and XQuery to the world of Python.
RumbleDB passes 100% of applicable W3C qt3tests. It means full builtin type support, full builtin function support, etc.
See in action!
The Python edition is relatively recent. In many cases, pip install jsoniq just works. We welcome feedback to improve the installation experience.
JSONiq is a language considerably more powerful than SQL as it can process messy, heterogeneous datasets (data in real life is not fully structured!), from kilobytes to Petabytes, with very little coding effort.
Spark aficionados can also pass Pandas DataFrames to JSONiq queries and take back DataFrames. This gives them an environment in which both Spark SQL and JSONiq co-exist to manipulate the data.
We welcome bug reports and feedback.
About RumbleDB
RumbleDB is a JSONiq engine that works both with very small amounts of data and very large amounts of data.
It works with JSON, CSV, text, Parquet, XML, etc.
It works on your laptop as well as on any Spark cluster (AWS, company clusters, etc).
It automatically detects and switches between execution modes in a way transparent to the user, bringing the convenience of data independence to the world of messy data.
It is an academic project, natively in Java, carried out at ETH Zurich by Ghislain Fourny's students over more than 8 years: Stefan Irimescu, Renato Marroquin, Rodrigo Bruno, Falko Noé, Ioana Stefan, Andrea Rinaldi, Stevan Mihajlovic, Mario Arduini, Can Berker Çıkış, Elwin Stephan, David Dao, Zirun Wang, Ingo Müller, Dan-Ovidiu Graur, Thomas Zhou, Olivier Goerens, Alexandru Meterez, Pierre Motard, Remo Röthlisberger, Dominik Bruggisser, David Loughlin, David Buzatu, Marco Schöb, Maciej Byczko, Matteo Agnoletto, Dwij Dixit, Abishek Ramdas.
It is free and open source, under an Apache 2.0 license, which can also be used commercially (but on an as-is basis with no guarantee).
High-level information on the library
A RumbleSession is a wrapper around a SparkSession that additionally makes sure the RumbleDB environment is in scope.
JSONiq queries are invoked with rumble.jsoniq() in a way similar to the way Spark SQL queries are invoked with spark.sql().
You can also use rumble.xquery() to run queries with XQuery 3.1 as the default language. It accepts the same keyword variable bindings and returns the same SequenceOfItems as rumble.jsoniq(), while preserving the session configuration. An explicit language version declaration in the query overrides the default.
result = rumble.xquery("<greeting>{$name}</greeting>", name="World")
print(result.serialize()) # XML declaration followed by <greeting>World</greeting>
JSONiq variables can be bound to lists of JSON values (str, int, float, True, False, None, dict, list) or to Pyspark DataFrames. A JSONiq query can use as many variables as needed (for example, it can join between different collections).
It will later also be possible to read tables registered in the Hive metastore, similar to spark.sql(). Alternatively, the JSONiq query can also read many files of many different formats from many places (local drive, HTTP, S3, HDFS, ...) directly with simple builtin function calls such as json-lines(), text-file(), parquet-file(), csv-file(), etc. See RumbleDB's documentation.
The resulting sequence of items can be retrieved as a list of JSON values, as a Pyspark DataFrame, or, for advanced users, with a streaming iteration over the items using the RumbleDB Item API.
It is also possible to write the sequence of items to the local disk, to HDFS, to S3, etc in a way similar to how DataFrames are written back by Pyspark.
The library also contains a jsoniq magic that allows you to directly write JSONiq queries in a Jupyter notebook and see the results automatically output on the screen. In notebooks, you can use %%jsoniq -s (or --serialize) to serialize the result sequence to text output, according to the XSLT and XQuery Serialization 3.1 specification by W3C. The method and serialization options can all be specified in the query with option declarations, following the XQuery/JSONiq standard. %%jsoniq -j shows the results in JSON lines format, while %%jsoniq -pdf shows a pandas data frame, and %%jsoniq -df shows a Spark data frame.
The same notebook extension also registers %%xquery, which calls rumble.xquery() and defaults to serialized output (-s) with the XML serialization method. Query option declarations in the http://www.w3.org/2010/xslt-xquery-serialization namespace can override the serialization method. It accepts the same options: use -j, -df, or -pdf to select another output format, -u to apply updates, and -t to show execution time.
%%xquery
<greeting>Hello, world!</greeting>
The design goal is that it is possible to chain DataFrames between JSONiq and Spark SQL queries seamlessly. For example, JSONiq can be used to clean up very messy data and turn it into a clean DataFrame, which can then be processed with Spark SQL, spark.ml, etc.
Any feedback or error reports are very welcome.
Type mapping
Any expression in JSONiq returns a sequence of items. Any variable in JSONiq is bound to a sequence of items.
Items can be objects, arrays, or atomic values (strings, integers, booleans, nulls, dates, binary, durations, doubles, decimal numbers, etc).
A sequence of items can be a sequence of just one item, but it can also be empty, or it can be as large as to contain millions, billions or even trillions of items. Obviously, for sequence longer than a billion items, it is a better idea to use a cluster than a laptop.
A relational table (or more generally a data frame) corresponds to a sequence of object items sharing the same schema. However, sequences of items are more general than tables or data frames and support heterogeneity seamlessly.
When passing Python values to JSONiq or getting them from a JSONiq queries, the mapping to and from Python is as follows:
| Python |
JSONiq |
| tuple |
sequence of items |
| dict |
object |
| list |
array |
| str |
string |
| int |
integer |
| bool |
boolean |
| None |
null |
Furthermore, other JSONiq types will be mapped to string literals. Users who want to preserve JSONiq types can use the Item API instead.
Installation
Install with
pip install jsoniq
Important note: since the jsoniq package depends on pyspark 4, Java 17 or Java 21 is a requirement. If another version of Java is installed, the execution of a Python program attempting to create a RumbleSession will lead to an error message on stderr that contains explanations.
By default, session creation uses Spark bundled with PySpark. Both getOrCreate() and create() temporarily unset SPARK_HOME and restore its original value (or absence) after startup, even if startup fails. To use an external Spark installation configured through SPARK_HOME, opt out before creating the session:
rumble = RumbleSession.builder.withBundledSpark(False).getOrCreate()
Use withBundledSpark() or withBundledSpark(True) to enable the default again. This setting cannot change the Spark installation of an existing session or JVM. Startup calls through this library are serialized while the environment is changed; unrelated threads should avoid changing SPARK_HOME or starting Spark at the same time.
Sample code
We will make more documentation available as we go. In the meantime, you will find a sample, commented code below that should just run
after installing the library.
You can directly copy paste the code below to a Python file and execute it with Python.