Calling a custom function from the console?

86 views
Skip to first unread message

William

unread,
Sep 21, 2016, 4:16:21 PM9/21/16
to OrientDB
Hi,

I have a simple graph with date information on each node in which the vertices are:

     id,date
     A,2014-01-02
     B,2015-01-02
     C,2016-01-02
     D,2013-06-01
     E,2014-06-01
     F,2016-06-01

I'm trying to put together something that would let me generate edges between nodes that occur on some date to a node with the same date on the following year.  For the vertices above, I'd end up with:

     A -> B -> C
     D -> E
     F

I have put together a quick and dirty JS function to do this called createSameDayOfYearEdges().  I add it into the graph using a console call that executes a CREATE FUNCTION call.  If I fire up Studio after creating the graph from my scripts, its there and running the query "SELECT createSameDayOfYearEdges()" works properly... but if I try to run it using that SELECT from the console it does not work.  The error message I'm seeing says:


     orientdb {db=test-add_edge_by_date.orientdb}> SELECT createSameDayOfYearEdges()

     Error: com.orientechnologies.orient.core.command.script.OCommandScriptException: Error on parsing script at position #0: Error on execution of the script
     Script: createSameDayOfYearEdges
     ------^
    DB name="test-add_edge_by_date.orientdb"

     Error: javax.script.ScriptException: TypeError: orient.getGraph is not a function in <eval> at line number 2
     Error: <eval>:2 TypeError: orient.getGraph is not a function


This is a little confusing to me, it would appear that it's trying to execute the script but I'm getting an error...

Is it possible to get my function to run from the console, or do I need to approach something like this differently?

Thanks!
  -William
test-add_edge_by_date.zip

Ivan Mainetti

unread,
Sep 21, 2016, 4:43:06 PM9/21/16
to orient-...@googlegroups.com
Hi,
which version of Orientdb are you using? I'm trying on 2.2.10 and not getting that error.
Message has been deleted

William

unread,
Sep 21, 2016, 5:19:08 PM9/21/16
to OrientDB
Hi Ivan,

I'm running 2.2.10-community under OSX.  

I'm not running server when I try it... could that be part of the issue?

-William

Ivan Mainetti

unread,
Sep 21, 2016, 5:29:10 PM9/21/16
to OrientDB
that's definitely the issue! JS functions are server side functions so they are interpreted by the running server

William

unread,
Sep 21, 2016, 6:18:27 PM9/21/16
to OrientDB
I tried running my scripts to create the graph, and then with the server running, I tried connecting via the console and can run the function.  The console where I'm running the server prints out the messages showing that it created edges, but the edges aren't being actually added to my database...

OrientDB console v.2.2.10 (build 2.2.x@rc32d4ee06d4c38e27eb66931d027eb54854872ff; 2016-09-15 08:47:27+0000) www.orientdb.com
Type 'help' to display all the supported commands.
Installing extensions for GREMLIN language v.2.6.0

orientdb> CONNECT REMOTE:localhost/test-add_edge_by_date.orientdb admin admin

Connecting to database [REMOTE:localhost/test-add_edge_by_date.orientdb] with user 'admin'...OK
orientdb {db=test-add_edge_by_date.orientdb}> select from V

+----+-----+--------+-------------------+----+
|#   |@RID |@CLASS  |date               |id  |
+----+-----+--------+-------------------+----+
|0   |#25:0|myVertex|2014-01-02 00:00:00|A   |
|1   |#26:0|myVertex|2015-01-02 00:00:00|B   |
|2   |#27:0|myVertex|2016-01-02 00:00:00|C   |
|3   |#28:0|myVertex|2013-06-01 00:00:00|D   |
|4   |#29:0|myVertex|2014-06-01 00:00:00|E   |
|5   |#30:0|myVertex|2016-06-01 00:00:00|F   |
+----+-----+--------+-------------------+----+

6 item(s) found. Query executed in 0.004 sec(s).
orientdb {db=test-add_edge_by_date.orientdb}> select from E


0 item(s) found. Query executed in 0.002 sec(s). 
orientdb {db=test-add_edge_by_date.orientdb}> select createSameDayOfYearEdges()

+----+
|#   |
+----+
|0   |
+----+

1 item(s) found. Query executed in 0.031 sec(s).

This gets printed to the console:
2016-09-21 15:55:37:703 INFO  {db=test-add_edge_by_date.orientdb} Create edge from #25:0 to #26:0. [ScriptObjectMirror]
2016-09-21 15:55:37:708 INFO  {db=test-add_edge_by_date.orientdb} Create edge from #26:0 to #27:0. [ScriptObjectMirror]
2016-09-21 15:55:37:709 INFO  {db=test-add_edge_by_date.orientdb} Create edge from #28:0 to #29:0. [ScriptObjectMirror]

... but querying for edges still comes up empty:
 
orientdb {db=test-add_edge_by_date.orientdb}> select from E


0 item(s) found. Query executed in 0.001 sec(s).

If I run the same select createSameDayOfYearEdges() call from within Studio, I see the same messages on the console, but the edges actually get saved.  I can see this on the console if I query for edges again:

orientdb {db=test-add_edge_by_date.orientdb}> select from E

+----+-----+-------------+-----+-----+
|#   |@RID |@CLASS       |out  |in   |
+----+-----+-------------+-----+-----+
|0   |#33:0|sameDayOfYear|#28:0|#29:0|
|1   |#39:0|sameDayOfYear|#25:0|#26:0|
|2   |#40:0|sameDayOfYear|#26:0|#27:0|
+----+-----+-------------+-----+-----+

3 item(s) found. Query executed in 0.002 sec(s).

Soooo... any ideas why the edges aren't being saved when I run it from the console?

-William

alessand...@gmail.com

unread,
Sep 22, 2016, 11:05:33 AM9/22/16
to OrientDB
Hi,
in your function createSameDayOfYearEdges you must insert g.commit() and it works also from console.

Best regards,
Alessandro 

William

unread,
Sep 22, 2016, 11:36:51 AM9/22/16
to OrientDB
Ah!  

Thanks!
Reply all
Reply to author
Forward
0 new messages