9 Quick Starts for Using Property Graph Views
This chapter contains quick start tutorials and other resources to help you get started on working with property graph views.
- Using Sample Data for Graph Analysis
- Quick Start: Working with Property Graph Views
This tutorial helps you get started on creating, querying and executing graph algorithms on property graph views. - Quick Start: Using the Python Client as a Module
This section describes how to use the Python client as a module in Python applications. - Oracle LiveLabs Workshops for Graphs
You can also explore Oracle Property Graph features using the graph workshops in Oracle LiveLabs.
Parent topic: Property Graph Views
9.1 Using Sample Data for Graph Analysis
The rpm installation of the graph server provides you with sample graph data in
/opt/oracle/graph/data
directory.
The bank_graph
folder contains data that represent the
vertices and edges of a graph in bank_nodes.csv
and
bank_edges_amt.csv
files respectively. You can import the graph
data from these .csv
files into the database. You can then create a
graph for querying and analyses.
9.1.1 Importing Data from CSV Files
-
See Data Import Wizard in Oracle SQL Developer User's Guide on how to import data from files into tables.
- See Oracle Database Utilities for more information on data transfer utilities.
The following instructions enable you to load data into the database tables using Oracle SQL Loader.
bank_accounts
) and edge
(bank_txns
) tables in the database:CREATE TABLE bank_accounts(id NUMBER, name VARCHAR2(10));
CREATE TABLE bank_txns(from_acct_id NUMBER, to_acct_id NUMBER, description VARCHAR2(10), amount NUMBER);
You can then perform the following steps to load the data:
Parent topic: Using Sample Data for Graph Analysis
9.2 Quick Start: Working with Property Graph Views
This tutorial helps you get started on creating, querying and executing graph algorithms on property graph views.
Parent topic: Quick Starts for Using Property Graph Views
9.3 Quick Start: Using the Python Client as a Module
This section describes how to use the Python client as a module in Python applications.
Remote Server
For this mode, all you need is the Python client to be
installed. In your Python program, you must authenticate with the
remote server before you can create a session as illustrated in the
following example. Note that you must replace the values for
base_url
, jdbc_url
,
username
, and password
with values to match your environment details.
import pypgx
import opg4py
import opg4py.graph_server as graph_server
pgql_conn = opg4py.pgql.get_connection("<username>","<password>", "<jdbc_url>")
pgql_statement = pgql_conn.create_statement()
pgql = """
CREATE PROPERTY GRAPH bank_graph
VERTEX TABLES (
bank_accounts
LABEL ACCOUNTS
PROPERTIES (ID, NAME)
)
EDGE TABLES (
bank_txns
SOURCE KEY (from_acct_id) REFERENCES bank_accounts
DESTINATION KEY (to_acct_id) REFERENCES bank_accounts
LABEL TRANSFERS
PROPERTIES (FROM_ACCT_ID, TO_ACCT_ID, AMOUNT, DESCRIPTION)
) OPTIONS(PG_VIEW)
"""
pgql_statement.execute(pgql)
instance = graph_server.get_instance("<base_url>", "<username>", "<password>")
session = instance.create_session("my_session")
graph = session.read_graph_by_name('BANK_GRAPH', 'pg_view')
analyst = session.create_analyst()
analyst.pagerank(graph)
rs = graph.query_pgql("SELECT id(x), x.pagerank FROM MATCH (x) LIMIT 5")
rs.print()
To execute, save the above program into a file named
program.py
and run the following
command:
python3 program.py
You will see the following output:
+-------------------------------------------+
| id(x) | pagerank |
+-------------------------------------------+
| BANK_ACCOUNTS(2) | 9.749447313256548E-4 |
| BANK_ACCOUNTS(4) | 0.004584001759076056 |
| BANK_ACCOUNTS(6) | 5.358461393401424E-4 |
| BANK_ACCOUNTS(8) | 0.0013051552434930175 |
| BANK_ACCOUNTS(10) | 0.0015040122009364232 |
+-------------------------------------------+
Converting PGQL result set into pandas dataframe
pandas.DataFrame
object using the
to_pandas()
method. This makes it easier to
perform various data filtering operations on the result set and it
can also be used in Lambda functions. For
example,example_query = (
"SELECT n.name AS name, n.age AS age "
"WHERE (n)"
)
result_set = sample_graph.query_pgql(example_query)
result_df = result_set.to_pandas()
result_df['age_bin'] = result_df['age'].apply(lambda x: int(x)/20) # create age bins based on age ranges
Note:
To view the complete set of available Python APIs, see OPG4PY Python API Reference.Embedded Server
For this mode, the Python client and the Graph Server RPM package must be installed on the same machine.
import os
os.environ["PGX_CLASSPATH"] = "/opt/oracle/graph/lib/*"
instance = graph_server.get_embedded_instance()
session = instance.create_session("python_pgx_client")
print(session)
To execute, save the above program into a file named
program.py
and run the following
command.
python3 program.py
After successful login, you must see a similar message indicating a PGX session was created:
PgxSession(id: 32fc7037-18f1-4381-ba94-107e5f63aec2, name: python_pgx_client)
Note:
To view the complete set of available Python APIs, see OPG4PY Python API Reference.Parent topic: Quick Starts for Using Property Graph Views
9.4 Oracle LiveLabs Workshops for Graphs
You can also explore Oracle Property Graph features using the graph workshops in Oracle LiveLabs.
See the Oracle LiveLabs Workshop for a complete example on querying, analyzing and visualizing graphs using data stored in a free tier Autonomous Database instance. You will provision a new free tier Autonomous Database instance, load data into it, create a graph, and then query, analyze and visualize the graph.
Parent topic: Quick Starts for Using Property Graph Views