This is my Groovy script in Netbeans:
import org.apache.tinkerpop.gremlin.tinkergraph.structure.*
import org.apache.tinkerpop.gremlin.groovy.loaders.*
GremlinLoader.load()
SugarLoader.load()
def graph = TinkerFactory.createModern()
def g = graph.traversal()
def results = []
g.V(1).outE('knows').inV().values('name').fill(results) //==>[vadas, josh] which is OK
println results
g.V(1).repeat(**out**()).times(2).fill(results) //==>Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: Test.**out**() is applicable for argument types: () values: []
println results
//also getting "Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: ..." for all the following ones:
g.V().hasLabel('person').out().has('name',within('vadas','josh')).fill(results)
g.V().values('age').is(lte(30)).fill(results)
g.V().group().by(bothE().count()).fill(results)
I get the "Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: ..." for "nested" steps (e.g. repeat(**out**()), has('xxx',**within**(...), is(**lte**(30)), by(**bothE**()), etc.) only. All plain steps (outE('xxx'),is(32), etc. ) are running well. All traversals are running fine in the Gremlin console.
I'm using the folowing java libraries in Netbeans:
commons-cli-1.2.jar
commons-codec-1.9.jar
commons-configuration-1.10.jar
commons-lang-2.6.jar
commons-lang3-3.3.1.jar
commons-logging-1.2.jar
gbench-0.4.3-groovy-2.4.jar
gprof-0.3.1-groovy-2.4.jar
gremlin-console-3.2.3.jar
gremlin-core-3.2.3.jar
gremlin-driver-3.2.3.jar
gremlin-groovy-3.2.3.jar
gremlin-shaded-3.2.3.jar
groovy-2.4.7-indy.jar
groovy-2.4.7.jar
groovy-ant-2.4.7.jar
groovy-console-2.4.7.jar
groovy-groovysh-2.4.7-indy.jar
groovy-json-2.4.7-indy.jar
groovy-jsr223-2.4.7-indy.jar
groovy-sql-2.4.7-indy.jar
groovy-swing-2.4.7.jar
groovy-templates-2.4.7.jar
groovy-xml-2.4.7.jar
hppc-0.7.1.jar
httpclient-4.5.1.jar
httpcore-4.4.3.jar
ivy-2.3.0.jar
jBCrypt-jbcrypt-0.4.jar
javatuples-1.2.jar
jcabi-log-0.14.jar
jcabi-manifests-1.1.jar
jcl-over-slf4j-1.7.21.jar
jline-2.12.jar
log4j-1.2.17.jar
netty-all-4.0.40.Final.jar
slf4j-api-1.7.21.jar
slf4j-log4j12-1.7.21.jar
snakeyaml-1.15.jar
tinkergraph-gremlin-3.2.3.jar
What am I doing wrong?
From the Apache TinkerPop docs on graph traversals:
To reduce the verbosity of the expression, it is good to import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*. This way, instead of doing __.inE() for an anonymous traversal, it is possible to simply write inE(). Be aware of language-specific reserved keywords when using anonymous traversals. For example, in and as are reserved keywords in Groovy, therefore you must use the verbose syntax __.in() and __.as() to avoid collisions.
Similarly for predicates, like lte() and within():
To reduce the verbosity of predicate expressions, it is good to import static org.apache.tinkerpop.gremlin.process.traversal.P.*.
Many thanks, it runs well with these imports:
import org.apache.tinkerpop.gremlin.tinkergraph.structure.*
import org.apache.tinkerpop.gremlin.groovy.loaders.*
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*
import static org.apache.tinkerpop.gremlin.process.traversal.P.*
import static org.apache.tinkerpop.gremlin.process.traversal.Order.*
GremlinLoader.load()
SugarLoader.load()
def graph = TinkerFactory.createModern()
def g = graph.traversal()
def results = []
def v4=g.V(4).next()
def v6=g.V(6).next()
v4.addEdge("knows", v6, "id" , 13, "weight", 0.4)
def subGraph = g.E().hasLabel('knows').subgraph('subGraph').cap('subGraph').next()
def sg = subGraph.traversal()
sg.E().fill(results)
println "sg.E() =>[e[7][1-knows->2], e[8][1-knows->4], e[12][4-knows->6]] ==>" + results
results = []
g.V(1).outE('knows').inV().values('name').fill(results)
println "g.V(1).outE('knows').inV().values('name') =>[vadas, josh] ==>" + results
results = []
g.V(4).outE('knows').inV().values('name').fill(results)
println "g.V(4).outE('knows').inV().values('name') =>[peter] ==>" + results
results = []
g.V(1).outE('knows').fill(results)
println "g.V(1).outE('knows') =>[e[7][1-knows->2], e[8][1-knows->4]] ==>" + results
results = []
g.V().has('name','marko').out('created').fill(results)
println "g.V(1).outE('knows') =>[v[3]] ==>" + results
results = []
g.V().has('name','marko').out('created').in('created').values('name').fill(results)
println "g.V().has('name','marko').out('created').in('created').values('name') =>[marko, josh, peter] ==>" + results
results = []
g.V().range(0, 2).fill(results)
println "g.V().range(0, 2) =>[v[1], v[2]] ==>" + results
results = []
g.V().values('name').order().fill(results)
println "g.V().values('name').order() =>[josh, lop, marko, peter, ripple, vadas] ==>" + results
results = []
g.V().values('name').order().tail().fill(results)
println "g.V().values('name').order().tail() =>[vadas] ==>" + results
results = []
g.V().hasLabel('person').fill(results)
println "g.V().hasLabel('person') =>[v[1], v[2], v[4], v[6]] ==>" + results
results = []
g.V().hasLabel('person').out().has('name','josh').fill(results) //
println "g.V().hasLabel('person').out().has('name','josh') =>[v[4]] ==>" + results
results = []
g.V().out('knows').map{it.get().value('name') + ' is the friend name'}.fill(results)
println "g.V().out('knows').map{it.get().value('name') + ' is the friend name'} =>[vadas is the friend name, josh is the friend name, peter is the friend name] ==>" + results
results = []
g.V().values('name').order().fill(results)
println "g.V().values('name').order() =>[josh, lop, marko, peter, ripple, vadas] ==>" + results
results = []
g.V().values('age').is(32).fill(results)
println "g.V().values('age').is(32) =>[32] ==>" + results
results = []
g.V().values('age').mean().fill(results)
println "g.V().values('age').mean() =>[30.75] ==>" + results
results = []
g.V(1).property('country','usa').fill(results)
println "g.V(1).property('country','usa') =>[v[1]] ==>" + results
results = []
g.V(1).out('created').aggregate('x').fill(results)
println "g.V(1).out('created').aggregate('x') =>[v[3]] ==>" + results
results = []
g.V().as('a').out('created').as('b').select('a','b').fill(results)
println "g.V().as('a').out('created').as('b').select('a','b') =>[[a:v[1], b:v[3]], [a:v[4], b:v[5]], [a:v[4], b:v[3]], [a:v[6], b:v[3]]] ==>" + results
results = []
g.V().hasLabel('person').count().fill(results)
println "g.V().hasLabel('person').count() =>[4] ==>" + results
results = []
g.V().coin(0.5).fill(results)
println "g.V().coin(0.5) =>some random vertices ==>" + results
results = []
g.withSack(1.0f).V().sack().fill(results)
println "g.withSack(1.0f).V().sack() =>[1.0, 1.0, 1.0, 1.0, 1.0, 1.0] ==>" + results
results = []
g.V().outE().sample(1).values('weight').fill(results)
println "g.V().outE().sample(1).values('weight') =>[0.5] ==>" + results
results = []
g.V().values('age').sum().fill(results)
println "g.V().values('age').sum() =>[123] ==>" + results
results = []
g.V().as('a').both().both().as('b').count().fill(results)
println "g.V().as('a').both().both().as('b').count() =>[40] ==>" + results
results = []
g.V().as('a').both().both().as('b').where('a',neq('b')).count().fill(results)
println "g.V().as('a').both().both().as('b').where('a',neq('b')) =>[26] ==>" + results
results = []
g.V().hasLabel('person').out().has('name',within('vadas','josh')).fill(results)
println "g.V().hasLabel('person').out().has('name',within('vadas','josh')) =>[v[2], v[4]] ==>" + results
results = []
g.V().values('age').is(lte(30)).fill(results)
println "g.V().values('age').is(lte(30)) =>[29, 27] ==>" + results
results = []
g.V().group().by(bothE().count()).fill(results)
println "g.V().group().by(bothE().count()) =>[[1:[v[2], v[5]], 2:[v[6]], 3:[v[1], v[3]], 4:[v[4]]]] ==>" + results
results = []
g.V(1).repeat(out()).times(2).fill(results)
println "g.V(1).repeat(out()).times(2) =>[v[5], v[3], v[6]] ==>" + results
results = []
g.V(1).repeat(out()).times(2).path().by('name').fill(results)
println "g.V(1).repeat(out()).times(2).path().by('name') =>[[marko, josh, ripple], [marko, josh, lop], [marko, josh, peter]] ==>" + results
results = []
g.V(1).repeat(out().simplePath()).until(hasId(6)).path().limit(1).fill(results)
println "g.V(1).repeat(out().simplePath()).until(hasId(6)).path().limit(1) =>[[v[1], v[4], v[6]]] ==>" + results
results = []
g.V().until(has('name','ripple')).repeat(out()).fill(results)
println "g.V().until(has('name','ripple')).repeat(out()) =>[v[5], v[5], v[5]] ==>" + results
results = []
g.V().until(has('name','ripple')).repeat(out()).path().by('name').fill(results)
println "g.V().until(has('name','ripple')).repeat(out()).path().by('name') =>[[marko, josh, ripple], [josh, ripple], [ripple]] ==>" + results
results = []
g.V(1).until(has('name','josh')).repeat(out('knows')).path().by('name').fill(results)
println "g.V(1).until(has('name','josh')).repeat(out('knows')).path().by('name') =>[[marko, josh]] ==>" + results
results = []
g.V(1).until(outE('knows').count().is(0)).repeat(out('knows')).path().by('name').fill(results)
println "g.V(1).until(outE('knows').count().is(0)).repeat(out('knows')).path().by('name') =>[[marko, vadas], [marko, josh, peter]] ==>" + results
results = []
g.V(2).optional(out('knows')).fill(results)
println "g.V(1).optional(out('knows')) =>[v[2]] ==>" + results
results = []
g.V().where(outE('created').and().outE('knows')).values('name').fill(results)
println "g.V().where(outE('created').and().outE('knows')).values('name') =>[marko, josh] ==>" + results
results = []
g.V().values('name').order().by(decr).fill(results)
println "g.V().values('name').order().by(decr) =>[vadas, ripple, peter, marko, lop, josh] ==>" + results
results = []
g.V().hasLabel('person').order().by(shuffle).fill(results)
println "g.V().hasLabel('person').order().by(shuffle) =>some random order of v[1], v[2], v[4], v[6] ==>" + results
results = []
Related
Not able to append object value. trying to transform json to expected output having all the user infomration.
Groovy Script:
import groovy.json.*;
def data='''{"totalcount":5,"employees":[{"name":"Sam","age":34},{"name":"Richard","age":38},{"name":"Harry","age":36},{"name":"Tom","age":40},{"name":"David","age":84},{"name":"Chris","age":52}],"Salaries":[{"name":"Sam","salary":34000},{"name":"Richard","salary":89889},{"name":"Harry","salary":36898},{"name":"David","salary":489889},{"name":"Chris","salary":84898},{"name":"Toms","salary":5298}]}'''
def test = new JsonSlurper().parseText(data)
def a=[:]
def OU=[:]
def status=[]
a.OUTPUT=OU
OU.STATUS=status
def b = [:]
for(def y=0;y<test.employees.size();y++) {
for (def j = 0; j < test.Salaries.size(); j++) {
if (test.employees[y].name ==test.Salaries[j].name ) {
b.name = test.employees[y].name
b.age = test.employees[y].age
b.salary = test.Salaries[j].salary
status<<b
j=test.Salaries.size()
}
}
}
String finalJson = JsonOutput.toJson a;
println JsonOutput.prettyPrint( finalJson)
Expected Result :
{"OUTPUT":{"STATUS":[{"name":"Sam","age":34,"salary":34000},{"name":"Richard","age":38,"salary":89889},{"name":"Harry","age":36,"salary":36898},{"name":"David","age":84,"salary":489889},{"name":"Chris","age":52,"salary":84898}]}}
You can simplify your code to:
import groovy.json.*
def data='{"totalcount":5,"employees":[{"name":"Sam","age":34},{"name":"Richard","age":38},{"name":"Harry","age":36},{"name":"Tom","age":40},{"name":"David","age":84},{"name":"Chris","age":52}],"Salaries":[{"name":"Sam","salary":34000},{"name":"Richard","salary":89889},{"name":"Harry","salary":36898},{"name":"David","salary":489889},{"name":"Chris","salary":84898},{"name":"Toms","salary":5298}]}'
def test = new JsonSlurper().parseText(data)
def joinedByName = (test.employees + test.Salaries).groupBy { it.name }
def result = [
OUTPUT: [
STATUS: [
joinedByName.collect { it.value.inject { a, b -> a + b } }
.findAll { it.salary && it.age } // Remove people who don't have both
]
]
]
String finalJson = JsonOutput.toJson result;
println JsonOutput.prettyPrint(finalJson)
Below is my groovy code where I am framing a command using json data, but json data have different types data like list in array, or only single variable so can any one please tell me how to find data type of a variable.
Below code I have high-lighted the one element "jsondata[key]" that is my values of keys, and I want to check the data type of this values like in my JSON data i have params(key) with 4 list of arrays(values) so before using params values i have check the data type.
my expected result:
key : [1 2 3]
if (typeof(key) == list) {
for (value in key) {
#here i want to frame a command using previous keys..
}
}
like typeof() in python
import groovy.json.JsonSlurper
def label = "test testname params"
File jsonFile = newFile("/home/developer/Desktop/kramdeni/vars/PARAMS.json")
def jsondata = new JsonSlurper().parse(jsonFile)
println jsondata.keySet()
println "jsondata: " + jsondata
def command = ""
keys = label.split(" ")
println "keys: " + keys
for (key in keys) {
command += "-" + key + " " + **jsondata[key]** + " "
}
println "command: " + command
my json data:
{
"test": "iTEST",
"testname": "BOV-VDSL-link-Rateprofile-CLI-Test-1",
"params": [
{
"n2x_variables/config_file": "C:/Program Files (x86)/Agilent/N2X/RouterTester900/UserData/config/7.30 EA SP1 Release/OSP Regression/BOV/Bov-data-1-single-rate-profile.xml"
},
{
"n2x_variables/port_list": "303/4 303/1"
},
{
"n2x_variables/port_list": "302/3 303/4"
},
{
"n2x_variables/port_list": "301/3 303/5"
}
]
}
jsondata.each{ entry->
println entry.value.getClass()
}
The following code illustrates instanceof for String and List types:
import groovy.json.JsonSlurper
def label = "test testname params"
def jsonFile = new File("PARAMS.json")
def jsondata = new JsonSlurper().parse(jsonFile)
def command = ""
def keys = label.split(" ")
for (key in keys) {
def value = jsondata[key]
if (value instanceof String) {
println "${key} ${value}"
} else if (value instanceof List) {
value.each { item ->
println "${key} contains ${item}"
}
} else {
println "WARN: unknown data type"
}
}
example output for the JSON specified (I'm not sure how you want to build the command, so this is simple output. It should be easy to build up command as desired):
$ groovy Example.groovy
test iTEST
testname BOV-VDSL-link-Rateprofile-CLI-Test-1
params contains [n2x_variables/config_file:C:/Program Files (x86)/Agilent/N2X/RouterTester900/UserData/config/7.30 EA SP1 Release/OSP Regression/BOV/Bov-data-1-single-rate-profile.xml]
params contains [n2x_variables/port_list:303/4 303/1]
params contains [n2x_variables/port_list:302/3 303/4]
params contains [n2x_variables/port_list:301/3 303/5]
I am just two days old to groovy, I need to parse a json file with below structure. My actual idea is I need to run a set of jobs in different environments based on different sequences, so I came up with this format of json as a input file to my groovy
{
"services": [{
"UI-Service": [{
"file-location": "/in/my/server/location",
"script-names": "daily-batch,weekly-batch,bi-weekly-batch",
"seq1": "daily-batch,weekly-batch",
"seq2": "daily-batch,weekly-batch,bi-weekly-batch",
"DEST-ENVT_seq1": ["DEV1", "DEV2", "QA1", "QA2"],
"DEST-ENVT_seq2": ["DEV3", "DEV4", "QA3", "QA4"]
}]
}, {
"Mobile-Service": [{
"file-location": "/in/my/server/location",
"script-names": "daily-batch,weekly-batch,bi-weekly-batch",
"seq1": "daily-batch,weekly-batch",
"seq2": "daily-batch,weekly-batch,bi-weekly-batch",
"DEST-ENVT_seq1": ["DEV1", "DEV2", "QA1", "QA2"],
"DEST-ENVT_seq2": ["DEV3", "DEV4", "QA3", "QA4"]
}]
}]
}
I tried below script for parsing the json
def jsonSlurper = new JsonSlurper()
//def reader = new BufferedReader(new InputStreamReader(new FileInputStream("in/my/location/config.json"),"UTF-8"))
//def data = jsonSlurper.parse(reader)
File file = new File("in/my/location/config.json")
def data = jsonSlurper.parse(file)
try{
Map jsonResult = (Map) data;
Map compService = (Map) jsonResult.get("services");
String name = (String) compService.get("UI-Service");
assert name.equals("file-location");
}catch (E){
println Exception
}
I need to first read all the services (UI-service, Mobile-Service, etc..) then their elements and their value
Or you could do something like:
new JsonSlurper().parseText(jsonTxt).services*.each { serviceName, elements ->
println serviceName
elements*.each { name, value ->
println " $name = $value"
}
}
But it depends what you want (and you don't really explain in the question)
Example for reading from JsonParser object:
def data = jsonSlurper.parse(file)
data.services.each{
def serviceName = it.keySet()
println "**** key:${serviceName} ******"
it.each{ k, v ->
println "element name: ${k}, element value: ${v}"
}
}
other options:
println data.services[0].get("UI-Service")["file-location"]
println data.services[1].get("Mobile-Service").seq1
Hi i have a problem with my code that i get a error in a loop that works for a few times but then throws me a typeerro: string indices must be integers.
I want to call an api to get a json back and get some parts of the json response. heres the code:
class API(object):
def __init__(self, api_key):
self.api_key = api_key
def _request(self, api_url, params={}):
args = {'api_key': self.api_key}
for key, value in params.items():
if key not in args:
args[key] = value
response = requests.get(
Consts.URL['base'].format(
url=api_url
),
params=args
)
if response.status_code == requests.codes.ok:
return response.json()
else:
return "not possible"
print(response.url)
def get_list(self):
excel = EXCEL('s6.xlsx')
api_url = Consts.URL['list'].format(
version = Consts.API_VERSIONS['matchversion'],
start = excel.get_gamenr()
)
return self._request(api_url)
def get_match(self, matchid):
idlist = matchid
api_url = Consts.URL['match'].format(
version = Consts.API_VERSIONS['matchversion'],
matchId = idlist
)
return self._request(api_url)
def match_ids(self):
api = API('c6ea2f68-7ed6-40fa-9b99-fd591c55c05f')
x = api.get_list()
y = x['matches']
count = len(y)
ids = []
while count > 0:
count = count - 1
temp = y[0]
ids.append(temp['matchId'])
del y[0]
return ids
def match_info(self):
matchids = self.match_ids()
print(matchids)
matchinfolist = {}
counter = 1
for gameids in matchids:
info = self.get_match(gameids)
myid = self.find_partid(info['participantIdentities'])
prepdstats = info['participants'][myid-1]
print(prepdstats)
matchinfolist['stats' + str(counter)] = prepdstats
return matchinfolist
def find_partid(self, partlist):
partid = 0
idlist = partlist
while partid < 10:
partid = partid + 1
tempplayer = idlist[0]['player']
if tempplayer['summonerId'] == 19204660:
playernr = partid
partid = 500
del idlist[0]
return playernr
when i run the match_info() function i get this error
Traceback (most recent call last):
File "C:\Users\Niklas\Desktop\python riot\main.py", line 17, in <module>
main()
File "C:\Users\Niklas\Desktop\python riot\main.py", line 10, in main
print(api.match_info())
File "C:\Users\Niklas\Desktop\python riot\api.py", line 78, in match_info
myid = self.find_partid(info['participantIdentities'])
TypeError: string indices must be integers
but only after the loop in the function has run for a few times. I have no idea what im doing wrong. Any help would be nice.
Here is a link to the json: https://euw.api.pvp.net/api/lol/euw/v2.2/match/2492271473?api_key=c6ea2f68-7ed6-40fa-9b99-fd591c55c05f
The error shows up on
myid = self.find_partid(info['participantIdentities'])
For this line to execute, info must be a mapping with string keys, not a string itself. info is
info = self.get_match(gameids)
get_match ends with
return self._request(api_url)
_request ends with
if response.status_code == requests.codes.ok:
return response.json()
else:
return "not possible"
For the loop to ever run, response.json() must be a dict with key 'participantIdentities'. Your bug is expecting that to always be true.
One fix might be to make the expectation always ture. If there is a satisfactory default value, return {'participantIdentities': <default value>}. Otherwise, return None and change the loop to
info = self.get_match(gameids)
if info is not None:
# as before
else:
# whatever default action you want
I have been experimenting with the groovy Jsonbuilder as you can see below trying to look at different ways to build JSON objects and arrays. After things started to make sense, I tried expanding to what is shown below. The question I have is, why does "content" show up in the json pretty string output? I actually have another json object displaying this.class information in json string outputs.
Any ideas? I'm new to this, so it could definitely be an obvious one.
def tt = ["test", "test1"]
def jjj = "jason"
def js3 = new groovy.json.JsonBuilder()
def js2 = new groovy.json.JsonBuilder(tt);
js3 hello: "$jjj", "$jjj": tt
def js4 = new groovy.json.JsonBuilder()
def result = js4([sdn: js3, openflow: js2, type: 3])
println js4.toPrettyString();
outputs:
{
"sdn": {
"content": {
"hello": "jason",
"jason": [
"test",
"test1"
]
}
},
"openflow": {
"content": [
"test",
"test1"
]
},
"type": 3
}
The problem can be restated as...
why does this:
import groovy.json.*
def js3 = new JsonBuilder(["test", "test1"])
def js4 = new JsonBuilder(js3)
println js4.toString()
print:
{"content":["test","test1"]}
and this:
import groovy.json.*
def js3 = new JsonBuilder(["test", "test1"])
def js4 = new JsonBuilder(js3.content)
println js4.toString()
prints this (?) :
["test","test1"]
The short answer is that JsonBuilder has a member named content, which represents the payload. When one JsonBuilder absorbs another, we want to replace the payload, and not nest it. This line is the way to replace the payload:
def js4 = new JsonBuilder(js3.content)
Ultimately, this stems from the fact that JsonBuilder.toString() (code here) calls JsonOutput.toJson(object) (code here).
An exercise for the reader is to experiment with:
class MyBuilder {
def content
}
def myB = new MyBuilder(content: ["test", "test1"])
println JsonOutput.toJson(myB)
println JsonOutput.toJson(myB.content)