Java (PGX) Interpreter

Java (PGX) paragraphs start with %java-pgx and expose the full Java language (based on JDK 11) as well as all the available Java (PGX) APIs.

See the Javadoc for more information on the Java APIs.

Tip:

You can hover over the bottom part of a notebook paragraph and click the java_pgx_icon Add Java-PGX Paragraph icon to open a Java (PGX) paragraph instantly in the notebook.

Some variables are built-in to make interaction with PGX easier:

  • session: the PgxSession object bound to your user. You can access all graphs currently loaded into memory via the session object. Note that sessions time out after a while of not being used. A new session will be created when you log back in to the notebook; thus, the underlying session ID is not always the same.

  • instance: the ServerInstance pointing to the PGX server.

  • visualQuery: a helper object to convert PGQL queries into visualizable output.

  • connectionPool: an instance of java.sql.DataSource for managing a pool of database connections.

The following imports are available on all Java (PGX) paragraphs:

import java.io.*
import java.util.concurrent.TimeUnit
import org.apache.commons.io.*
import oracle.pgx.common.*
import oracle.pgx.common.mutations.*
import oracle.pgx.common.types.*
import oracle.pgx.api.*
import oracle.pgx.api.admin.*
import oracle.pgx.config.*
import oracle.pg.rdbms.pgql.*
import oracle.pg.rdbms.pgql.pgview.*
import oracle.pgx.api.filter.*
import oracle.pgx.api.PgxGraph.SortOrder
import oracle.pgx.api.PgxGraph.Degree
import oracle.pgx.api.PgxGraph.Mode
import oracle.pgx.api.PgxGraph.SelfEdges
import oracle.pgx.api.PgxGraph.MultiEdges
import oracle.pgx.api.PgxGraph.TrivialVertices

The following is an example of a Java (PGX) paragraph:

%java-pgx
var g = session.getGraph("MY_FIRST_GRAPH") // reference in-memory graphs by name
session.createAnalyst().pagerank(g) // run algorithms

You can also define new helper classes/functions inside paragraphs. For example:

%java-pgx
import java.lang.Math // import 

// can define new classes
public class Functions {
    public static double haversine(double lat1, double lon1, double lat2, double lon2) {
        double delta_lon = (lon2 - lon1) * Math.PI / 180;
        double delta_lat = (lat2 - lat1) * Math.PI / 180;
        double a = Math.pow(Math.sin(delta_lat / 2 ), 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.pow(Math.sin(delta_lon / 2), 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double r = 6371; // Radius of the Earth in kilometers. Use 3956 for miles
        return c * r;
    }
}

Functions.haversine(30.26, 97.74, 48.13, 11.58)

Internally, the Java (PGX) interpreter operates on the same PGX session as the Python (PGX) interpreter. So, any analysis results computed in Python (PGX) paragraphs are available for querying in subsequent Java (PGX) paragraphs.

The following example show the PageRank values computed on a graph in a Python (PGX) paragraph. The pagerank property on the graph is then queried in the subsequent Java (PGX) paragraph.

%python-pgx
g = session.get_graph("MY_FIRST_GRAPH")
analyst.pagerank(g,tol=0.001,damping=0.85,max_iter=100,norm=False,rank='pagerank')
%java-pgx
session.getGraph("MY_FIRST_GRAPH").queryPgql("SELECT x.pagerank FROM MATCH (x)").print(out,10,0)

Additionally, with single sign-on support for Graph Studio on your Autonomous AI Database, you can access the built-in connectionPool object in a Java paragraph. Note that the connectionPool object is an instance of java.sql.DataSource which points to your Autonomous AI Database instance. The following example gets a database connection from a pool using the connectionPool object, runs a SQL query, iterates over the result set, and prints the query results.

%java-pgx
import java.sql.*

String sql = "SELECT 'Hello from Oracle' AS msg, SYSDATE AS now FROM dual";

try (Connection conn = connectionPool.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery()) {

    while (rs.next()) {
        String msg = rs.getString("msg");
        Timestamp now = rs.getTimestamp("now");
        out.println(msg + " | " + now);
    }
}

See Known Issues for Graph Studio to learn about any known problems when executing a Java (PGX) paragraph.