@Override public void convert(Object from, BytesArray to) { // expect a tuple holding one field - chararray or bytearray Assert.isTrue(from instanceof SinkCall, String.format("Unexpected object type, expecting [%s], given [%s]", SinkCall.class, from.getClass())); // handle common cases SinkCall sinkCall = (SinkCall) from; Tuple rawTuple = sinkCall.getOutgoingEntry().getTuple(); if (rawTuple == null || rawTuple.isEmpty()) { to.bytes("{}"); return; } Assert.isTrue(rawTuple.size() == 1, "When using JSON input, only one field is expected"); // postpone the coercion Tuple tuple = CascadingUtils.coerceToString(sinkCall); super.convert(tuple.getObject(0), to); } }
@SuppressWarnings("unchecked") @Override public Result write(SinkCall<Object[], ?> sinkCall, Generator generator) { Tuple tuple = CascadingUtils.coerceToString(sinkCall); // consider names (in case of aliases these are already applied) List<String> names = (List<String>) sinkCall.getContext()[SINK_CTX_ALIASES]; generator.writeBeginObject(); for (int i = 0; i < tuple.size(); i++) { String name = (i < names.size() ? names.get(i) : "tuple" + i); // filter out fields if (shouldKeep(generator.getParentPath(), name)) { generator.writeFieldName(name); Object object = tuple.getObject(i); Result result = jdkWriter.write(object, generator); if (!result.isSuccesful()) { if (object instanceof Writable) { return writableWriter.write((Writable) object, generator); } return Result.FAILED(object); } } } generator.writeEndObject(); return Result.SUCCESFUL(); }
private boolean areNullsEqual() { try { Tuple tupleWithNull = Tuple.size( 1 ); return groupComparators[ 0 ].compare( tupleWithNull, tupleWithNull ) == 0; } catch( Exception exception ) { return true; // assume we have an npe or something and they don't expect to see nulls } }
public static Tuple nulledCopy( Fields declarator, Tuple tuple, Fields selector ) { // use tuple.size() in case declarator is UNKNOWN and selector has relative pos return tuple.nulledCopy( declarator.getPos( selector, tuple.size() ) ); }
static CoercibleType[] getCoercions( Fields fields, Tuple tuple ) { Type[] types = fields.types; // safe to not get a copy int size = fields.size(); size = size == 0 && tuple != null ? tuple.size() : size; if( size == 0 ) return EMPTY_COERCIONS; return Coercions.coercibleArray( size, types ); }
protected void performOperation( Tuple[] context, TupleEntry entry ) { if( context[ 0 ] == null ) context[ 0 ] = new Tuple(); if( context[ 0 ].size() < firstN ) context[ 0 ].add( entry.getTupleCopy() ); }
@Override public void prepare( FlowProcess flowProcess, OperationCall<Context> operationCall ) { operationCall.setContext( new Context( Tuple.size( 1 ), operationCall.getArgumentFields().subtract( Fields.FIRST ) ) ); }
public CombineInputPartitionTupleEntryIterator( FlowProcess<? extends Configuration> flowProcess, Fields sourceFields, Partition partition, String parentIdentifier, TupleEntrySchemeIterator childIterator ) { this.flowProcess = flowProcess; this.partition = partition; this.parentIdentifier = parentIdentifier; this.childIterator = childIterator; this.sourceFields = sourceFields; this.partitionEntry = new TupleEntry( partition.getPartitionFields(), Tuple.size( partition.getPartitionFields().size() ) ); }
@Override public void prepare( FlowProcess flowProcess, OperationCall<Pair<Matcher, TupleEntry>> operationCall ) { int size; if( groups != null ) size = groups.length; else size = operationCall.getDeclaredFields().size(); // if Fields.UNKNOWN size will be zero // TupleEntry allows us to honor the declared field type information TupleEntry entry = new TupleEntry( operationCall.getDeclaredFields(), Tuple.size( size ) ); operationCall.setContext( new Pair<>( getPattern().matcher( "" ), entry ) ); }
public static Tuple setOnEmpty( TupleEntry baseEntry, TupleEntry valuesEntry ) { Tuple emptyTuple = Tuple.size( baseEntry.getFields().size() ); emptyTuple.set( baseEntry.getFields(), valuesEntry.getFields(), valuesEntry.getTuple() ); return emptyTuple; }
@Override public Tuple aggregate( FlowProcess flowProcess, TupleEntry args, Tuple context ) { if( context == null ) context = Tuple.size( 2 ); if( include == Include.NO_NULLS && args.getObject( 0 ) == null ) return context; context.set( 0, context.getDouble( 0 ) + args.getDouble( 0 ) ); context.set( 1, context.getLong( 1 ) + 1 ); return context; }
@Override public void prepare( FlowProcess flowProcess, OperationCall<Pair<Pattern, TupleEntry>> operationCall ) { length = operationCall.getDeclaredFields().isUnknown() ? -1 : operationCall.getDeclaredFields().size(); TupleEntry tupleEntry = new TupleEntry( operationCall.getDeclaredFields(), Tuple.size( Math.max( 1, length ) ) ); operationCall.setContext( new Pair<>( getPattern(), tupleEntry ) ); }
@Override public void prepare( FlowProcess flowProcess, OperationCall<Pair<SimpleDateFormat, TupleEntry>> operationCall ) { operationCall.setContext( new Pair<>( getDateFormat(), new TupleEntry( operationCall.getDeclaredFields(), Tuple.size( getDeclaredSize() ) ) ) ); }
@Test public void testLeave() { Tuple aTuple = tuple.leave( new int[]{0} ); assertEquals( "not equal: tuple.size()", 1, tuple.size() ); assertEquals( "not equal: tuple.get( 0 )", "a", tuple.getObject( 0 ) ); assertEquals( "not equal: aTuple.size()", 4, aTuple.size() ); assertEquals( "not equal: aTuple.get( 0 )", "b", aTuple.getObject( 0 ) ); assertEquals( "not equal: tuple.get( 1 )", "c", aTuple.getObject( 1 ) ); }
@Test public void testGet() { Tuple aTuple = tuple.get( new int[]{0} ); assertEquals( "not equal: aTuple.size()", 1, aTuple.size() ); assertEquals( "not equal: aTuple.get( 0 )", "a", aTuple.getObject( 0 ) ); assertEquals( "not equal: tuple.size()", 5, tuple.size() ); assertEquals( "not equal: tuple.get( 0 )", "a", tuple.getObject( 0 ) ); assertEquals( "not equal: tuple.get( 1 )", "b", tuple.getObject( 1 ) ); }
@Test public void testGetNull() { Tuple aTuple = tuple.get( (int[]) null ); assertEquals( "not equal: aTuple.size()", 5, aTuple.size() ); assertEquals( "not equal: aTuple.get( 0 )", "a", aTuple.getObject( 0 ) ); assertEquals( "not equal: aTuple.get( 1 )", "b", aTuple.getObject( 1 ) ); assertEquals( "not equal: tuple.size()", 5, tuple.size() ); assertEquals( "not equal: tuple.get( 0 )", "a", tuple.getObject( 0 ) ); assertEquals( "not equal: tuple.get( 1 )", "b", tuple.getObject( 1 ) ); }
@Test public void testGet() throws Exception { TupleEntry entry = new TupleEntry( new Fields( "json", JSONCoercibleType.TYPE ), Tuple.size( 1 ) ); entry.setObject( 0, JSONData.nested ); JSONGetFunction function = new JSONGetFunction( new Fields( "result" ), "/person/name" ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNotNull( value ); assertEquals( "John Doe", ( (TextNode) value ).textValue() ); }
@Test public void testSelectNotComparable() { Fields selector = new Fields( 1, "d" ); Object object = new Object(); TupleEntry entryA = new TupleEntry( new Fields( "a", "b" ), new Tuple( "a", object ) ); TupleEntry entryB = new TupleEntry( new Fields( "c", "d" ), new Tuple( "c", "d" ) ); Tuple tuple = TupleEntry.select( selector, entryA, entryB ); assertEquals( "wrong size", 2, tuple.size() ); assertEquals( "not equal: tuple.get(0)", object, tuple.getObject( 0 ) ); assertEquals( "not equal: tuple.get(1)", "d", tuple.getObject( 1 ) ); }
@Test public void testPut() { Fields aFields = new Fields( "one", "five" ); tuple.put( fields, aFields, new Tuple( "ten", "eleven" ) ); assertEquals( "not equal: tuple.size()", 5, tuple.size() ); assertEquals( "not equal: tuple.get( 0 )", "ten", tuple.getObject( 0 ) ); assertEquals( "not equal: tuple.get( 0 )", "ten", tuple.get( fields, new Fields( "one" ) ).getObject( 0 ) ); assertEquals( "not equal: tuple.get( 0 )", "eleven", tuple.getObject( 4 ) ); assertEquals( "not equal: tuple.get( 0 )", "eleven", tuple.get( fields, new Fields( "five" ) ).getObject( 0 ) ); }