Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AppendToBag |
|
| 2.0;2 |
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.DataBag; | |
22 | import org.apache.pig.data.DataType; | |
23 | import org.apache.pig.data.Tuple; | |
24 | import org.apache.pig.impl.logicalLayer.FrontendException; | |
25 | import org.apache.pig.impl.logicalLayer.schema.Schema; | |
26 | ||
27 | import datafu.pig.util.SimpleEvalFunc; | |
28 | ||
29 | /** | |
30 | * Appends a tuple to a bag. | |
31 | * <p> | |
32 | * Example: | |
33 | * <pre> | |
34 | * {@code | |
35 | * define AppendToBag datafu.pig.bags.AppendToBag(); | |
36 | * | |
37 | * -- input: | |
38 | * -- ({(1),(2),(3)},(4)) | |
39 | * -- ({(10),(20),(30),(40),(50)},(60)) | |
40 | * input = LOAD 'input' AS (B: bag{T: tuple(v:INT)}, T: tuple(v:INT)); | |
41 | ||
42 | * -- output: | |
43 | * -- ({(1),(2),(3),(4)}) | |
44 | * -- ({(10),(20),(30),(40),(50),(60)}) | |
45 | * output = FOREACH input GENERATE AppendToBag(B,T) as B; | |
46 | * } | |
47 | * </pre> | |
48 | */ | |
49 | 1279 | public class AppendToBag extends SimpleEvalFunc<DataBag> |
50 | { | |
51 | public DataBag call(DataBag inputBag, Tuple t) throws IOException | |
52 | { | |
53 | 2 | inputBag.add(t); |
54 | 2 | return inputBag; |
55 | } | |
56 | ||
57 | @Override | |
58 | public Schema outputSchema(Schema input) | |
59 | { | |
60 | try { | |
61 | 277 | return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), |
62 | input.getField(0).schema, DataType.BAG)); | |
63 | } | |
64 | 0 | catch (FrontendException e) { |
65 | 0 | return null; |
66 | } | |
67 | } | |
68 | } |