9.2 Creating a PGQL Property Graph By Importing a GraphSON file

Using the GraphImporterBuilder API, you can create a PGQL property graph 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 PGQL property graph 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_PGQL).
...>     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_PGQL)
     .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_pgql',
...       '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_PGQL )"

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_PGQL).
...>     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_PGQL)
     .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 9-7 are supported.

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

9.2.1 Additional Information on the GraphImporter Parameters

Learn more about the parameters used by the GraphImporter.

Table 9-3 Database Connection Parameters

Parameter Description Setter in API Default Value Optional
dataSource Data source for the database setDataSource NULL Only if passing dbJdbcUrl, dbUsername and dbPassword
dbJdbcUrl JDBC url of the database setDbJdbcUrl "" Only if passing a dataSouce
dbPassword Database password setDbPassword "" Only if passing a dataSouce
dbUsername Database user name setDbUsername "" Only if passing a dataSouce

Table 9-4 GraphImporter Configuration Parameters

Parameter Description Setter in API Default Value Optional
pathName Path to the GraphSON file setPathname "" No
graphName Resulting graph name setGraphName "" Yes
inFormat Input format for the importer setInputFormat GraphImportInputFormat.GRAPHSON Yes
outFormat Output format for the importer setOutputFormat GraphImportOutputFormat.PG_PGQL Yes
batchSize Number of rows read before inserting data to the database setBatchSize 1000 Yes
threads Number of threads to be used to insert to the database setThreads 1 Yes

Table 9-5 SQL Storage Parameters

Parameter Description Setter in API Default Value Optional
stringFieldSize GraphSON String data type is translated as VARCHAR2 in the database.

This parameter represents the VARCHAR2 size for the data storage.

setStringFieldsSize 100 Yes
fractionalSecondsPrecision The fractional seconds precision parameter found in TIMESTAMP data type in the Oracle Database. setFractionalSecondsPrecision 6 Yes

Table 9-6 PGQL Supported Parameters

Parameter Description Setter in API Default Value Optional
parallel Degree of parallelism to use for query and update operations setPathname 0 Yes
dynamicSampling Dynamic sampling value setGraphName 2 Yes
matchOptions Additional options used to influence query translation and execution setMatchOptions NULL Yes
options Additional options used to influence modify translation and execution setOptions NULL Yes

9.2.2 Mapping GraphSON Types to Oracle Database Data Types

The GraphSON data types can be mapped to their corresponding Oracle Database data types.

The following table shows GraphSON data types mapping to Oracle Database data types:

Table 9-7 Mapping GraphSON Types to Oracle Database Types

GraphSON Type Oracle Database Type
String VARCHAR2Foot 1
g:Int32 NUMBER(10)
g:Int64 NUMBER(10)
g:Float FLOAT
g:Double FLOAT
g:Date DATE
g:Timestamp TIMESTAMPFoot 2
g:UUID CHAR(36)

Footnote 1 You can use the stringFieldSize parameter to determine the string size for the database to store on the String columns.

Footnote 2 You can use the fractionalSecondsPrecision parameter to specify the precision on the columns of type Timestamp.