You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pig.apache.org by dhaval deshpande <dh...@gmail.com> on 2009/11/25 22:53:55 UTC

Need help with grouping.

Hi,
       I am back with a questions again :). This time i can explain better
because I have explored little better than what i did last time :). I have
three fields in my table. And they are name, id, time from and time to which
tuple existed in the database. so for example 1-4 means tuple existed from
1sec to 4 sec in the database. So my table looks like this.

name1 , 1, 1-4
name2, 2, 1-6
name3, 3, 2-4
name4, 4, 3-7
name5, 5, 4-6

Now I want to group this table using my timestamp field. and I wanted my
intended output to be like this

(1-4, {(name1,1,1-4),(name3,3,2-4)})
(1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
(2-4, {(name3, 3, 2-4)})
(3-7, {(name4,4,3-7),(name5,5,4-6)})
(4-6, {(name5,5,4-6)})

But the problem I was facing was when I extend the EvalFunc class then I
work with a single tuple at a time and I dont have all the other tuple
information so that I can iterate though it and check if any other tuple
exist between these time stamps and add it to the group as well. Any ideas?

Re: Need help with grouping.

Posted by Zaki Rahaman <za...@gmail.com>.
It depends. If you want to manipulate your timestamp field numerically 
(take average, difference, etc.) you will have to parse it somehow.  
You can write a custom LoadFunc to load your data in (harder) or you  
can write an EvalFunc(easier) that will return new tipped with the  
beginning and ending timestamps.

If you want to group first you can group how I mentioned then apply  
your EvalFunc to get back int or long timestamp pairs and do whatever  
you're trying to do

I really need a better idea of what you're doing to really help you out.

Sent from my iPhone

On Nov 25, 2009, at 5:10 PM, dhaval deshpande <dhaval.deshpande@gmail.com 
 > wrote:

> Hi Zaki,
>              My timestamp field is chararray ,  so pig will not  
> recognize
> it as timestamps right? And it will not take care of it. I did  
> exactly what
> you had told me before and then realized the same problem and then  
> decided
> to write a UDF. But again had the problem of being handling only one  
> tuple
> at a time as I had mentioned above.
>
> Thanks,
> Dhaval.
>
> On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman  
> <za...@gmail.com>wrote:
>
>> It's a lot simpler than that... you simply have to tell pig to  
>> group your
>> data on the timestamp field... it takes care of the rest..
>>
>> something like..
>>
>> A = LOAD 'data' AS (name, id, timestamp);
>> B = GROUP A by timestamp.
>>
>> Yes, it's that easy.
>>
>> On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
>> dhaval.deshpande@gmail.com> wrote:
>>
>>> Hi,
>>>      I am back with a questions again :). This time i can explain  
>>> better
>>> because I have explored little better than what i did last  
>>> time :). I
>> have
>>> three fields in my table. And they are name, id, time from and  
>>> time to
>>> which
>>> tuple existed in the database. so for example 1-4 means tuple  
>>> existed
>> from
>>> 1sec to 4 sec in the database. So my table looks like this.
>>>
>>> name1 , 1, 1-4
>>> name2, 2, 1-6
>>> name3, 3, 2-4
>>> name4, 4, 3-7
>>> name5, 5, 4-6
>>>
>>> Now I want to group this table using my timestamp field. and I  
>>> wanted my
>>> intended output to be like this
>>>
>>> (1-4, {(name1,1,1-4),(name3,3,2-4)})
>>> (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
>>> (2-4, {(name3, 3, 2-4)})
>>> (3-7, {(name4,4,3-7),(name5,5,4-6)})
>>> (4-6, {(name5,5,4-6)})
>>>
>>> But the problem I was facing was when I extend the EvalFunc class  
>>> then I
>>> work with a single tuple at a time and I dont have all the other  
>>> tuple
>>> information so that I can iterate though it and check if any other  
>>> tuple
>>> exist between these time stamps and add it to the group as well. Any
>> ideas?
>>>
>>
>>
>>
>> --
>> Zaki Rahaman
>>

Re: Need help with grouping.

Posted by Nikhil Gupta <gu...@gmail.com>.
I don't know how you plan to group on basis of the last field in your table.
Writing an UDF that gets all the tuples (irrespective of time ranges) and
then outputs overlapping range groups seems cumbersome. One thing you can
probably try to do is to flatten the timestamp ranges. So, for example -

"name1, 1, 1-4" will become

"name1, 1, 1-4, 1"
"name1, 1, 1-4, 2"
"name1, 1, 1-4, 3"
"name1, 1, 1-4, 4"

Then you can group by this flattened time field.

-nikhil


On Wed, Nov 25, 2009 at 2:10 PM, dhaval deshpande <
dhaval.deshpande@gmail.com> wrote:

> Hi Zaki,
>              My timestamp field is chararray ,  so pig will not recognize
> it as timestamps right? And it will not take care of it. I did exactly what
> you had told me before and then realized the same problem and then decided
> to write a UDF. But again had the problem of being handling only one tuple
> at a time as I had mentioned above.
>
> Thanks,
> Dhaval.
>
> On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman <zaki.rahaman@gmail.com
> >wrote:
>
> > It's a lot simpler than that... you simply have to tell pig to group your
> > data on the timestamp field... it takes care of the rest..
> >
> > something like..
> >
> > A = LOAD 'data' AS (name, id, timestamp);
> > B = GROUP A by timestamp.
> >
> > Yes, it's that easy.
> >
> > On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
> > dhaval.deshpande@gmail.com> wrote:
> >
> > > Hi,
> > >       I am back with a questions again :). This time i can explain
> better
> > > because I have explored little better than what i did last time :). I
> > have
> > > three fields in my table. And they are name, id, time from and time to
> > > which
> > > tuple existed in the database. so for example 1-4 means tuple existed
> > from
> > > 1sec to 4 sec in the database. So my table looks like this.
> > >
> > > name1 , 1, 1-4
> > > name2, 2, 1-6
> > > name3, 3, 2-4
> > > name4, 4, 3-7
> > > name5, 5, 4-6
> > >
> > > Now I want to group this table using my timestamp field. and I wanted
> my
> > > intended output to be like this
> > >
> > > (1-4, {(name1,1,1-4),(name3,3,2-4)})
> > > (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
> > > (2-4, {(name3, 3, 2-4)})
> > > (3-7, {(name4,4,3-7),(name5,5,4-6)})
> > > (4-6, {(name5,5,4-6)})
> > >
> > > But the problem I was facing was when I extend the EvalFunc class then
> I
> > > work with a single tuple at a time and I dont have all the other tuple
> > > information so that I can iterate though it and check if any other
> tuple
> > > exist between these time stamps and add it to the group as well. Any
> > ideas?
> > >
> >
> >
> >
> > --
> > Zaki Rahaman
> >
>

Re: Need help with grouping.

Posted by Dmitriy Ryaboy <dv...@gmail.com>.
On Wed, Nov 25, 2009 at 5:59 PM, Dmitriy Ryaboy <dv...@gmail.com> wrote:

> This is a good use case that manages to expose a with the UDF apis -- it
> would be nice to output multiple records per processed tuple in exec(), to
> allow the kind of processing actual Pig operators sometimes do, with
> buffering inputs and the like.
>
>
I seem to have swallowed a word: "expose a *problem* with UDF apis"

Re: Need help with grouping.

Posted by Dmitriy Ryaboy <dv...@gmail.com>.
What I mean is, you calculate the starts and ends, and store them to a
regular file in HDFS.
Once the file is written (you will need to call exec if multiquery
exectution is on), you can read it into memory using regular IO operations
when initializing your emitter EvalFunc.

-D

On Wed, Nov 25, 2009 at 11:53 PM, dhaval deshpande <
dhaval.deshpande@gmail.com> wrote:

> Hi Dmitriy,
>                   When I find the start and end interval for each tuple
> then how do I store it in memory or in some place or file so that when I
> read the next tuple I could check with my start and end interval and if it
> does belong to the same group then add it to the bucket? Also I didnt
> understand the meaning of sidefile. Can you please explain me that? Sorry
> for the trouble.
>
> Thanks,
> Dhaval.
> On Wed, Nov 25, 2009 at 3:59 PM, Dmitriy Ryaboy <dv...@gmail.com>
> wrote:
>
> > Hi Dhaval,
> > First, I want to caution you against doing this :-). You are increasing
> the
> > cardinality of your data quite a bit, as every record gets repeated as
> many
> > times in the output as there are overlapping time periods. I imagine that
> > can lead to multiplying the number of tuples by a fairly large factor.
> >
> > Assuming you ignore this warning, and proceed anyway...
> >
> > Here's an idea of how you could go about this:
> >
> > 0. determine needed degree of parallelism, P.
> >
> > 1. Find timestamps such that they divide your records into P roughly even
> > chunks (they may be equidistant if your timestamps are uniformly
> > distributed
> > along the time range, or they may need to be adjusted to follow the
> actual
> > distribution of the data).
> >
> > 2. For each tuple, emit it to all intervals of the timeline, as described
> > by
> > the timestamps from the previous step, such that (interval_start >=
> > tuple_start && interval_end <= tuple_end) ( this double counts if you
> have
> > 0-width intervals, btw, so be careful there).
> >
> > 3. Spin up a reducer for each bucket. Perform the grouping in memory by
> > streaming tuples sorted by start time; any time you see a tuple whose
> start
> > is after some group's end, you can output that group.
> >
> > Approach for implementing this in Pig:
> >
> > Finding the boundaries can be done by a finding min, max, and count, and
> > dividing accordingly. These values can be written into a side file, then
> > you
> > can stream the data through a custom binary that will perform step 2, by
> > reading the sidefile, and outputting the records it sees with the bucket
> > number as an added first field. Step 3 can be done by grouping by first
> > field (bucket #) of step 2's output, streaming once more through a custom
> > aggregator.
> >
> > This is a good use case that manages to expose a with the UDF apis -- it
> > would be nice to output multiple records per processed tuple in exec(),
> to
> > allow the kind of processing actual Pig operators sometimes do, with
> > buffering inputs and the like.
> >
> > -Dmitriy
> >
> > On Wed, Nov 25, 2009 at 5:10 PM, dhaval deshpande <
> > dhaval.deshpande@gmail.com> wrote:
> >
> > > Hi Zaki,
> > >              My timestamp field is chararray ,  so pig will not
> recognize
> > > it as timestamps right? And it will not take care of it. I did exactly
> > what
> > > you had told me before and then realized the same problem and then
> > decided
> > > to write a UDF. But again had the problem of being handling only one
> > tuple
> > > at a time as I had mentioned above.
> > >
> > > Thanks,
> > > Dhaval.
> > >
> > > On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman <zaki.rahaman@gmail.com
> > > >wrote:
> > >
> > > > It's a lot simpler than that... you simply have to tell pig to group
> > your
> > > > data on the timestamp field... it takes care of the rest..
> > > >
> > > > something like..
> > > >
> > > > A = LOAD 'data' AS (name, id, timestamp);
> > > > B = GROUP A by timestamp.
> > > >
> > > > Yes, it's that easy.
> > > >
> > > > On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
> > > > dhaval.deshpande@gmail.com> wrote:
> > > >
> > > > > Hi,
> > > > >       I am back with a questions again :). This time i can explain
> > > better
> > > > > because I have explored little better than what i did last time :).
> I
> > > > have
> > > > > three fields in my table. And they are name, id, time from and time
> > to
> > > > > which
> > > > > tuple existed in the database. so for example 1-4 means tuple
> existed
> > > > from
> > > > > 1sec to 4 sec in the database. So my table looks like this.
> > > > >
> > > > > name1 , 1, 1-4
> > > > > name2, 2, 1-6
> > > > > name3, 3, 2-4
> > > > > name4, 4, 3-7
> > > > > name5, 5, 4-6
> > > > >
> > > > > Now I want to group this table using my timestamp field. and I
> wanted
> > > my
> > > > > intended output to be like this
> > > > >
> > > > > (1-4, {(name1,1,1-4),(name3,3,2-4)})
> > > > > (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
> > > > > (2-4, {(name3, 3, 2-4)})
> > > > > (3-7, {(name4,4,3-7),(name5,5,4-6)})
> > > > > (4-6, {(name5,5,4-6)})
> > > > >
> > > > > But the problem I was facing was when I extend the EvalFunc class
> > then
> > > I
> > > > > work with a single tuple at a time and I dont have all the other
> > tuple
> > > > > information so that I can iterate though it and check if any other
> > > tuple
> > > > > exist between these time stamps and add it to the group as well.
> Any
> > > > ideas?
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Zaki Rahaman
> > > >
> > >
> >
>

Re: Need help with grouping.

Posted by dhaval deshpande <dh...@gmail.com>.
Hi Dmitriy,
                   When I find the start and end interval for each tuple
then how do I store it in memory or in some place or file so that when I
read the next tuple I could check with my start and end interval and if it
does belong to the same group then add it to the bucket? Also I didnt
understand the meaning of sidefile. Can you please explain me that? Sorry
for the trouble.

Thanks,
Dhaval.
On Wed, Nov 25, 2009 at 3:59 PM, Dmitriy Ryaboy <dv...@gmail.com> wrote:

> Hi Dhaval,
> First, I want to caution you against doing this :-). You are increasing the
> cardinality of your data quite a bit, as every record gets repeated as many
> times in the output as there are overlapping time periods. I imagine that
> can lead to multiplying the number of tuples by a fairly large factor.
>
> Assuming you ignore this warning, and proceed anyway...
>
> Here's an idea of how you could go about this:
>
> 0. determine needed degree of parallelism, P.
>
> 1. Find timestamps such that they divide your records into P roughly even
> chunks (they may be equidistant if your timestamps are uniformly
> distributed
> along the time range, or they may need to be adjusted to follow the actual
> distribution of the data).
>
> 2. For each tuple, emit it to all intervals of the timeline, as described
> by
> the timestamps from the previous step, such that (interval_start >=
> tuple_start && interval_end <= tuple_end) ( this double counts if you have
> 0-width intervals, btw, so be careful there).
>
> 3. Spin up a reducer for each bucket. Perform the grouping in memory by
> streaming tuples sorted by start time; any time you see a tuple whose start
> is after some group's end, you can output that group.
>
> Approach for implementing this in Pig:
>
> Finding the boundaries can be done by a finding min, max, and count, and
> dividing accordingly. These values can be written into a side file, then
> you
> can stream the data through a custom binary that will perform step 2, by
> reading the sidefile, and outputting the records it sees with the bucket
> number as an added first field. Step 3 can be done by grouping by first
> field (bucket #) of step 2's output, streaming once more through a custom
> aggregator.
>
> This is a good use case that manages to expose a with the UDF apis -- it
> would be nice to output multiple records per processed tuple in exec(), to
> allow the kind of processing actual Pig operators sometimes do, with
> buffering inputs and the like.
>
> -Dmitriy
>
> On Wed, Nov 25, 2009 at 5:10 PM, dhaval deshpande <
> dhaval.deshpande@gmail.com> wrote:
>
> > Hi Zaki,
> >              My timestamp field is chararray ,  so pig will not recognize
> > it as timestamps right? And it will not take care of it. I did exactly
> what
> > you had told me before and then realized the same problem and then
> decided
> > to write a UDF. But again had the problem of being handling only one
> tuple
> > at a time as I had mentioned above.
> >
> > Thanks,
> > Dhaval.
> >
> > On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman <zaki.rahaman@gmail.com
> > >wrote:
> >
> > > It's a lot simpler than that... you simply have to tell pig to group
> your
> > > data on the timestamp field... it takes care of the rest..
> > >
> > > something like..
> > >
> > > A = LOAD 'data' AS (name, id, timestamp);
> > > B = GROUP A by timestamp.
> > >
> > > Yes, it's that easy.
> > >
> > > On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
> > > dhaval.deshpande@gmail.com> wrote:
> > >
> > > > Hi,
> > > >       I am back with a questions again :). This time i can explain
> > better
> > > > because I have explored little better than what i did last time :). I
> > > have
> > > > three fields in my table. And they are name, id, time from and time
> to
> > > > which
> > > > tuple existed in the database. so for example 1-4 means tuple existed
> > > from
> > > > 1sec to 4 sec in the database. So my table looks like this.
> > > >
> > > > name1 , 1, 1-4
> > > > name2, 2, 1-6
> > > > name3, 3, 2-4
> > > > name4, 4, 3-7
> > > > name5, 5, 4-6
> > > >
> > > > Now I want to group this table using my timestamp field. and I wanted
> > my
> > > > intended output to be like this
> > > >
> > > > (1-4, {(name1,1,1-4),(name3,3,2-4)})
> > > > (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
> > > > (2-4, {(name3, 3, 2-4)})
> > > > (3-7, {(name4,4,3-7),(name5,5,4-6)})
> > > > (4-6, {(name5,5,4-6)})
> > > >
> > > > But the problem I was facing was when I extend the EvalFunc class
> then
> > I
> > > > work with a single tuple at a time and I dont have all the other
> tuple
> > > > information so that I can iterate though it and check if any other
> > tuple
> > > > exist between these time stamps and add it to the group as well. Any
> > > ideas?
> > > >
> > >
> > >
> > >
> > > --
> > > Zaki Rahaman
> > >
> >
>

Re: Need help with grouping.

Posted by Dmitriy Ryaboy <dv...@gmail.com>.
Hm. I see your point.

Maybe just a special UDF for which exec always returns DataBag instead of T,
which gets auto-flattened and auto-filtered by IsEmpty(), and which is
guaranteed to get an "endOfInput(); exec(null)" at the end of execution?

-D

On Wed, Nov 25, 2009 at 8:07 PM, Alan Gates <ga...@yahoo-inc.com> wrote:

>
> On Nov 25, 2009, at 4:04 PM, Dmitriy Ryaboy wrote:
>
>  On Wed, Nov 25, 2009 at 6:45 PM, Alan Gates <ga...@yahoo-inc.com> wrote:
>>
>>  A UDF that returns a bag followed by a flatten allows multiple output
>>> records per input.  It does require the input and output be synchronized.
>>> That is, exec must return some output each time.  To get around this,
>>> there
>>> is streaming.  Streaming isn't as efficient in this case because the
>>> executable is run in another process.  But functionally at least I think
>>> Pig
>>> has this covered.
>>>
>>> Alan.
>>>
>>>
>> Alan,
>>
>> Please correct me if I am thinking about this wrong:
>>
>> The approach you describe works when you want to output 1+ output record
>> per
>> input, although I think the need to use flatten afterwards leads to
>> programs
>> that are less "clean" than they could be.  However, it breaks down if we
>> are
>> writing, for example, some variant of an in-memory group by operator.
>> Assuming that the input is coming in sorted by group key, one can buffer
>> until the key changes; as is, this requires outputting nulls, and
>> post-filtering by not null. This is a bit clunky, but it works. More
>> significantly, there is no way to output the last bag, since you only know
>> that no more keys are coming in the finish() call, at which point you
>> don't
>> have a way to produce any output.
>>
>> So, for example, the reduce-side grouping I described can't actually be
>> written as a UDF, even if we were to try the bag+flatten approach.
>>
>
> Right, that's what I was trying to say above.
>
>
>
>> I completely agree that streaming solves this issue, but reworking udfs to
>> a
>> pull model would allow us to not need streaming.
>>
>
> By this do you mean UDFs should be able to communicate to the framework
> 'give me another record' rather than returning a value?  That seems like
> what is necessary here.  That would get confusing if there were other
> elements in the foreach's projection:
>
> B = foreach A generate udf1(x), udf2(y);
>
> if udf1 calls for another record, what do we do with udf2?
>
> Alan.
>
>
>> -D
>>
>
>

Re: Need help with grouping.

Posted by Alan Gates <ga...@yahoo-inc.com>.
On Nov 25, 2009, at 4:04 PM, Dmitriy Ryaboy wrote:

> On Wed, Nov 25, 2009 at 6:45 PM, Alan Gates <ga...@yahoo-inc.com>  
> wrote:
>
>> A UDF that returns a bag followed by a flatten allows multiple output
>> records per input.  It does require the input and output be  
>> synchronized.
>> That is, exec must return some output each time.  To get around  
>> this, there
>> is streaming.  Streaming isn't as efficient in this case because the
>> executable is run in another process.  But functionally at least I  
>> think Pig
>> has this covered.
>>
>> Alan.
>>
>
> Alan,
>
> Please correct me if I am thinking about this wrong:
>
> The approach you describe works when you want to output 1+ output  
> record per
> input, although I think the need to use flatten afterwards leads to  
> programs
> that are less "clean" than they could be.  However, it breaks down  
> if we are
> writing, for example, some variant of an in-memory group by operator.
> Assuming that the input is coming in sorted by group key, one can  
> buffer
> until the key changes; as is, this requires outputting nulls, and
> post-filtering by not null. This is a bit clunky, but it works. More
> significantly, there is no way to output the last bag, since you  
> only know
> that no more keys are coming in the finish() call, at which point  
> you don't
> have a way to produce any output.
>
> So, for example, the reduce-side grouping I described can't actually  
> be
> written as a UDF, even if we were to try the bag+flatten approach.

Right, that's what I was trying to say above.

>
> I completely agree that streaming solves this issue, but reworking  
> udfs to a
> pull model would allow us to not need streaming.

By this do you mean UDFs should be able to communicate to the  
framework 'give me another record' rather than returning a value?   
That seems like what is necessary here.  That would get confusing if  
there were other elements in the foreach's projection:

B = foreach A generate udf1(x), udf2(y);

if udf1 calls for another record, what do we do with udf2?

Alan.

>
> -D


Re: Need help with grouping.

Posted by Dmitriy Ryaboy <dv...@gmail.com>.
On Wed, Nov 25, 2009 at 6:45 PM, Alan Gates <ga...@yahoo-inc.com> wrote:

> A UDF that returns a bag followed by a flatten allows multiple output
> records per input.  It does require the input and output be synchronized.
>  That is, exec must return some output each time.  To get around this, there
> is streaming.  Streaming isn't as efficient in this case because the
> executable is run in another process.  But functionally at least I think Pig
> has this covered.
>
> Alan.
>

Alan,

Please correct me if I am thinking about this wrong:

The approach you describe works when you want to output 1+ output record per
input, although I think the need to use flatten afterwards leads to programs
that are less "clean" than they could be.  However, it breaks down if we are
writing, for example, some variant of an in-memory group by operator.
Assuming that the input is coming in sorted by group key, one can buffer
until the key changes; as is, this requires outputting nulls, and
post-filtering by not null. This is a bit clunky, but it works. More
significantly, there is no way to output the last bag, since you only know
that no more keys are coming in the finish() call, at which point you don't
have a way to produce any output.

So, for example, the reduce-side grouping I described can't actually be
written as a UDF, even if we were to try the bag+flatten approach.

I completely agree that streaming solves this issue, but reworking udfs to a
pull model would allow us to not need streaming.

-D

Re: Need help with grouping.

Posted by Alan Gates <ga...@yahoo-inc.com>.
On Nov 25, 2009, at 2:59 PM, Dmitriy Ryaboy wrote:

<snip>
>
>
> This is a good use case that manages to expose a with the UDF apis  
> -- it
> would be nice to output multiple records per processed tuple in  
> exec(), to
> allow the kind of processing actual Pig operators sometimes do, with
> buffering inputs and the like.
>

A UDF that returns a bag followed by a flatten allows multiple output  
records per input.  It does require the input and output be  
synchronized.  That is, exec must return some output each time.  To  
get around this, there is streaming.  Streaming isn't as efficient in  
this case because the executable is run in another process.  But  
functionally at least I think Pig has this covered.

Alan.

Re: Need help with grouping.

Posted by Dmitriy Ryaboy <dv...@gmail.com>.
Hi Dhaval,
First, I want to caution you against doing this :-). You are increasing the
cardinality of your data quite a bit, as every record gets repeated as many
times in the output as there are overlapping time periods. I imagine that
can lead to multiplying the number of tuples by a fairly large factor.

Assuming you ignore this warning, and proceed anyway...

Here's an idea of how you could go about this:

0. determine needed degree of parallelism, P.

1. Find timestamps such that they divide your records into P roughly even
chunks (they may be equidistant if your timestamps are uniformly distributed
along the time range, or they may need to be adjusted to follow the actual
distribution of the data).

2. For each tuple, emit it to all intervals of the timeline, as described by
the timestamps from the previous step, such that (interval_start >=
tuple_start && interval_end <= tuple_end) ( this double counts if you have
0-width intervals, btw, so be careful there).

3. Spin up a reducer for each bucket. Perform the grouping in memory by
streaming tuples sorted by start time; any time you see a tuple whose start
is after some group's end, you can output that group.

Approach for implementing this in Pig:

Finding the boundaries can be done by a finding min, max, and count, and
dividing accordingly. These values can be written into a side file, then you
can stream the data through a custom binary that will perform step 2, by
reading the sidefile, and outputting the records it sees with the bucket
number as an added first field. Step 3 can be done by grouping by first
field (bucket #) of step 2's output, streaming once more through a custom
aggregator.

This is a good use case that manages to expose a with the UDF apis -- it
would be nice to output multiple records per processed tuple in exec(), to
allow the kind of processing actual Pig operators sometimes do, with
buffering inputs and the like.

-Dmitriy

On Wed, Nov 25, 2009 at 5:10 PM, dhaval deshpande <
dhaval.deshpande@gmail.com> wrote:

> Hi Zaki,
>              My timestamp field is chararray ,  so pig will not recognize
> it as timestamps right? And it will not take care of it. I did exactly what
> you had told me before and then realized the same problem and then decided
> to write a UDF. But again had the problem of being handling only one tuple
> at a time as I had mentioned above.
>
> Thanks,
> Dhaval.
>
> On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman <zaki.rahaman@gmail.com
> >wrote:
>
> > It's a lot simpler than that... you simply have to tell pig to group your
> > data on the timestamp field... it takes care of the rest..
> >
> > something like..
> >
> > A = LOAD 'data' AS (name, id, timestamp);
> > B = GROUP A by timestamp.
> >
> > Yes, it's that easy.
> >
> > On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
> > dhaval.deshpande@gmail.com> wrote:
> >
> > > Hi,
> > >       I am back with a questions again :). This time i can explain
> better
> > > because I have explored little better than what i did last time :). I
> > have
> > > three fields in my table. And they are name, id, time from and time to
> > > which
> > > tuple existed in the database. so for example 1-4 means tuple existed
> > from
> > > 1sec to 4 sec in the database. So my table looks like this.
> > >
> > > name1 , 1, 1-4
> > > name2, 2, 1-6
> > > name3, 3, 2-4
> > > name4, 4, 3-7
> > > name5, 5, 4-6
> > >
> > > Now I want to group this table using my timestamp field. and I wanted
> my
> > > intended output to be like this
> > >
> > > (1-4, {(name1,1,1-4),(name3,3,2-4)})
> > > (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
> > > (2-4, {(name3, 3, 2-4)})
> > > (3-7, {(name4,4,3-7),(name5,5,4-6)})
> > > (4-6, {(name5,5,4-6)})
> > >
> > > But the problem I was facing was when I extend the EvalFunc class then
> I
> > > work with a single tuple at a time and I dont have all the other tuple
> > > information so that I can iterate though it and check if any other
> tuple
> > > exist between these time stamps and add it to the group as well. Any
> > ideas?
> > >
> >
> >
> >
> > --
> > Zaki Rahaman
> >
>

Re: Need help with grouping.

Posted by zaki rahaman <za...@gmail.com>.
First off, is there a reason you're looking for overlapping intervals? This
seems to me like it would lead to a lot of doublecounting as tuples could be
a member of more than one group.

What is the end result you're looking for, not just the output, but what
"question" are you trying to answer?

On Wed, Nov 25, 2009 at 11:48 PM, dhaval deshpande <
dhaval.deshpande@gmail.com> wrote:

> Hi Zaki,
>              the myudf(timestamp) would still take tuple by tuple and I
> cannot compare each of them with others because I need to take all the
> tuples at once so that I can compare and then put the key and the the group
> which belong to the particular key in the bag and then return.
>
> Thanks,
> Dhaval.
>

Not really sure what you mean by this. Say your data was as follows.

name1, 1, 1-2
name2, 2, 1-3
name2, 3, 1-4
name3, 4, 1-5
name4, 5, 4-5
name5, 6, 2-3
name 6, 7, 9-12

etc. etc.

And your goal was to come up with intervals from X to Y, say 1 to 100. It
would help to know how big of a dataset you're working with and how many
groups/intervals you want to end up with approximately.

I would use the following approach, parts of which Alan/Dmitry highlighted
above.

1. Determine the size of the intervals. You could assume the timestamp pairs
are uniformly distributed (1-4 is as likely as 96-100 for example) and
simply divide up the absolute scale (determined by finding the min and max
values of timestamps by the number of groups/intervals desired. Lets call
this the equal-length interval approach. Intervals = 1-20, 21-40, 41-60,
61-80, 81-100 if you want 5 groups in the end for example.

2. If you want to distribute tuples fairly evenly among groups/intervals.
First, in one pass over the data, write a udf to evaluate the delta of each
time pair (so map 2-7 to 5 and 1-4 to 3 so on so forth). Group by this delta
and get the counts for each delta group. This should provide you with a sort
of histogram of the distribution of deltas. From this, choose intervals so
that you'll have approximately roughly the same amount of member tuples in
each interval. (You may have to create many more intervals if you find the
delta distribution is skewed towards small values or you can get by with
bigger values if its skewed the other direction. Call this the equal size
interval approach.

3. Now for a second pass over the data, write a udf that looks at the two
timestamp values and maps them to the appropriate pre-defined interval
(equal-length or equal size), emitting a tuple with a group label. So yes,
this function goes tuple by tuple, but since you've predefined the
intervals, this shouldn't cause any problems and all tuples will be properly
sorted. Finally group by the label and you'll have your groups.

Again, I'm really failing to grasp what exactly you want to accomplish here.



> On Wed, Nov 25, 2009 at 4:02 PM, Zaki Rahaman <zaki.rahaman@gmail.com
> >wrote:
>
> > You need to define the intervals for your groups, then write an EvalFunc
> to
> > parse your timestamps and add a new "group label" field to your tuples.
> > Afterwards you can group on these group labels. Your script might look
> > something like:
> >
> > A= LOAD 'data' AS (name:chararray, id:long, timestamp:chararray);
> >
> > B = FOREACH A GENERATE name, id, myudf(timestamp) as label;
> >
> > C = GROUP B by label;
> >
> > Or if you wan something more concise just
> >
> > B = GROUP A BY myudf(timestamp);
> >
> > If I'm not mistaken both will process as one MR job.
> >
> > Sent from my iPhone
> >
> >
> > On Nov 25, 2009, at 5:10 PM, dhaval deshpande <
> dhaval.deshpande@gmail.com>
> > wrote:
> >
> >  Hi Zaki,
> >>             My timestamp field is chararray ,  so pig will not recognize
> >> it as timestamps right? And it will not take care of it. I did exactly
> >> what
> >> you had told me before and then realized the same problem and then
> decided
> >> to write a UDF. But again had the problem of being handling only one
> tuple
> >> at a time as I had mentioned above.
> >>
> >> Thanks,
> >> Dhaval.
> >>
> >> On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman <zaki.rahaman@gmail.com
> >> >wrote:
> >>
> >>  It's a lot simpler than that... you simply have to tell pig to group
> your
> >>> data on the timestamp field... it takes care of the rest..
> >>>
> >>> something like..
> >>>
> >>> A = LOAD 'data' AS (name, id, timestamp);
> >>> B = GROUP A by timestamp.
> >>>
> >>> Yes, it's that easy.
> >>>
> >>> On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
> >>> dhaval.deshpande@gmail.com> wrote:
> >>>
> >>>  Hi,
> >>>>     I am back with a questions again :). This time i can explain
> better
> >>>> because I have explored little better than what i did last time :). I
> >>>>
> >>> have
> >>>
> >>>> three fields in my table. And they are name, id, time from and time to
> >>>> which
> >>>> tuple existed in the database. so for example 1-4 means tuple existed
> >>>>
> >>> from
> >>>
> >>>> 1sec to 4 sec in the database. So my table looks like this.
> >>>>
> >>>> name1 , 1, 1-4
> >>>> name2, 2, 1-6
> >>>> name3, 3, 2-4
> >>>> name4, 4, 3-7
> >>>> name5, 5, 4-6
> >>>>
> >>>> Now I want to group this table using my timestamp field. and I wanted
> my
> >>>> intended output to be like this
> >>>>
> >>>> (1-4, {(name1,1,1-4),(name3,3,2-4)})
> >>>> (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
> >>>> (2-4, {(name3, 3, 2-4)})
> >>>> (3-7, {(name4,4,3-7),(name5,5,4-6)})
> >>>> (4-6, {(name5,5,4-6)})
> >>>>
> >>>> But the problem I was facing was when I extend the EvalFunc class then
> I
> >>>> work with a single tuple at a time and I dont have all the other tuple
> >>>> information so that I can iterate though it and check if any other
> tuple
> >>>> exist between these time stamps and add it to the group as well. Any
> >>>>
> >>> ideas?
> >>>
> >>>>
> >>>>
> >>>
> >>>
> >>> --
> >>> Zaki Rahaman
> >>>
> >>>
>



-- 
Zaki Rahaman

Re: Need help with grouping.

Posted by dhaval deshpande <dh...@gmail.com>.
Hi Zaki,
              the myudf(timestamp) would still take tuple by tuple and I
cannot compare each of them with others because I need to take all the
tuples at once so that I can compare and then put the key and the the group
which belong to the particular key in the bag and then return.

Thanks,
Dhaval.

On Wed, Nov 25, 2009 at 4:02 PM, Zaki Rahaman <za...@gmail.com>wrote:

> You need to define the intervals for your groups, then write an EvalFunc to
> parse your timestamps and add a new "group label" field to your tuples.
> Afterwards you can group on these group labels. Your script might look
> something like:
>
> A= LOAD 'data' AS (name:chararray, id:long, timestamp:chararray);
>
> B = FOREACH A GENERATE name, id, myudf(timestamp) as label;
>
> C = GROUP B by label;
>
> Or if you wan something more concise just
>
> B = GROUP A BY myudf(timestamp);
>
> If I'm not mistaken both will process as one MR job.
>
> Sent from my iPhone
>
>
> On Nov 25, 2009, at 5:10 PM, dhaval deshpande <dh...@gmail.com>
> wrote:
>
>  Hi Zaki,
>>             My timestamp field is chararray ,  so pig will not recognize
>> it as timestamps right? And it will not take care of it. I did exactly
>> what
>> you had told me before and then realized the same problem and then decided
>> to write a UDF. But again had the problem of being handling only one tuple
>> at a time as I had mentioned above.
>>
>> Thanks,
>> Dhaval.
>>
>> On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman <zaki.rahaman@gmail.com
>> >wrote:
>>
>>  It's a lot simpler than that... you simply have to tell pig to group your
>>> data on the timestamp field... it takes care of the rest..
>>>
>>> something like..
>>>
>>> A = LOAD 'data' AS (name, id, timestamp);
>>> B = GROUP A by timestamp.
>>>
>>> Yes, it's that easy.
>>>
>>> On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
>>> dhaval.deshpande@gmail.com> wrote:
>>>
>>>  Hi,
>>>>     I am back with a questions again :). This time i can explain better
>>>> because I have explored little better than what i did last time :). I
>>>>
>>> have
>>>
>>>> three fields in my table. And they are name, id, time from and time to
>>>> which
>>>> tuple existed in the database. so for example 1-4 means tuple existed
>>>>
>>> from
>>>
>>>> 1sec to 4 sec in the database. So my table looks like this.
>>>>
>>>> name1 , 1, 1-4
>>>> name2, 2, 1-6
>>>> name3, 3, 2-4
>>>> name4, 4, 3-7
>>>> name5, 5, 4-6
>>>>
>>>> Now I want to group this table using my timestamp field. and I wanted my
>>>> intended output to be like this
>>>>
>>>> (1-4, {(name1,1,1-4),(name3,3,2-4)})
>>>> (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
>>>> (2-4, {(name3, 3, 2-4)})
>>>> (3-7, {(name4,4,3-7),(name5,5,4-6)})
>>>> (4-6, {(name5,5,4-6)})
>>>>
>>>> But the problem I was facing was when I extend the EvalFunc class then I
>>>> work with a single tuple at a time and I dont have all the other tuple
>>>> information so that I can iterate though it and check if any other tuple
>>>> exist between these time stamps and add it to the group as well. Any
>>>>
>>> ideas?
>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Zaki Rahaman
>>>
>>>

Re: Need help with grouping.

Posted by Zaki Rahaman <za...@gmail.com>.
You need to define the intervals for your groups, then write an  
EvalFunc to parse your timestamps and add a new "group label" field to  
your tuples. Afterwards you can group on these group labels. Your  
script might look something like:

A= LOAD 'data' AS (name:chararray, id:long, timestamp:chararray);

B = FOREACH A GENERATE name, id, myudf(timestamp) as label;

C = GROUP B by label;

Or if you wan something more concise just

B = GROUP A BY myudf(timestamp);

If I'm not mistaken both will process as one MR job.

Sent from my iPhone

On Nov 25, 2009, at 5:10 PM, dhaval deshpande <dhaval.deshpande@gmail.com 
 > wrote:

> Hi Zaki,
>              My timestamp field is chararray ,  so pig will not  
> recognize
> it as timestamps right? And it will not take care of it. I did  
> exactly what
> you had told me before and then realized the same problem and then  
> decided
> to write a UDF. But again had the problem of being handling only one  
> tuple
> at a time as I had mentioned above.
>
> Thanks,
> Dhaval.
>
> On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman  
> <za...@gmail.com>wrote:
>
>> It's a lot simpler than that... you simply have to tell pig to  
>> group your
>> data on the timestamp field... it takes care of the rest..
>>
>> something like..
>>
>> A = LOAD 'data' AS (name, id, timestamp);
>> B = GROUP A by timestamp.
>>
>> Yes, it's that easy.
>>
>> On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
>> dhaval.deshpande@gmail.com> wrote:
>>
>>> Hi,
>>>      I am back with a questions again :). This time i can explain  
>>> better
>>> because I have explored little better than what i did last  
>>> time :). I
>> have
>>> three fields in my table. And they are name, id, time from and  
>>> time to
>>> which
>>> tuple existed in the database. so for example 1-4 means tuple  
>>> existed
>> from
>>> 1sec to 4 sec in the database. So my table looks like this.
>>>
>>> name1 , 1, 1-4
>>> name2, 2, 1-6
>>> name3, 3, 2-4
>>> name4, 4, 3-7
>>> name5, 5, 4-6
>>>
>>> Now I want to group this table using my timestamp field. and I  
>>> wanted my
>>> intended output to be like this
>>>
>>> (1-4, {(name1,1,1-4),(name3,3,2-4)})
>>> (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
>>> (2-4, {(name3, 3, 2-4)})
>>> (3-7, {(name4,4,3-7),(name5,5,4-6)})
>>> (4-6, {(name5,5,4-6)})
>>>
>>> But the problem I was facing was when I extend the EvalFunc class  
>>> then I
>>> work with a single tuple at a time and I dont have all the other  
>>> tuple
>>> information so that I can iterate though it and check if any other  
>>> tuple
>>> exist between these time stamps and add it to the group as well. Any
>> ideas?
>>>
>>
>>
>>
>> --
>> Zaki Rahaman
>>

Re: Need help with grouping.

Posted by dhaval deshpande <dh...@gmail.com>.
Hi Zaki,
              My timestamp field is chararray ,  so pig will not recognize
it as timestamps right? And it will not take care of it. I did exactly what
you had told me before and then realized the same problem and then decided
to write a UDF. But again had the problem of being handling only one tuple
at a time as I had mentioned above.

Thanks,
Dhaval.

On Wed, Nov 25, 2009 at 2:57 PM, zaki rahaman <za...@gmail.com>wrote:

> It's a lot simpler than that... you simply have to tell pig to group your
> data on the timestamp field... it takes care of the rest..
>
> something like..
>
> A = LOAD 'data' AS (name, id, timestamp);
> B = GROUP A by timestamp.
>
> Yes, it's that easy.
>
> On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
> dhaval.deshpande@gmail.com> wrote:
>
> > Hi,
> >       I am back with a questions again :). This time i can explain better
> > because I have explored little better than what i did last time :). I
> have
> > three fields in my table. And they are name, id, time from and time to
> > which
> > tuple existed in the database. so for example 1-4 means tuple existed
> from
> > 1sec to 4 sec in the database. So my table looks like this.
> >
> > name1 , 1, 1-4
> > name2, 2, 1-6
> > name3, 3, 2-4
> > name4, 4, 3-7
> > name5, 5, 4-6
> >
> > Now I want to group this table using my timestamp field. and I wanted my
> > intended output to be like this
> >
> > (1-4, {(name1,1,1-4),(name3,3,2-4)})
> > (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
> > (2-4, {(name3, 3, 2-4)})
> > (3-7, {(name4,4,3-7),(name5,5,4-6)})
> > (4-6, {(name5,5,4-6)})
> >
> > But the problem I was facing was when I extend the EvalFunc class then I
> > work with a single tuple at a time and I dont have all the other tuple
> > information so that I can iterate though it and check if any other tuple
> > exist between these time stamps and add it to the group as well. Any
> ideas?
> >
>
>
>
> --
> Zaki Rahaman
>

Re: Need help with grouping.

Posted by zaki rahaman <za...@gmail.com>.
It's a lot simpler than that... you simply have to tell pig to group your
data on the timestamp field... it takes care of the rest..

something like..

A = LOAD 'data' AS (name, id, timestamp);
B = GROUP A by timestamp.

Yes, it's that easy.

On Wed, Nov 25, 2009 at 4:53 PM, dhaval deshpande <
dhaval.deshpande@gmail.com> wrote:

> Hi,
>       I am back with a questions again :). This time i can explain better
> because I have explored little better than what i did last time :). I have
> three fields in my table. And they are name, id, time from and time to
> which
> tuple existed in the database. so for example 1-4 means tuple existed from
> 1sec to 4 sec in the database. So my table looks like this.
>
> name1 , 1, 1-4
> name2, 2, 1-6
> name3, 3, 2-4
> name4, 4, 3-7
> name5, 5, 4-6
>
> Now I want to group this table using my timestamp field. and I wanted my
> intended output to be like this
>
> (1-4, {(name1,1,1-4),(name3,3,2-4)})
> (1-6, {(name1,1,1-4),(name2,2,1-6),(name3,3,2-4),(name5,5,4-6)})
> (2-4, {(name3, 3, 2-4)})
> (3-7, {(name4,4,3-7),(name5,5,4-6)})
> (4-6, {(name5,5,4-6)})
>
> But the problem I was facing was when I extend the EvalFunc class then I
> work with a single tuple at a time and I dont have all the other tuple
> information so that I can iterate though it and check if any other tuple
> exist between these time stamps and add it to the group as well. Any ideas?
>



-- 
Zaki Rahaman