4.2.5 データベースからのデータの読取り
ログインすると、接続情報をグラフ構成に指定しなくてもデータベースからグラフ・サーバーにデータを読み込むことができるようになりました。
データベース・ユーザーが存在し、データベースのグラフ・データに対する読取りアクセス権を持っている必要があります。
たとえば、次のグラフ構成では、hr
という名前のプロパティ・グラフがメモリーに読み込まれ、データベースで<database user>/<database password>
として認証されます。
GraphConfig config = GraphConfigBuilder.forPropertyGraphRdbms()
.setName("hr")
.addVertexProperty("FIRST_NAME", PropertyType.STRING)
.addVertexProperty("LAST_NAME", PropertyType.STRING)
.addVertexProperty("EMAIL", PropertyType.STRING)
.addVertexProperty("CITY", PropertyType.STRING)
.setPartitionWhileLoading(PartitionWhileLoading.BY_LABEL)
.setLoadVertexLabels(true)
.setLoadEdgeLabel(true)
.build();
PgxGraph hr = session.readGraphWithProperties(config);
次の例は、接続情報を構成ファイル自体に指定せずにリレーショナル表からグラフ・サーバーに読み込むJSON形式のグラフ構成です。
{
"name":"hr",
"vertex_id_strategy":"no_ids",
"vertex_providers":[
{
"name":"Employees",
"format":"rdbms",
"database_table_name":"EMPLOYEES",
"key_column":"EMPLOYEE_ID",
"key_type":"string",
"props":[
{
"name":"FIRST_NAME",
"type":"string"
},
{
"name":"LAST_NAME",
"type":"string"
}
]
},
{
"name":"Departments",
"format":"rdbms",
"database_table_name":"DEPARTMENTS",
"key_column":"DEPARTMENT_ID",
"key_type":"string",
"props":[
{
"name":"DEPARTMENT_NAME",
"type":"string"
}
]
}
],
"edge_providers":[
{
"name":"WorksFor",
"format":"rdbms",
"database_table_name":"EMPLOYEES",
"key_column":"EMPLOYEE_ID",
"source_column":"EMPLOYEE_ID",
"destination_column":"EMPLOYEE_ID",
"source_vertex_provider":"Employees",
"destination_vertex_provider":"Employees"
},
{
"name":"WorksAs",
"format":"rdbms",
"database_table_name":"EMPLOYEES",
"key_column":"EMPLOYEE_ID",
"source_column":"EMPLOYEE_ID",
"destination_column":"JOB_ID",
"source_vertex_provider":"Employees",
"destination_vertex_provider":"Jobs"
}
]
}
データベースからグラフ・サーバーにデータを読み込む方法の詳細は、キーストアへのデータベース・パスワードの格納を参照してください。
親トピック: ユーザーの認証および認可