X

Oracle Big Data Spatial and Graph - technical tips, best practices, and news from the product team

  • October 13, 2017

3 Ways to Serialize and Write a Sub-Graph to Client Side (Part II)

Alan Wu
Architect

This is the second installment of the "3 Ways to Serialize and Write a Sub-Graph to Client Side" series. The first installment showed an approach which serializes a sub graph on the server side and copies the graph data files to the client side. Today, I am going to talk about a different approach that does not require access to the server side OS or filesystem.

  • Approach #2 Read a Sub-Graph to the Client Side and Serialize It

Similar to the first approach, I am using a Groovy script. Important APIs are highlighted.

cd /opt/oracle/oracle-spatial-graph/property_graph/dal/groovy/
sh ./gremlin-opg-nosql.sh

pgxServer="http://127.0.0.1:7007"
pgxSession=Pgx.getInstance(pgxServer).createSession("session1");

server = new ArrayList<String>();
server.add("bigdatalite:5000");
cfg = GraphConfigBuilder.forPropertyGraphNosql().setCreateEdgeIdMapping(true).setName("connections").setStoreName("kvstore") .setHosts(server) .hasEdgeLabel(true).setLoadEdgeLabel(true) .addEdgeProperty("weight", PropertyType.DOUBLE, "1000000") .setMaxNumConnections(2).build();

opg = OraclePropertyGraph.getInstance(cfg);
pgxGraph = session.readGraphWithProperties(opg.getConfig());
==>PgxGraph[name=connections,N=78,E=164,created=1507628356244]

ef = new EdgeFilter("edge.weight < 5.0");

subGraph = pgxGraph.filter(ef);
==>PgxGraph[name=connections_sub-graph_1,N=69,E=118,created=1507628611702]

Now subGraph, an in-memory sub graph, is ready. The following code snippet gets, from the sub graph, a set of vertices  to the client side and uses an utility API OraclePropertyGraphUtilsBase.outputVertexRecord to write the vertices into a .opv file. Note that this utility API takes care of escaping and encoding so you don't have to worry about special characters including comma, new line, quotes etc.

vertexSet = subGraph.getVertices(); vertexIter = vertexSet.iterator(); i=1
osVertex = new FileOutputStream("/tmp/mysubgraph.opv");

while (vertexIter.hasNext())  {
  v = vertexIter.next();
  OraclePropertyGraphUtilsBase.outputVertexRecord(osVertex, v.getId(), null, null);
}

osVertex.flush();

osVertex.close();

Last step, issue a simple PGQL query about edges, get back a result set, and write out the edges using OraclePropertyGraphUtilsBase.outputEdgeRecord into a .ope file.

osEdge   = new FileOutputStream("/tmp/mysubgraph.ope");

pgxResultSet = subGraph.queryPgql("SELECT n.id(), e.label(), m.id(), e.id() WHERE (n) -[e]-> (m)")
resultsIter = pgxResultSet.getResults().iterator(); i=1

while (resultsIter.hasNext()) {
  r = resultsIter.next()
  OraclePropertyGraphUtilsBase.outputEdgeRecord(osEdge, r.get(3), r.get(0), r.get(2), r.get(1), null, null);
}

osEdge.flush()
osEdge.close()

That is it. If the above steps complete successfully, you will see the following two files on the client side.

[root@hqgraph1 Downloads]# head -5 /tmp/mysubgraph.opv
2,%20,,,,
4,%20,,,,
11,%20,,,,
16,%20,,,,
18,%20,,,,


[root@hqgraph1 Downloads]# head -3 /tmp/mysubgraph.ope
1081,38,2,admires,%20,,,,
1000,1,2,collaborates,%20,,,,
1084,36,2,collaborates,%20,,,,
 

Cheers,

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha