How to find something using the Java code Gremlin or Pipe

279 views
Skip to first unread message

Pranav Shah

unread,
Apr 28, 2012, 7:30:55 PM4/28/12
to gremli...@googlegroups.com
  I am trying to learn the basics but having a very hard time.  This is what I am trying to do (based on the sample graph):
  Find all app / name created by marko, josh, and peter using the language java
  The answer of course is "lop"

  Thanks

Pierre De Wilde

unread,
Apr 29, 2012, 2:18:19 AM4/29/12
to gremli...@googlegroups.com
Hi,

Your 'hard time' is comprehensible since there is no easy way to express an ADD clause in Gremlin.

I suggest to start with one vertex and express the AND clause with a filter step (*):

g.v(1).out('created').has('lang','java').as('x').in('created').filter{it.name in ['marko','josh','peter']}.back('x').name

HTH,
Pierre

(*) it is more efficient to use the has() step; unfortunately, there is no IN op.

Marko Rodriguez

unread,
Apr 29, 2012, 2:44:25 PM4/29/12
to gremli...@googlegroups.com
Hi,

Another approach is simply search for all java lang apps using an index and the filter. E.g.

g.idx(T.v)[[lang:'java']].filter{
it.in('created').gather.transform{it.name as Set == ['marko','josh','peter'] as Set}.next()
}.name 

1. get all the java language projects
2. filter them by 3.
3. for each project, gather up who created them into a list and emit 'true' if the names are all of marko, josh and peter. (by using Set equality)
4. return the name of the projects

gremlin> g.idx(T.v)[[lang:'java']].filter{it.in('created').gather.transform{it.name as Set == ['marko','josh','peter'] as Set}.next()}.name 
==>lop

pShah

unread,
Apr 30, 2012, 10:35:09 AM4/30/12
to gremli...@googlegroups.com
Hi Marko,
  Thanks for the response.  My problem is a bit different than described in your query.

  You assume that language being searched.  What I am trying to do is:

1.  Find the vertices for 'marko', 'peter', and 'josh'
2.  Find what they 'created' together.

  I am assuming that I can use the same Index approach that you provided, but I am not sure. 

  I will try reading more on the wiki and the code to understand how to:
1. Create Index in Tinkergraph in Java
2. Create a list of the results based on the index search

  Would I be on the right track with this approach?

pShah

unread,
Apr 30, 2012, 10:37:38 AM4/30/12
to gremli...@googlegroups.com
Hi,
  This approach would work if the vertex was known, but I am trying to find the vertex based on some parameters?

  I will try the index approach and see how that works out.

Thanks

Pierre De Wilde

unread,
Apr 30, 2012, 10:45:55 AM4/30/12
to gremli...@googlegroups.com
Hi,

Replace g.v(1) by g.idx(T.v)[[name:'marko']] to find first vertex:

g.idx(T.v)[[name:'marko']].out('created').has('lang','java').as('x').in('created').filter{it.name in ['marko','josh','peter']}.back('x').name

HTH,
Pierre

pShah

unread,
Apr 30, 2012, 11:59:56 AM4/30/12
to gremli...@googlegroups.com
Thanks.

I will start by creating an index tonight and see how that works.  Thanks.

Marko Rodriguez

unread,
Apr 30, 2012, 12:42:22 PM4/30/12
to gremli...@googlegroups.com
Hi,

> What I am trying to do is:
>
> 1. Find the vertices for 'marko', 'peter', and 'josh'
> 2. Find what they 'created' together.

There are various ways to do this. First, lets get the vertices marko, peter, and josh using the default Vertex index.

marko = g.idx(T.v)[[name:'marko']].next()
peter = g.idx(T.v)[[name:'peter']].next()
josh = g.idx(T.v)[[name:'josh']].next()

-------------------------------------

1. No set intersection
* count the number of times a project is traversed too, if its been traversed to three times, it must have been created by all marko, peter, and josh.

gremlin> [marko, peter, josh]._().out('created').groupCount.cap.scatter.filter{it.value == 3}.transform{it.key}.name
==>lop

2. Set intersection
* intersect the projects that marko, josh, and peter have created --- note that this requires memory.

gremlin> marko.out('created').toList().intersect(peter.out('created').toList()).intersect(josh.out('created').toList()).name
==>lop

I'm sure there are other ways of attacking the problem. However, I believe this provides you the two extremes (data flow vs. set-based). If you come up with some new ways, please post them.

Good luck,
Marko.

http://markorodriguez.com

pShah

unread,
May 8, 2012, 12:32:10 AM5/8/12
to gremli...@googlegroups.com
How do you specify the "filter" on "scatter"?

I tried multiple things before posting here, but I cannot solve it.

I wrote this small function which is called from inside the filter, but all I know is the Object class, but no clue on how to work with it.

PipeFunction GetFilter = new PipeFunction<Object, Boolean>() {
            public Boolean compute(Object o) {
                System.out.println(o.getClass());
                    return false;
            }
        };

Output:  class java.util.HashMap$Entry

Almost everything I try to get the avle gives me a ClassCastException:
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap$Entry cannot be cast to java.util.Map

Please help.
Reply all
Reply to author
Forward
0 new messages