Gremlin query to give response for 1 and 2 hops at same time
I have a graph setup with certain type of vertices like:
inputFiles, outputFiles, convertors
convertors basically take some input files, process them and produce output files.
For example:
convertor-abc -----output----> outputFile-1
|
|
input
|
|
inputFile-1
this inputFile-1 can be an input to more than one convertors., an input file can also be an output file for another convertor vice versa for output file.
I need to write a gremlin query which takes an input file id, looks for all the convertors for which this input file is given as input, and also all the input output files given to these convertors and gives in response;
For example:
{
"file": "inputFile-id",
"convertors":
[
{
"convertor": "convertor-1",
"inputs" : [
{
"file": "inputFile-id-1"
},
{
"file": "inputFile-id-2"
}
],
"outputs" : [
{
"file": "outputFile-id-1"
},
{
"file": "outputFile-id-2"
}
]
},
{
"convertor": "convertor-2",
"inputs" : [
{
"file": "inputFile-id-3"
},
{
"file": "inputFile-id-4"
}
],
"outputs" : [
{
"file": "outputFile-id-3"
},
{
"file": "outputFile-id-4"
}
]
}
]
}
How can I achieve this in single gremlin query, also it is fesable to have a single query to achieve this? or its better to break the query into two and first we fetch all the convertors and then look for its input output files?6 Replies
When asking questions about Gremlin it's usually best to include some sample data to make things clear and so that we can provide a tested query as an answer. Your diagram and your explanation of your schema don't quite align for me. Is this representative of your data? if not, please edit and provide a more accurate example:
apologies @spmallette
yes that is the correct representation of the graph.
I just added one more correction, please check https://gremlify.com/gko236mppr6
thanks for your response
so, given:
i think you just need an nested
project()
:
The output doesn' t quite match what you're asking but that should inspire you to the solution if you need to match it exactly:
@spmallette hey thanks!!
I needed to get the list of converters for which a added .fold() which fulfilled my purpose.
Appreciate your help 🙂
just one more query, do you believe the query can face issues due to loops?
i don't think so. you usually worry about loops when you have
repeat()
which if you don't use proper guards can lead to traversals that simply never end. but here you only traverse in()/out()
a finite number of times, so unless you don't want such outputs i don't think there's a problemgot it!! thanks a lot