Oracle Graph in Python (Setup Jupyter)

Ryota Yamanaka
Oracle Developers
Published in
4 min readMay 16, 2022

--

Oracle Graph can be accessed through Python via Jupyter notebooks, enabling data analysts and developers to easily add powerful graph analytics to their applications. Jupyter notebooks are a great way to get started quickly with exploring data, visualizing it, and performing graph analysis. And they’re easy to use, even if you don’t know a lot about graphs.

In this post, we’ll show the first step: how to install Jupyter in the cloud environment where you set up Oracle Graph. In the previous articles, “Building Oracle Graph in the Cloud” for both ADB and DBCS, we built a Graph Server from a Marketplace image, so we will install Jupyter on the same compute instance.

Install Jupyter

Open port 8888 to access Jupyter via web browsers.

We can temporally set 0.0.0.0/0 to allow access from all IP addresses. In production use, restrict the IP addresses of clients for better security.

Log in to the compute instance.

$ ssh <public_ip_address> -l opc -i <secret_key_file>

Install Jupyter. We will be following the official instructions below.

$ sudo pip3 install --upgrade pip
$ pip3 install jupyter

If the command is not found, make sure the PATH environment variable is properly set.

$ vi ~/.bash_profile

# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH

$ source ~/.bash_profile

Verify that the jupyter command is available. (See below if the command is not found.)

$ jupyter --version
Selected Jupyter core packages...
IPython : 8.4.0
ipykernel : 6.15.1
ipywidgets : 8.0.1
jupyter_client : 7.3.5
jupyter_core : 4.11.1
jupyter_server : not installed
jupyterlab : not installed
nbclient : 0.6.7
nbconvert : 7.0.0
nbformat : 5.4.0
notebook : 6.4.12
qtconsole : 5.3.1
traitlets : 5.3.0

Generate a configuration file.

$ jupyter notebook --generate-config
Writing default config to: /home/opc/.jupyter/jupyter_notebook_config.py

Change the settings to allow access from all IP addresses.

$ vi /home/opc/.jupyter/jupyter_notebook_config.py

## The IP address the notebook server will listen on.
#c.NotebookApp.ip = 'localhost'
c.NotebookApp.ip = '*'

Set the login password.

$ jupyter notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /home/opc/.jupyter/jupyter_notebook_config.json

Configure the firewall to open port 8888.

$ sudo firewall-cmd --permanent --zone=public --add-port=8888/tcp
$ sudo firewall-cmd --reload
$ sudo firewall-cmd --list-all

Create a root directory for Jupyter, navigate there, and start Jupyter.

$ mkdir ~/jupyter
$ cd ~/jupyter
$ jupyter notebook --no-browser
[W 03:30:05.192 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 03:30:05.196 NotebookApp] Serving notebooks from local directory: /home/opc/jupyter
[I 03:30:05.196 NotebookApp] Jupyter Notebook 6.4.11 is running at:
[I 03:30:05.197 NotebookApp] http://oraclegraph-instance:8888/
[I 03:30:05.197 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

Access http://<public_ip_address>:8888/ with a web browser.

Input the password and log in.

Jupyter is now installed.

Connect to Graph Server

Let’s connect to the Graph Server and run a PGQL query.

The first cell creates a session to the Graph Server. The password entered here is the one set for the database user, GRAPHUSER. Graph Server will query the database and, if this user is successfully authenticated, create a session and return a session ID.


from opg4py import graph_server
from pypgx import setloglevel
from getpass import getpass

setloglevel("ROOT", "WARN")

base_url = "https://localhost:7007"
username = "graphuser"
password = getpass("Password: ")

instance = graph_server.get_instance(base_url, username, password)
session = instance.create_session("jupyter")
analyst = session.create_analyst()

print(session)
PgxSession(id: 9021c495-f3c4-4040-9aae-4c38814fc59b, name: jupyter)

In the version 22.3 or earlier, please import graph_server from pypgx package as below (the first line).

import pypgx.pg.rdbms.graph_server as graph_server

Get the graph hr, which is loaded by default, so the response will show the number of nodes and edges of the graph.

graph = session.get_graph("hr")
graph
PgxGraph(name: hr, v: 215, e: 509, directed: True, memory(Mb): 0)

This is a PGQL query to get five edges and the vertices at both ends.

session.query_pgql("""
SELECT *
FROM MATCH (v1)-[e]->(v2) ON hr
LIMIT 5
""").print()

Using this environment, we will see how to visualize query results, perform graph algorithms, and more, in future articles. Stay tuned!

Please learn more about Oracle Graph from:

--

--