Import Java classes and Python modules
Software libraries allow users to share and reuse code, making the software development process more efficient. We'll cover all of the ways that libraries can be used in Deephaven. Many functions are built into Deephaven and can be manually imported in your queries. However, you can also take advantage of libraries from outside sources (e.g., Seaborn, Apache, etc.). We'll discuss how to import and install these libraries.
#
Simple ImportPython, Groovy, Java, and other supported languages have built-in mechanisms to import code. These mechanisms can be used to import code from Deephaven, third-party vendors, or you, as long as the code is available to the programming language.
For example, in Python, the Deephaven TableTools module can be imported and used to create a table with 10 rows.
The same results can be obtained more succinctly by just importing the emptyTable
function.
In Groovy, this would look like:
or
Groovy can be configured to import commonly-used classes by default. emptyTable
is one such default import. As a result, in Groovy, the query can be condensed down to.
Because Python can not be configured to import commonly-used classes by default, almost all Python queries require imports.
#
Using Java in PythonBecause Deephavenâs core database is written in Java, we have made it easy for Java and Python to work together. You can use Java libraries to solve problems in Python.
To illustrate using Java in Python, letâs create a Java String within a Python session, call Javaâs split method on the String, and iterate over the resulting Java String array.
Occasionally, you may need to create a Java array for use with a library. This is also easy.
#
Groovy SourceIn Deephavenâs Groovy implementation, files can be executed within a Groovy script. This can be useful to load functions, variables, and configurations into Groovy scripts.
To source a file:
source("/path/to/script.groovy")
To source a file only once during a session:
sourceOnce("/path/to/script.groovy")
Scripts are sourced using a script path loader - they will be retrieved from the Iris controller. The path will be based on the git parameters defined for the controller. The script must be available to the controller based on the following properties:
iris.scripts.repo.irisrepo.root
- defines the root directory used to create the search pathsiris.scripts.repo.irisrepo.paths
- defines the subdirectories which will be searched for the scripts
In a default installation, these properties resolve to the following path:
/db/TempFiles/irisadmin/git/irisrepo/groovy
#
Extend The Query LanguageBecause Deephavenâs database is implemented in Java, it is possible to use Java within query strings.
In Groovy, this can be done by providing a fully qualified path for the class.
The query strings can be made more concise by importing the class or static methods into the query language.
In Python, these queries are:
and
#
Built-In LibrariesDocumentation for built-in libraries can be found at:
#
Installing LibrariesYou can also install other libraries for use in Deephaven. To use a java library, it must be available on the java classpath. The classpath directory must be readable by the server running your query. This can be done in a few ways.
- Create a Deephaven plugin containing the jar.
- Copy the jar file to
/etc/sysconfig/illumon.d/java_lib/
. - Copy the jar file to a directory listed in the
CUSTOMER_JAR_DIR
environment variable in the hostconfig file (/etc/sysconfig/illumon.confs/illumon.iris.hostconfig
).
Deploying an open source Python package from PyPi is most commonly done by adding the new Python package to requirements.txt and regenerating the Python virtual environment (/usr/illumon/latest/install/python/auto_provision.sh
). If the virtual environments are not crossmounted on all query hosts, the virtual environments must be regenerated on each query host.
You can also make your own custom Python modules available to Deephaven queries. Your code must be on the python path. This can be done in a few ways:
- Set the
PYTHONPATH
environment variable when launching your query. - Append to the python path from within your query.
Remember that your python path must be mounted on the node where your query is run. Also, the path must be readable by the dbquery Unix user, which is the user queries run as.