@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(); }
/** * Method posToObject returns the object at the given tuple position. * * @param pos the ordinal position to select from * @param <R> the object type * @return the value in the given ordinal position */ @SuppressWarnings("unchecked") public static <R> Function<Tuple, ? extends R> posToObject( int pos ) { return value -> (R) value.getObject( pos ); }
/** * Method getObject returns the value in the given position pos. * <p> * No coercion is performed if there is an associated coercible type. * * @param pos position of the element to return. * @return Object */ public Object getObject( int pos ) { return tuple.getObject( pos ); }
@Override public Object get( int index ) { for( int i = 0; i < basePos.length; i++ ) { if( index < skip[ i + 1 ] ) return bases[ i ].getObject( basePos[ i ][ index - skip[ i ] ] ); } throw new IllegalArgumentException( "invalid index: " + index + ", length: " + basePos.length ); }
@Property(name = "firstValue", visibility = Visibility.PRIVATE) public Serializable getFirstValue() { return (Serializable) values[ 0 ].getObject( 0 ); }
/** * Method getBoolean returns the element at the given position as a boolean. If the value is (case ignored) the * string 'true', a {@code true} value will be returned. {@code false} if null. * * @param pos of type int * @return boolean */ public boolean getBoolean( int pos ) { return Coercions.BOOLEAN.coerce( getObject( pos ) ); }
/** * Method getShort returns the element at the given position as an short. Zero if null. * * @param pos of type int * @return long */ public short getShort( int pos ) { return Coercions.SHORT.coerce( getObject( pos ) ); }
@Override public Tuple complete( FlowProcess flowProcess, Tuple context ) { context.set( 0, canonical.canonical( context.getObject( 0 ) ) ); return context; } }
@Test public void testRemove2() { Tuple aTuple = tuple.remove( new int[]{2, 4} ); assertEquals( "not equal: aTuple.size()", 2, aTuple.size() ); assertEquals( "not equal: aTuple.get( 0 )", "c", aTuple.getObject( 0 ) ); assertEquals( "not equal: tuple.size()", 3, tuple.size() ); assertEquals( "not equal: tuple.get( 0 )", "a", tuple.getObject( 0 ) ); assertEquals( "not equal: tuple.get( 1 )", "b", tuple.getObject( 1 ) ); assertEquals( "not equal: tuple.get( 1 )", "d", tuple.getObject( 2 ) ); }
@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 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 ) ); }
@Test public void testRemove() { Fields aFields = new Fields( "one" ); Tuple aTuple = tuple.remove( fields, aFields ); assertEquals( "not equal: aTuple.size()", 1, aTuple.size() ); assertEquals( "not equal: aTuple.get( 0 )", "a", aTuple.getObject( 0 ) ); fields = fields.subtract( aFields ); assertEquals( "not equal: tuple.size()", 4, tuple.size() ); assertEquals( "not equal: tuple.get( 0 )", "b", tuple.get( fields, new Fields( "two" ) ).getObject( 0 ) ); assertEquals( "not equal: tuple.get( 1 )", "c", tuple.get( fields, new Fields( "three" ) ).getObject( 0 ) ); }
@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 testSelect() { Fields selector = new Fields( "a", "d" ); TupleEntry entryA = new TupleEntry( new Fields( "a", "b" ), new Tuple( "a", "b" ) ); 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)", "a", tuple.getObject( 0 ) ); assertEquals( "not equal: tuple.get(1)", "d", tuple.getObject( 1 ) ); }
@Test public void testGetMissing() 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/foobar" ); TupleListCollector result = invokeFunction( function, entry, new Fields( "result" ) ); Object value = result.iterator().next().getObject( 0 ); assertNull( value ); }
@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() ); }