private static Tuple transformToTuple(List<?> objList, HCatSchema hs) throws Exception { if (objList == null) { return null; } Tuple t = tupFac.newTuple(objList.size()); List<HCatFieldSchema> subFields = hs.getFields(); for (int i = 0; i < subFields.size(); i++) { t.set(i, extractPigObject(objList.get(i), subFields.get(i))); } return t; }
@Override public Tuple exec(Tuple input) throws IOException { // Since Initial is guaranteed to be called // only in the map, it will be called with an // input of a bag with a single tuple - the // count should always be 1 if bag is non empty DataBag bag = (DataBag)input.get(0); return mTupleFactory.newTuple(bag.iterator().hasNext()? Long.valueOf(1L) : Long.valueOf(0L)); } }
@Override public Tuple call(Tuple input) throws Exception { Tuple output = TupleFactory.getInstance() .newTuple(input.getAll().size() - 2); for (int i = 1; i < input.getAll().size() - 2; i ++) { output.set(i, input.get(i+2)); } long offset = calculateOffset((Integer) input.get(0)); output.set(0, offset + (Long)input.get(2)); return output; }
@Override public Tuple exec(Tuple input) throws IOException { if (input == null || input.size() == 0) { return null; } DataBag db = (DataBag) input.get(0); Iterator<Tuple> iter = db.iterator(); Tuple output = tupleFactory.newTuple(); while (iter.hasNext()) { Tuple t = iter.next(); for (Object o : t.getAll()) { output.append(o); } } return output; }
@Override public Tuple exec(Tuple tuple) throws IOException { if (tuple == null || tuple.size() != 2 || tuple.getType(1) != DataType.BYTEARRAY) { throw new IOException("" + this.getClass().getName() + " expects 2 arguments, 2nd must be a bytearray"); } String rowId = (String) tuple.get(0); DataByteArray protoDBA = (DataByteArray) tuple.get(1); byte[] protoBytes = protoDBA.get(); DocumentWrapper doc = DocumentProtos.DocumentWrapper.parseFrom(protoBytes); Tuple result = tupleFactory.newTuple(); result.append(rowId); result.append(doc.getDocumentMetadata().getKey()); result.append(protoDBA); return result; } }
@Override public Tuple exec(Tuple input) throws IOException { // the input has a single field which is a tuple // representing the data we want to distinct. // unwrap, put in a bag and send down try { Tuple single = (Tuple)input.get(0); DataBag bag = single == null ? createDataBag() : new SingleTupleBag(single); return tupleFactory.newTuple(bag); } catch (ExecException e) { throw e; } } }
Tuple GetJoinInput(Tuple group, List<Integer> groupCols0, List<Integer> groupCols, int numFields) throws ExecException { Tuple t = TupleFactory.getInstance().newTuple(numFields); if (groupCols.size() == 1) { // GroupLabel would be a data atom t.set(groupCols.get(0), group.get(groupCols0.get(0))); } else { if (!(group instanceof Tuple)) throw new RuntimeException("Unrecognized group label!"); for (int i = 0; i < groupCols.size(); i++) { t.set(groupCols.get(i), group.get(groupCols0.get(i))); } } return t; }
private void recursivelyCube(List<Tuple> result, Tuple input, int index, Tuple newt) throws ExecException { newt.set(index, input.get(index)); if (index == input.size() - 1 ) { result.add(newt); } else { recursivelyCube(result, input, index + 1, newt); } // tf.newTuple makes a copy. tf.newTupleNoCopy doesn't. Tuple newnewt = tf.newTuple(newt.getAll()); newnewt.set(index, allMarker); if (index == input.size() - 1) { result.add(newnewt); } else { recursivelyCube(result, input, index + 1, newnewt); } }
@Override public void accumulate(Tuple arg0) throws IOException { DataBag inputBag = (DataBag)arg0.get(0); for (Tuple t : inputBag) { Tuple t1 = TupleFactory.getInstance().newTuple(t.getAll()); t1.append(i); outputBag.add(t1); if (count % 1000000 == 0) { outputBag.spill(); count = 0; } i++; count++; } }
@Override public Tuple exec(Tuple input) throws IOException { // Since Initial is guaranteed to be called // only in the map, it will be called with an // input of a bag with a single tuple - the // count should always be 1 if bag is non empty DataBag bag = (DataBag)input.get(0); Iterator it = bag.iterator(); if (it.hasNext()){ Tuple t = (Tuple)it.next(); if (t != null && t.size() > 0 && t.get(0) != null) return mTupleFactory.newTuple(Long.valueOf(1)); } return mTupleFactory.newTuple(Long.valueOf(0)); } }
Tuple GetJoinInput(Tuple group, List<Integer> groupCols, int numFields) throws ExecException { Tuple t = TupleFactory.getInstance().newTuple(numFields); if (groupCols.size() == 1) { // GroupLabel would be a data atom t.set(groupCols.get(0), group); } else { if (!(group instanceof Tuple)) throw new RuntimeException("Unrecognized group label!"); for (int i = 0; i < groupCols.size(); i++) { t.set(groupCols.get(i), group.get(i)); } } return t; }
|| projectedColumns.length != columnValues.size()) { return null; } int numColumns = columnValues.size(); Tuple tuple = TUPLE_FACTORY.newTuple(numColumns); try { int i = 0; Object object = entry.getValue(); if (object == null) { tuple.set(i++, null); continue; case DataType.BYTEARRAY: byte[] bytes = PDataType.fromTypeId(PBinary.INSTANCE.getSqlType()).toBytes(object); tuple.set(i, new DataByteArray(bytes, 0, bytes.length)); break; case DataType.CHARARRAY: tuple.set(i, DataType.toString(object)); break; case DataType.DOUBLE: Tuple t = TUPLE_FACTORY.newTuple(array.getDimensions());; for(int j = 0 ; j < array.getDimensions() ; j++) { t.set(j,array.getElement(j));
@Override public Tuple exec(Tuple input) throws ExecException{ String title = (String) input.get(0); DataBag kwds = (DataBag) input.get(1); List<String> l = new ArrayList<String>(); for(Tuple kwd : kwds){ l.add((String) kwd.get(0)); } Collections.sort(l); Tuple t = TupleFactory.getInstance().newTuple(); t.append(title + " " + Joiner.on(" ").join(l)); return t; } }
Tuple GetGroupByInput(Object groupLabel, List<Integer> groupCols, int numFields) throws ExecException { Tuple t = TupleFactory.getInstance().newTuple(numFields); if (groupCols.size() == 1) { // GroupLabel would be a data atom t.set(groupCols.get(0), groupLabel); } else { if (!(groupLabel instanceof Tuple)) throw new RuntimeException("Unrecognized group label!"); Tuple group = (Tuple) groupLabel; for (int i = 0; i < groupCols.size(); i++) { t.set(groupCols.get(i), group.get(i)); } } return t; }
/** * Test Use the udf to reserve multiple tuples * * @throws Exception */ @Test public void testMultipleTuples() throws Exception { Tuple tuple = tupleFactory.newTuple(2); tuple.set(0, 2L); tuple.set(1, SEQUENCE_NAME); final String tentantId = conn.getClientInfo(PhoenixRuntime.TENANT_ID_ATTRIB); ReserveNSequence udf = new ReserveNSequence(zkQuorum, tentantId); for (int i = 0; i < 2; i++) { udf.exec(tuple); } long nextValue = getNextSequenceValue(conn); assertEquals(5L, nextValue); }
@Override public Tuple exec(Tuple tuple) throws IOException { if (tuple == null) { throw new IOException("" + this.getClass().getName() +": null tuple has been passed to UDF!"); } Tuple ret = tupleFactory.newTuple(); for(int i=1;i<(tuple.size()-1);i++){ ret.append(tuple.get(i)); } ret.append(tuple.get(0)); return ret; } }
public DataBag exec(Tuple input) throws IOException { DataBag outputBag = bagFactory.newDefaultBag(); String idBase = (String)input.get(0); for (int k=0; k < numKeys; k++) { String key = idBase+k; int key_bucket = random.nextInt(maxRandom); Tuple next = tupleFactory.newTuple(2); next.set(0, key); next.set(1, key_bucket); outputBag.add(next); } return outputBag; }