16.9 Using Graph Server (PGX) as a Library
When you utilize PGX as a library in your application, the graph server (PGX) instance runs in the same JVM as the Java application and all requests are translated into direct function calls instead of remote procedure invocations.
In this case, you must install the graph server (PGX) using RPM
in the
same machine as the client applications. The shell executables provided by the graph
server installation helps you to launch the Java or the Python shell in an embedded
server mode. See Installing Oracle Graph Server for more information.
You can now start the Java shell without any parameters as shown:
cd /opt/oracle/graph
./bin/opg4j
/etc/oracle/graph/pgx.conf
You can change the location of the configuration file by passing the
--pgx_conf
command-line option followed by the path to the
configuration file:
# start local PGX instance with custom config
./bin/opg4j --pgx_conf <path_to_pgx.conf>
You can also start the Python shell without any parameters as shown:
cd /opt/oracle/graph/
./bin/opg4py
When using Java, you can obtain a reference to the local PGX instance as shown:
import oracle.pg.rdbms.*; import oracle.pgx.api.*; ... ServerInstance instance = GraphServer.getEmbeddedInstance();
In a Python application, you can obtain a reference to the local PGX instance as shown:
import os os.environ["PGX_CLASSPATH"] = "/opt/oracle/graph/lib/*" import opg4py.graph_server as graph_server ... instance = graph_server.get_embedded_instance()
Starting the PGX Engine
PGX provides a convenience mechanism to start the PGX Engine when using
the graph server (PGX) as a library. That is, the graph server (PGX) is
automatically initialized and starts up automatically when
ServerInstance.createSession()
is called the first time. This
is provided that the engine is not already running at that time.
For this implicit initialization, PGX will configure itself with the PGX configuration file at the default locations. If the PGX configuration file is not found, PGX will configure itself using default parameter values as shown in Configuration Parameters for the Graph Server (PGX) Engine.
Stopping the PGX Engine
When using the graph server (PGX) as a library, the
shutdownEngine()
method will be called automatically via a JVM
shutdown hook on exit. Specifically, the shutdown hook is invoked once all the non-daemon threads of the
application exit.
It is recommended that you do not terminate your PGX application
forcibly with kill -9
, as it will not clear the
temp
directory. See tmp_dir
in Configuration Parameters for the Graph Server (PGX) Engine.
- Using the PGX JDBC Driver when Graph Server (PGX) is Utilized as a Library
When using the graph server (PGX) as a library, you can use the PGX JDBC driver to query graphs that are loaded from files.
Parent topic: Developing Applications with Graph Analytics