2.2.1.2 Load the Graph into Memory and Run Graph Analytics

Load the graph from the property graph schema into memory

In this section of the quickstart, you will load the graph stored in the Property Graphs schema in the database into the graph server (PGX). This will enable you to run a variety of different built-in algorithms on the graph and will also improve query performance for larger graphs.

First, start the JShell client and connect to the graph server (PGX):
./bin/opg4j --base_url https://<graph server host>:7007 --username <graphuser>
<graphuser> is the database user you will use to for the PGX server authentication. You will be prompted for the database password.

Note:

For demo purposes only, if you have set enable_tls to false in the /etc/oracle/graph/server.conf file you can use an http instead of https connection.
./bin/opg4j --base_url http://<graph server host>:7007 --username <graphuser>

This starts the shell and makes a connection to the graph server.

Note:

Always use low-privilege read-only database user accounts for PGX, as explained in Security Best Practices with Graph Data.

Next load the graph into memory in this server.

To load the graph into memory, create a PGX graph config object, using the PGX graph config builder API to do this directly in the shell.

The following example creates a PGX graph config object. It lists the properties to load into memory so that you can exclude other properties, thus reducing memory consumption.
Supplier<GraphConfig> pgxConfig = () -> { return GraphConfigBuilder.forPropertyGraphRdbms()
.setName("hr")
 .addVertexProperty("COUNTRY_NAME", PropertyType.STRING)
 .addVertexProperty("DEPARTMENT_NAME", PropertyType.STRING)
 .addVertexProperty("FIRST_NAME", PropertyType.STRING)
 .addVertexProperty("LAST_NAME", PropertyType.STRING)
 .addVertexProperty("EMAIL", PropertyType.STRING)
 .addVertexProperty("PHONE_NUMBER", PropertyType.STRING)
 .addVertexProperty("SALARY", PropertyType.DOUBLE)
 .addVertexProperty("MIN_SALARY", PropertyType.DOUBLE)
 .addVertexProperty("MAX_SALARY", PropertyType.DOUBLE)
 .addVertexProperty("STREET_ADDRESS", PropertyType.STRING)
 .addVertexProperty("POSTAL_CODE", PropertyType.STRING)
 .addVertexProperty("CITY", PropertyType.STRING)
 .addVertexProperty("STATE_PROVINCE", PropertyType.STRING)
 .addVertexProperty("REGION_NAME", PropertyType.STRING)
 .setPartitionWhileLoading(PartitionWhileLoading.BY_LABEL)
 .setLoadVertexLabels(true)
 .setLoadEdgeLabel(true)
 .build(); }
Now that you have a graph config object, use the following API to read the graph into PGX:
opg4j> var graph = session.readGraphWithProperties(pgxConfig.get())
graph ==> PgxGraph[name=hr,N=215,E=433,created=1586996113457]

The session object is created for you automatically.

Execute algorithms and query the algorithm results

Now that you have the graph in memory, you can run any built-in algorithm using a single API invocation. For example, for pagerank:
opg4j> analyst.pagerank(graph)
$31==> VertexProperty[name=pagerank,type=double,graph=hr]
As you can see from the preceding outputs, each algorithm created a new vertex property on the graph holding the output of the algorithm. To print the most important people in the graph (according to pagerank), you can run the following query:
opg4j>  session.queryPgql("SELECT m.FIRST_NAME, m.LAST_NAME, m.pagerank "
...> + "FROM MATCH (m:EMPLOYEE) ON hr ORDER BY m.pagerank DESC LIMIT 10").print().close()
+----------------------------------------------------+
| m.FIRST_NAME | m.LAST_NAME | m.pagerank |
+----------------------------------------------------+
| Adam | Fripp | 0.002959240305566317 |
| John | Russell | 0.0028810951120575284 |
| Michael | Hartstein | 0.002181365227465801 |
| Alexander | Hunold | 0.002082616009054747 |
| Den | Raphaely | 0.0020378615199327507 |
| Shelley | Higgins | 0.002028946863425767 |
| Nancy | Greenberg | 0.0017419394483596667 |
| Steven | King | 0.0016622985848193119 |
| Neena | Kochhar | 0.0015252785582170803 |
| Jennifer | Whalen | 0.0014263044976976823 |
+----------------------------------------------------+

Share the Graph with Other Sessions

After you load the graph into the graph server, you can use the publish() API to make the graph visible to other sessions, such as the graph visualization session. For example:
opg4j> graph.publish(VertexProperty.ALL, EdgeProperty.ALL)

The published graph will include any new properties you add to the graph by calling functions, such as pagerank.

You can use the Graph Visualization Application by navigating to <my-server-name>:7007/ui/ in your browser.

You can connect to a particular client session by providing the session ID when you log into the Graph Visualization Application. You will then be able to visualize all graphs in the session, even if they have not been published.

opg4j> session
session ==> PgxSession[ID=5adf83ab-31b1-4a0e-8c08-d6a95ba63ee0,source=pgxShell]

The session id is 5adf83ab-31b1-4a0e-8c08-d6a95ba63ee0.

Note:

You must create a server certificate to connect to the graph server (PGX) from the Graph Visualization Application. See Setting Up Transport Layer Security for more details.