5.9.6 Oracleデータベース表からOracle定義のプロパティ・グラフ・フラット・ファイルへの変換

グラフの頂点とエッジを表すOracleデータベース表を、Oracle定義のフラット・ファイル形式(ファイルの拡張子が.opvおよび.ope)に変換します

Oracleデータベース表に格納されたグラフ・データがある場合、Java APIメソッドを使用して、そのデータをフラット・ファイルに変換し、後でプロパティ・グラフとしてOracleデータベースへロードできます。これにより、既存のOracleデータベース表からフラット・ファイルを生成するための、その他の手動アプローチを使用する必要性がなくなります。

グラフの頂点を格納している表の.opvファイルへの変換

エンティティ(グラフの頂点として表すことができる)を含むOracleデータベース表を、.opv形式のプロパティ・グラフのフラット・ファイルへ変換できます。

たとえば、次のリレーショナル表があるとします。EmployeeTab (empID integer not null, hasName varchar(255), hasAge integer, hasSalary number)

この表には次のデータがあるとします。

101, Jean, 20, 120.0
102, Mary, 21, 50.0
103, Jack, 22, 110.0
……

各従業員はグラフで頂点として表示できます。頂点IDは、employeeIDまたはハッシュなどのヒューリスティックを使用して生成されたIDの値である可能性があります。列hasName、hasAge、およびhasSalaryは属性として表示することができます。

JavaメソッドOraclePropertyGraphUtils.convertRDBMSTable2OPVおよびそのJavadoc情報は次のとおりです。

/**
* conn: is an connect instance to the Oracle relational database
* rdbmsTableName: name of the RDBMS table to be converted
* vidColName is the name of an column in RDBMS table to be treated as vertex ID
* lVIDOffset is the offset will be applied to the vertex ID
* ctams defines how to map columns in the RDBMS table to the attributes
* dop degree of parallelism
* dcl an instance of DataConverterListener to report the progress and control the behavior when errors happen 
*/
OraclePropertyGraphUtils.convertRDBMSTable2OPV(
       Connection conn, 
       String rdbmsTableName, 
       String vidColName, 
       long lVIDOffset, 
       ColumnToAttrMapping[] ctams, 
       int dop, 
       OutputStream opvOS, 
       DataConverterListener dcl);

次のコード・スニペットは、この表をOracle定義の頂点ファイル(.opv)に変換します。

// location of the output file
String opv = "./EmployeeTab.opv"; 
OutputStream opvOS = new FileOutputStream(opv);
// an array of ColumnToAttrMapping objects; each object defines how to map a column in the RDBMS table to an attribute of the vertex in an Oracle Property Graph.
ColumnToAttrMapping[] ctams = new ColumnToAttrMapping[3];
// map column "hasName" to attribute "name" of type String
ctams[0] = ColumnToAttrMapping.getInstance("hasName", "name", String.class);
// map column "hasAge" to attribute "age" of type Integer
ctams[1] = ColumnToAttrMapping.getInstance("hasAge", "age", Integer.class);
// map column "hasSalary" to attribute "salary" of type Double
ctams[2] = ColumnToAttrMapping.getInstance("hasSalary", "salary",Double.class);
// convert RDBMS table "EmployeeTab" into opv file "./EmployeeTab.opv", column "empID" is the vertex ID column, offset 1000l will be applied to vertex ID, use ctams to map RDBMS columns to attributes, set DOP to 8
OraclePropertyGraphUtils.convertRDBMSTable2OPV(conn, "EmployeeTab", "empID", 1000l, ctams, 8, opvOS, (DataConverterListener) null);

ノート:

オフセット値1000lの最後の文字である小文字の"l"は、その前の値が長整数であることを示します。

変換結果は次のようになります。

1101,name,1,Jean,,
1101,age,2,,20,
1101,salary,4,,120.0,
1102,name,1,Mary,,
1102,age,2,,21,
1102,salary,4,,50.0,
1103,name,1,Jack,,
1103,age,2,,22,
1103,salary,4,,110.0,

この場合、表EmployeeTabの各行は、3つの属性を持つ1つの頂点に変換されます。たとえば、データ"101, Jean, 20, 120.0"は、IDが1101で属性がname/"Jean", age/20, salary/120.0である頂点に変換されます。オフセット1000lが適用されるため、元のempID 101と頂点ID 1101の間にはオフセットがあります。オフセットはグラフ要素のID値の衝突を避けるのに有用です。

グラフのエッジを格納している表の.opeファイルへの変換

エンティティの関係(グラフのエッジとして表すことができる)を含むOracleデータベース表を、.ope形式のプロパティ・グラフのフラット・ファイルへ変換できます。

たとえば、次のリレーショナル表があるとします。EmpRelationTab (relationID integer not null, source integer not null, destination integer not null, relationType varchar(255), startDate date)

この表には次のデータがあるとします。

90001, 101, 102, manage, 10-May-2015
90002, 101, 103, manage, 11-Jan-2015
90003, 102, 103, colleague, 11-Jan-2015
……

各関係(行)はグラフでエッジとして表示できます。特に、エッジIDは、relationIDまたはハッシュなどのヒューリスティックを使用して生成されたIDと同じである可能性があります。列relationTypeはエッジ・ラベルの定義に使用でき、列startDateはエッジ属性として扱うことができます。

JavaメソッドOraclePropertyGraphUtils.convertRDBMSTable2OPEおよびそのJavadoc情報は次のとおりです。

/**
* conn: is an connect instance to the Oracle relational database
* rdbmsTableName: name of the RDBMS table to be converted
* eidColName is the name of an column in RDBMS table to be treated as edge ID
* lEIDOffset is the offset will be applied to the edge ID
* svidColName is the name of an column in RDBMS table to be treated as source vertex ID of the edge
* dvidColName is the name of an column in RDBMS table to be treated as destination vertex ID of the edge
* lVIDOffset is the offset will be applied to the vertex ID
* bHasEdgeLabelCol a Boolean flag represents if the given RDBMS table has a column for edge labels; if true, use value of column elColName as the edge label; otherwise, use the constant string elColName as the edge label
* elColName is the name of an column in RDBMS table to be treated as edge labels
* ctams defines how to map columns in the RDBMS table to the attributes
* dop degree of parallelism
* dcl an instance of DataConverterListener to report the progress and control the behavior when errors happen 
*/
OraclePropertyGraphUtils.convertRDBMSTable2OPE(
        Connection conn, 
        String rdbmsTableName, 
        String eidColName, 
        long lEIDOffset, 
        String svidColName, 
        String dvidColName, 
        long lVIDOffset, 
        boolean bHasEdgeLabelCol, 
        String elColName, 
        ColumnToAttrMapping[] ctams, 
        int dop, 
        OutputStream opeOS, 
        DataConverterListener dcl);

次のコード・スニペットは、この表をOracle定義のエッジ・ファイル(.ope)に変換します。

// location of the output file
String ope = "./EmpRelationTab.ope"; 
OutputStream opeOS = new FileOutputStream(ope);
// an array of ColumnToAttrMapping objects; each object defines how to map a column in the RDBMS table to an attribute of the edge in an Oracle Property Graph.
ColumnToAttrMapping[] ctams = new ColumnToAttrMapping[1];
// map column "startDate" to attribute "since" of type Date
ctams[0] = ColumnToAttrMapping.getInstance(“startDate", “since",Date.class);
// convert RDBMS table “EmpRelationTab" into ope file “./EmpRelationTab.opv", column “relationID" is the edge ID column, offset 10000l will be applied to edge ID, the source and destination vertices of the edge are defined by columns “source" and “destination", offset 1000l will be applied to vertex ID, the RDBMS table has an column “relationType" to be treated as edge labels, use ctams to map RDBMS columns to edge attributes, set DOP to 8
OraclePropertyGraphUtils.convertRDBMSTable2OPE(conn, “EmpRelationTab", “relationID", 10000l, “source", “destination", 1000l, true, “relationType", ctams, 8, opeOS, (DataConverterListener) null);

ノート:

オフセット値10000lの最後の文字である小文字の"l"は、その前の値が長整数であることを示します。

変換結果は次のようになります。

100001,1101,1102,manage,since,5,,,2015-05-10T00:00:00.000-07:00
100002,1101,1103,manage,since,5,,,2015-01-11T00:00:00.000-07:00
100003,1102,1103,colleague,since,5,,,2015-01-11T00:00:00.000-07:00

この場合、表EmpRelationTabの各行は、属性sinceを持つ個別のエッジに変換されます。たとえば、データ"90001, 101, 102, manage, 10-May-2015"は、IDが100001で頂点1101から頂点1102にリンクしているエッジに変換されます。このエッジは属性since/"2015-05-10T00:00:00.000-07:00"を持ちます。オフセット10000lが適用されるため、元のrelationID "90001"とエッジID "100001"の間にはオフセットがあります。同様に、オフセット1000lが出力および入力頂点IDに適用されます。