exit the initilization groovy if graph exist

47 visningar
Hoppa till det första olästa meddelandet

Yingjie Li

oläst,
8 dec. 2022 06:40:282022-12-08
till 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

oläst,
8 dec. 2022 13:51:062022-12-08
till 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

oläst,
8 dec. 2022 16:33:432022-12-08
till gremli...@googlegroups.com
I see. Got it. Thanks.  So to exit the script, is there anything like System.exit()?  

Stephen Mallette

oläst,
8 dec. 2022 17:00:112022-12-08
till gremli...@googlegroups.com
i think you can just do a normal :exit command?

Yingjie Li

oläst,
9 dec. 2022 06:42:562022-12-09
till gremli...@googlegroups.com
That's right.  Should think it the same way as through the gremlin command.

Thanks Stephen.

Best regards
Yingjie

Svara alla
Svara författaren
Vidarebefordra
0 nya meddelanden