date formatting in Gremlin

1,254 views
Skip to first unread message

Adolfo Rodriguez

unread,
Aug 12, 2011, 10:48:36 AM8/12/11
to Gremlin-users
Hi all,

lets assume a Gremlin expression as below:

resultsMap = [:]
sourceNode.in("some").outE("arbitrarily").inV("long").out("traversal").DATE.groupCount(resultsMap)

I am trying to count the number of repeats for each day in my records.
However, my date fields come as "yyyy-MM-dd HH:mm:ss".
I would need some way in Gremlin to format this date (in my case to
remove the time) to feed groupCount method.

Something as:

sourceNode.in("some").outE("arbitrarily").inV("long").out("traversal").DATE.format("yyyy-
MM-dd").groupCount(resultsMap).

How can I do this in Gremlin??

Thanks,

Adolfo

Boris Kizelshteyn

unread,
Aug 12, 2011, 11:19:57 AM8/12/11
to gremli...@googlegroups.com
Hi Adolfo,

This is not a direct answer to your question, but perhaps it will help in some way: when working with dates I find it useful to work with epoch time stamps, that way you can sort and count ints rather than date objects. If you could get the date into epoch format before you run gremlin it should work just fine. Maybe something to consider until someone who knows java gets back to you (I'm a python hacker myself ;))

Best!

Daniel Quest

unread,
Aug 12, 2011, 11:50:15 AM8/12/11
to gremli...@googlegroups.com
Before you do the group count do a .transform{it.substring()} to wack
off the seconds

-Daniel

Pierre De Wilde

unread,
Aug 12, 2011, 11:59:51 AM8/12/11
to gremli...@googlegroups.com
Hi,

Marko would said: what a beautiful name you have! Hopefully, he is on holidays...

As Daniel suggested, the way to transform DATE into YYYY-MM-DD is the transform{} step.

If DATE is a string:

.transform{it.DATE.substring(0,10)}
==>2011-08-05

If DATE is a date:

.transform{sprintf("%d-%02d-%02d", it.DATE.getYear()+1900, it.DATE.getMonth()+1, it.DATE.getDay())}
==>2011-08-05

HTH,
Pierre

2011/8/12 Boris Kizelshteyn <boris.ki...@popcha.com>

Adolfo Rodriguez

unread,
Aug 12, 2011, 12:47:05 PM8/12/11
to Gremlin-users
wow, still unleashing the power of Gremlin. Tested in OrientDB and it
works.
Thanks Boris, Daniel and Pierre,

Adolfo



On Aug 12, 5:59 pm, Pierre De Wilde <pierredewi...@gmail.com> wrote:
> Hi,
>
> Marko would said: what a beautiful name you have! Hopefully, he is on
> holidays...
>
> As Daniel suggested, the way to transform DATE into YYYY-MM-DD is the
> transform{} step.
>
> If DATE is a string:
>
> .transform{it.DATE.substring(0,10)}
> ==>2011-08-05
>
> If DATE is a date:
>
> .transform{sprintf("%d-%02d-%02d", it.DATE.getYear()+1900,
> it.DATE.getMonth()+1, it.DATE.getDay())}
> ==>2011-08-05
>
> HTH,
> Pierre
>
> 2011/8/12 Boris Kizelshteyn <boris.kizelsht...@popcha.com>
>
>
>
>
>
>
>
> > Hi Adolfo,
>
> > This is not a direct answer to your question, but perhaps it will help in
> > some way: when working with dates I find it useful to work with epoch time
> > stamps, that way you can sort and count ints rather than date objects. If
> > you could get the date into epoch format before you run gremlin it should
> > work just fine. Maybe something to consider until someone who knows java
> > gets back to you (I'm a python hacker myself ;))
>
> > Best!
>
> > On Fri, Aug 12, 2011 at 10:48 AM, Adolfo Rodriguez <pellyado...@yahoo.es>wrote:
>
> >> Hi all,
>
> >> lets assume a Gremlin expression as below:
>
> >> resultsMap = [:]
>
> >> sourceNode.in("some").outE("arbitrarily").inV("long").out("traversal").DATE .groupCount(resultsMap)
>
> >> I am trying to count the number of repeats for each day in my records.
> >> However, my date fields come as "yyyy-MM-dd HH:mm:ss".
> >> I would need some way in Gremlin to format this date (in my case to
> >> remove the time) to feed groupCount method.
>
> >> Something as:
>
> >> sourceNode.in("some").outE("arbitrarily").inV("long").out("traversal").DATE .format("yyyy-
Reply all
Reply to author
Forward
0 new messages