| 45 | import com.google.common.base.Charsets; |
| 46 | |
| 47 | public class PigStreamingUDF extends PigStreamingBase { |
| 48 | private static final byte PRE_WRAP_DELIM = '|'; |
| 49 | private static final byte POST_WRAP_DELIM = '_'; |
| 50 | private static final StreamingDelimiters DELIMS = |
| 51 | new StreamingDelimiters(PRE_WRAP_DELIM, POST_WRAP_DELIM, false); |
| 52 | |
| 53 | private FieldSchema topLevelFs; |
| 54 | private static TupleFactory tupleFactory = TupleFactory.getInstance(); |
| 55 | private static BagFactory bagFactory = BagFactory.getInstance(); |
| 56 | |
| 57 | private WritableByteArray out; |
| 58 | |
| 59 | public PigStreamingUDF() { |
| 60 | out = new WritableByteArray(); |
| 61 | } |
| 62 | |
| 63 | public PigStreamingUDF(FieldSchema topLevelFs) { |
| 64 | out = new WritableByteArray(); |
| 65 | this.topLevelFs = topLevelFs; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public WritableByteArray serializeToBytes(Tuple t) throws IOException { |
| 70 | out.reset(); |
| 71 | int sz; |
| 72 | Object field; |
| 73 | if (t == null) { |
| 74 | sz = 0; |
| 75 | } else { |
| 76 | sz = t.size(); |
| 77 | } |
| 78 | for (int i=0; i < sz; i++) { |
| 79 | field = t.get(i); |
| 80 | StorageUtil.putField(out, field, DELIMS, true); |
| 81 | if (i != sz-1) { |
| 82 | out.write(DELIMS.getParamDelim()); |
| 83 | } |
| 84 | } |
| 85 | byte[] recordDel = DELIMS.getRecordEnd(); |
| 86 | out.write(recordDel, 0, recordDel.length); |
| 87 | return out; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public LoadCaster getLoadCaster() throws IOException { |
| 92 | return new Utf8StorageConverter(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Tuple deserialize(byte[] bytes, int offset, int length) throws IOException { |
| 97 | Object o = deserialize(topLevelFs, bytes, 0 + offset, length - DELIMS.getRecordEnd().length); //Drop newline |
| 98 | return tupleFactory.newTuple(o); |
| 99 | } |
| 100 | |
| 101 | public byte[] getRecordDelim() { |
| 102 | return DELIMS.getRecordEnd(); |
| 103 | } |
| 104 |
nothing calls this directly
no test coverage detected