X

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

A Robust and Easy Way to Create Flat Files (.opv/.ope) using Python on BDSG

Alan Wu
Architect

This is a sibling blog for a very similar one posted a few days ago. The only difference is the example code snippets shown blown are using Python and JPype.

Basically, a user's question made me realize it is indeed tricky for a user to manually create records in the flat file format (.opv/.ope). Fortunately, the product offers two utility APIs to help a user to manually and incrementally create .opv/.ope. Note that, if you have a CSV or a relational data source, then there are utility APIs to convert the whole CSV or tables (views) into flat files in one shot. There is no need to do the graph generation incrementally.  Below, I am showing some code snippets in Python. Specifically, the snippets are calling Java APIs via JPype.

...

util=JClass("oracle.pg.common.OraclePropertyGraphUtilsBase")
osv=JClass('java.io.FileOutputStream')("/tmp/g.opv")
ose=JClass('java.io.FileOutputStream')("/tmp/g.ope")

vid1 = JClass("java.lang.Long")(1)
util.outputVertexRecord(osv, vid1, "name", "Jon");
util.outputVertexRecord(osv, vid1, "salary", 18000.5);
util.outputVertexRecord(osv, vid1, "age", 18);

vid2 = JClass("java.lang.Long")(2)
util.outputVertexRecord(osv, vid2, "name", "Mary");
util.outputVertexRecord(osv, vid2, "married", True);
osv.flush()

sdf=JClass('java.text.SimpleDateFormat')("mm/dd/yyyy")
eid1=JClass("java.lang.Long")(100)
util.outputEdgeRecord(ose, eid1, vid1, vid2, "friend_of", "started_on", sdf.parse("01/02/2004"));
ose.flush()

​...
 

Now, let's check the output. As you can see, all the commas, ID of data types, property values are all handled properly.

$ cat /tmp/g.opv
1,name,1,Jon,,
1,salary,4,,18000.5,
1,age,2,,18,

$ cat /tmp/g.ope
100,1,2,friend_of,started_on,5,,,2004-01-02T00:01:00.000-05:00

Cheers,

Zhe Wu

 

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.