exit the initilization groovy if graph exist

46 views
Skip to first unread message

Yingjie Li

unread,
Dec 8, 2022, 6:40:28 AM12/8/22
to Gremlin-users
Hello, 

I am using Janusgraph. 

In my graph initialization groovy script, I would like to do a simple check, if the graph already exist, exist the script, else continue with the rest of the initialization.

when I run below code,  it complains that 
groovysh_parse: 1: unexpected token: else @ line 1, column 1.
   else {

Also, is there a command equivalent to System.exit(0)? the return command does not work.



//Create a sessioned connection to the Gremlin Server
:remote connect tinkerpop.server conf/remote.yaml session
:remote console

//define graph name
graph_name = "graph1"

//check whether graph exists

if (ConfiguredGraphFactory.getGraphNames().contains("graph_name")) {
println "graph already exists"
return
}

else {
map = new HashMap();
map.put("storage.backend", "cql");
map.put("storage.hostname", "127.0.0.1")
map.put("index.search.backend", "elasticsearch")
map.put("index.search.hostname", "127.0.0.1")
....
map.put("graph.graphname", graph_name)
ConfiguredGraphFactory.createConfiguration(new MapConfiguration(map))
....

graph = ConfiguredGraphFactory.open(graph_name)
//Creat graph schema
//Close all transactions
graph.getOpenTransactions().forEach { tx -> tx.rollback() }

mgmt = graph.openManagement()
mgmt.getOpenInstances().forEach {
if (it.reverse().take(1) != ")") {
mgmt.forceCloseInstance(it)
}
}
....
pk_id = mgmt.makePropertyKey('id').dataType(String.class).cardinality(org.janusgraph.core.Cardinality.LIST).make()
pk_name = mgmt.makePropertyKey('name').dataType(String.class).cardinality(org.janusgraph.core.Cardinality.LIST).make()
....
mgmt.commit()
ConfiguredGraphFactory.close(graph_name)
}

Thanks
Yingjie

Stephen Mallette

unread,
Dec 8, 2022, 1:51:06 PM12/8/22
to gremli...@googlegroups.com
I'm not sure if your post just got mangled or not, but you have to be wary of spaces when you write Gremlin Console initialization scripts because they get evaluated line by line just as you would if you were typing them in. so if you do:

if(1==1) {
  println "1"
}
else {
  println "2"
}

the groovy evaluator won't know that there is anything after that first closing brace and will start a new evaluation with "else {" which is invalid syntax. you would want to make sure you spacing was like:

if(1==1) {
  println "1"
} else {
  println "2"
}

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/bb87ea79-2d53-4408-ab04-ef2903a3d437n%40googlegroups.com.

Yingjie Li

unread,
Dec 8, 2022, 4:33:43 PM12/8/22
to gremli...@googlegroups.com
I see. Got it. Thanks.  So to exit the script, is there anything like System.exit()?  

Stephen Mallette

unread,
Dec 8, 2022, 5:00:11 PM12/8/22
to gremli...@googlegroups.com
i think you can just do a normal :exit command?

Yingjie Li

unread,
Dec 9, 2022, 6:42:56 AM12/9/22
to gremli...@googlegroups.com
That's right.  Should think it the same way as through the gremlin command.

Thanks Stephen.

Best regards
Yingjie

Reply all
Reply to author
Forward
0 new messages