2 * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
3 * See accompanying LICENSE file.
5 package com
.yahoo
.pig
.builtin
;
7 import java
.io
.IOException
;
8 import java
.util
.Random
;
10 import com
.yahoo
.pig
.GroupFunc
;
11 import com
.yahoo
.pig
.data
.Datum
;
12 import com
.yahoo
.pig
.data
.Tuple
;
14 public class GFCross
extends GroupFunc
{
15 int numInputs
, myNumber
, numGroupsPerInput
;
17 public static int DEFAULT_PARALLELISM
= 96;
19 public Datum
[] exec(Tuple input
) {
22 numInputs
= Integer
.parseInt(input
.getAtomField(0).strval());
23 myNumber
= Integer
.parseInt(input
.getAtomField(1).strval());
26 numGroupsPerInput
= (int) Math
.ceil(Math
.pow(DEFAULT_PARALLELISM
, 1.0/numInputs
));
27 int numGroupsGoingTo
= (int) Math
.pow(numGroupsPerInput
,numInputs
- 1);
29 int[] digits
= new int[numInputs
];
30 for (int i
=0; i
<numInputs
; i
++){
32 Random r
= new Random(System
.currentTimeMillis());
33 digits
[i
] = r
.nextInt(numGroupsPerInput
);
39 Tuple
[] groups
= new Tuple
[numGroupsGoingTo
];
40 for (int i
=0; i
<numGroupsGoingTo
; i
++){
41 groups
[i
] = toTuple(digits
);
46 }catch(IOException e
){
47 RuntimeException re
= new RuntimeException(e
.getMessage());
48 re
.setStackTrace(e
.getStackTrace());
53 private Tuple
toTuple(int[] digits
) throws IOException
{
54 Tuple t
= new Tuple(numInputs
);
55 for (int i
=0; i
<numInputs
; i
++){
56 t
.setField(i
, digits
[i
]);
61 private void next(int[] digits
){
62 for (int i
=0; i
<numInputs
; i
++){
66 if (digits
[i
] == numGroupsPerInput
- 1){