Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PrependToBag |
|
| 2.5;2.5 |
1 | /* | |
2 | * Copyright 2010 LinkedIn, Inc | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not | |
5 | * use this file except in compliance with the License. You may obtain a copy of | |
6 | * the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
13 | * License for the specific language governing permissions and limitations under | |
14 | * the License. | |
15 | */ | |
16 | ||
17 | package datafu.pig.bags; | |
18 | ||
19 | import java.io.IOException; | |
20 | ||
21 | import org.apache.pig.data.BagFactory; | |
22 | import org.apache.pig.data.DataBag; | |
23 | import org.apache.pig.data.DataType; | |
24 | import org.apache.pig.data.Tuple; | |
25 | import org.apache.pig.impl.logicalLayer.FrontendException; | |
26 | import org.apache.pig.impl.logicalLayer.schema.Schema; | |
27 | ||
28 | import datafu.pig.util.SimpleEvalFunc; | |
29 | ||
30 | /** | |
31 | * Prepends a tuple to a bag. N.B. this copies the entire input bag, so don't use it for large bags. | |
32 | * <p> | |
33 | * Example: | |
34 | * <pre> | |
35 | * {@code | |
36 | * define PrependToBag datafu.pig.bags.PrependToBag(); | |
37 | * | |
38 | * -- input: | |
39 | * -- ({(1),(2),(3)},(4)) | |
40 | * -- ({(10),(20),(30),(40),(50)},(60)) | |
41 | * input = LOAD 'input' AS (B: bag{T: tuple(v:INT)}, T: tuple(v:INT)); | |
42 | ||
43 | * -- output: | |
44 | * -- ({(4),(1),(2),(3)}) | |
45 | * -- ({(60),(10),(20),(30),(40),(50)}) | |
46 | * output = FOREACH input GENERATE PrependToBag(B,T) as B; | |
47 | * } | |
48 | * </pre> | |
49 | */ | |
50 | 817 | public class PrependToBag extends SimpleEvalFunc<DataBag> |
51 | { | |
52 | public DataBag call(DataBag inputBag, Tuple t) throws IOException | |
53 | { | |
54 | 2 | DataBag outputBag = BagFactory.getInstance().newDefaultBag(); |
55 | 2 | outputBag.add(t); |
56 | 2 | for (Tuple x : inputBag) |
57 | 8 | outputBag.add(x); |
58 | 2 | return outputBag; |
59 | } | |
60 | ||
61 | @Override | |
62 | public Schema outputSchema(Schema input) | |
63 | { | |
64 | try { | |
65 | 125 | return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), |
66 | input.getField(0).schema, DataType.BAG)); | |
67 | } | |
68 | 0 | catch (FrontendException e) { |
69 | 0 | return null; |
70 | } | |
71 | } | |
72 | } |