X

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

Property Graph (v2.2) in a Box

Charles Wang
Senior Development Manager

To start, download Oracle Big Data Lite Virtual Machine v4.9 from the following page.
http://www.oracle.com/technetwork/database/bigdata-appliance/oracle-bigdatalite-2104726.html
It is recommended to use Oracle VM Virtual Box 5.0.16 or up to import this virtual machine. Once import is done successfully, login as oracle (default password is welcome1). On the desktop, there is a "Start/Stop Services" icon - double clicking that will lead to a console popup with a list of services. Check Oracle NoSQL Database, hit enter, and the built-in Oracle NoSQL Database will start automatically. If you need to shutdown the Oracle NoSQL Database, just repeat this process. 
Next, I am going to show you a simple Groovy based script that loads a sample property graph representing a small social network, reads out vertices and edges, writes it out, and finally counts the number of triangles and run a simple pgql in this network.
Open a Linux terminal in this virtual machine and type in the following:
$ cd /opt/oracle/oracle-spatial-graph/property_graph/dal/groovy
$ ./gremlin-opg-nosql.sh
--------------------------------
opg-nosql>
//
// Connect to the Oracle NoSQL Database in this virtual box
//
    server = new ArrayList<String>();
    server.add("bigdatalite:5000");
    cfg = GraphConfigBuilder.forPropertyGraphNosql()             \
      .setName("connections").setStoreName("kvstore")                \
      .setHosts(server)                                          \
      .addEdgeProperty("weight", PropertyType.DOUBLE, "1000000") \
      .addVertexProperty("company", PropertyType.STRING, "NULL") \
      .setMaxNumConnections(2).build();
// Get an instance of OraclePropertyGraph
//
opg = OraclePropertyGraph.getInstance(cfg);
opg.clearRepository();
//
// Use the parallel data loader API to load a
// sample property graph in flat file formatwith a
// degree of parallelism (DOP) 2
//
vfile="../../data/connections.opv"
efile="../../data/connections.ope"
opgdl=OraclePropertyGraphDataLoader.getInstance();
opgdl.loadData(opg, vfile, efile, 2);

opg-nosql> opg.countVertices()
==>78
opg-nosql> opg.countEdges()
==>164

// read through the vertices

opg-nosql> opg.getVertices();
==>Vertex ID 21 {country:str:United States, name:str:Hillary Clinton, occupation:str:67th United States Secretary of State, political party:str:Democratic, religion:str:Methodism, role:str:political authority}
==>Vertex ID 25 {country:str:Russia, name:str:Vladimir Putin, occupation:str:president of Russia, political party:str:Comunist party of the Soviet Union, role:str:political authority}
... ...
// read through the edges
opg-nosql> opg.getEdges();
==>Edge ID 1051 from Vertex ID 20 {company:str:Farallon Capital Management, country:str:United States, name:str:Tom Steyer, occupation:str:founder, political party:str:Democratic, role:str:philantropist} =[collaborates]=> Vertex ID 1 {country:str:United States, name:str:Barack Obama, occupation:str:44th president of United States of America, political party:str:Democratic, religion:str:Christianity, role:str:political authority} edgeKV[{weight:flo:1.0}]
==>Edge ID 1054 from Vertex ID 19 {country:str:United States, name:str:Kirsten Gillibrand, occupation:str:junior United States Senator from New York, political party:str:Democratic, religion:str:Methodism, role:str:political authority} =[admires]=> Vertex ID 31 {country:str:United States, name:str:Jason Collins, occupation:str:basketball center player, role:str:professional basketball player, team:str:Brooklyn nets} edgeKV[{weight:flo:1.0}]
... ...

//
// You can add vertices/edges, change properties etc. here.

a = opg.addVertex(1l);
a.setProperty("company", "oracle");
a.setProperty("age", 31);
b = opg.addVertex(2l);
b.setProperty("company", "oracle");
b.setProperty("age", 28);
// Add edges e1,
e1 = opg.addEdge(1l, a, b, "knows");
e1.setProperty("type", "partners");

// Serialize the graph out into a pair of flat files with DOP=2
//
vOutput="/tmp/mygraph.opv"
eOutput="/tmp/mygraph.ope"
OraclePropertyGraphUtils.exportFlatFiles(opg, vOutput, eOutput, 2, false);

//start the pgx services
   $ cd /opt/oracle/oracle-spatial-graph/property_graph/pgx/bin
   $ sh ./start-server 

// Create an analyst instance
//
session=Pgx.createSession("session_ID_1");
analyst=session.createAnalyst();
// Read graph data from database into memory
pgxGraph = session.readGraphWithProperties(opg.getConfig(), true);
//call the count triangle api
analyst.countTriangles(pgxGraph, true);
==>22

// run a pgql
opg-nosql> pgxResultSet = pgxGraph.queryPgql("SELECT n.company,m WHERE (n WITH company='Koch Industries')->(m)->(n), n!=m")
==>PgqlResultSetImpl[graph=connections,numResults=4]
opg-nosql> pgxResultSet.print(4);
+-------------------------------------+
| n.company         | m               |
+-------------------------------------+
| "Koch Industries" | PgxVertex[ID=1] |
| "Koch Industries" | PgxVertex[ID=9] |
| "Koch Industries" | PgxVertex[ID=1] |
| "Koch Industries" | PgxVertex[ID=8] |
+-------------------------------------+
==>null

// get the total number of the records from the result set
pgxResultSet.getNumResults()    
==>132
Note that the same virtual machine has Apache HBase installed as well as Oracle NoSQL Database.Once Apache HBase is configured and started, the same above script (except the DB connection initialization part) can be used without a change.

//HBase DB connection initialization configuration
cfg = GraphConfigBuilder.forPropertyGraphHbase()            \
 .setName("connections")                                    \
 .setZkQuorum("bigdatalite").setZkClientPort(2181)          \
 .setZkSessionTimeout(120000).setInitialEdgeNumRegions(3)   \
 .setInitialVertexNumRegions(3).setSplitsPerRegion(1)       \
 .addEdgeProperty("weight", PropertyType.DOUBLE, "1000000") \
 .addVertexProperty("company", PropertyType.STRING, "NULL") \
 .build();

 For more detailed deveoper level guide information, please visit our formal developer guide here(http://docs.oracle.com/bigdata/bda49/index.htm).

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.