Creating a PG View By Importing a GraphSON file

Using the GraphImporterBuilder API, you can create a property graph view (PG View) by importing graph data from a GraphSON file.

This import functionality consists of the following steps:
  1. Parsing of the GraphSON to a data structure.
  2. Creating the SQL tables from the data structure and inserting the data.
  3. Generating and running the CREATE PROPERTY GRAPH statement.

The following example show using the GraphImporterBuilder API to create a PG View from a GraphSON file.

opg4j> import oracle.pg.imports.*
opg4j> var importer = new GraphImporter.Builder().
...>     setFilePath("<path_to_graphson_file>").
...>     setBatchSize(2).
...>     setInputFormat(GraphImportInputFormat.GRAPHSON).
...>     setOutputFormat(GraphImportOutputFormat.PG_VIEW).
...>     setThreads(4).
...>     setDbJdbcUrl("<jdbc_url>").
...>     setDbUsername("<username>").
...>     setDbPassword("<password>").
...>     setGraphName("mygraph").
...>     build()
importer ==> oracle.pg.imports.GraphImporter@5d957cf0
opg4j> var ddl = importer.importGraph()
import oracle.pg.imports.*;
GraphImporter importer = new GraphImporter.Builder()
     .setFilePath("<path_to_graphson_file>")
     .setBatchSize(2)
     .setInputFormat(GraphImportInputFormat.GRAPHSON)
     .setOutputFormat(GraphImportOutputFormat.PG_VIEW)
     .setThreads(4)
     .setDbJdbcUrl("<jdbc_url>")
     .setDbUsername("<username>")
     .setDbPassword("<password>")
     .setGraphName("mygraph")
     .build();
>>> from opg4py.graph_importer import GraphImporter
>>> config = {
...       'jdbc_url'     : '<jdbc_url>',
...       'username'     : '<username>',
...       'password'     : '<password>',
...       'file_path'    : '<path_to_graphson_file>',
...       'graph_name'   : 'mygraph',
...       'output_format': 'pg_view',
...       'input_format' : 'graphson'
... }
>>> importer = GraphImporter(config)
>>> importer.import_graph()

The preceding example sets up the required SQL tables in the database, generates and runs the DDL statement to create mygraph. For instance, this example generates the following CREATE PROPERTY GRAPH DDL statement:

"CREATE PROPERTY GRAPH mygraph 
           VERTEX TABLES ( 
             software 
               KEY (id) 
               LABEL software 
               PROPERTIES ARE ALL COLUMNS, 
             person 
               KEY (id) 
               LABEL person 
               PROPERTIES ARE ALL COLUMNS 
           ) 
           EDGE TABLES ( 
             created 
               KEY (id) 
               SOURCE KEY (sid) REFERENCES person (id) 
               DESTINATION KEY (did) REFERENCES software (id) 
               LABEL created 
               PROPERTIES ARE ALL COLUMNS, 
             knows 
               KEY (id) 
               SOURCE KEY (sid) REFERENCES person (id) 
               DESTINATION KEY (did) REFERENCES person (id) 
               LABEL knows 
               PROPERTIES ARE ALL COLUMNS 
           ) OPTIONS ( pg_view )"

Alternatively, you can also create a connection to the database by using a data source to connect to the database as shown in the following example:

opg4j> import oracle.pg.imports.*
opg4j> import oracle.jdbc.pool.OracleDataSource

opg4j> var ds = new OracleDataSource() // setup the data source
ds ==> oracle.jdbc.pool.OracleDataSource@4154ecd3
ds.setURL("<jdbc_url>")
ds.setUser("<username>")
ds.setPassword("<password>")

opg4j> var importer = new GraphImporter.Builder().
...>     setFilePath("<path_to_graphson_file>").
...>     setBatchSize(2).
...>     setInputFormat(GraphImportInputFormat.GRAPHSON).
...>     setOutputFormat(GraphImportOutputFormat.PG_VIEW).
...>     setThreads(4).
...>     setDataSource(ds).
...>     setGraphName("mygraph").
...>     build()
importer ==> oracle.pg.imports.GraphImporter@5d957cf0
opg4j> var ddl = importer.importGraph()
import oracle.pg.imports.*;
import oracle.jdbc.pool.OracleDataSource;
//Setup the datasource
var ds = new OracleDataSource();
ds.setURL(<jdbc_url>)
ds.setUser(<username>);
ds.setPassword(<password>);
//Setup the GraphImporter
GraphImporter importer = new GraphImporter.Builder()
     .setFilePath("<path_to_graphson_file>")
     .setBatchSize(2)
     .setInputFormat(GraphImportInputFormat.GRAPHSON)
     .setOutputFormat(GraphImportOutputFormat.PG_VIEW)
     .setThreads(4)
     .setDataSource(ds)
     .setGraphName("mygraph")
     .build();
var ddl = importer.importGraph();

Also, note the following:

  • The GraphImporterBuilder API supports GraphSON file format version 3.0 only.
  • Only GraphSON data types listed in Table 7-6 are supported.

The following sections provide more details on the GraphImporter parameters and the data type mapping between GraphSON and Oracle Database.