private void onGivenGroups( FunctionCall<Pair<Matcher, TupleEntry>> functionCall, Matcher matcher, TupleEntry output ) { for( int i = 0; i < groups.length; i++ ) output.setString( i, matcher.group( groups[ i ] ) ); functionCall.getOutputCollector().add( output ); }
@Override public void operate( FlowProcess flowProcess, FunctionCall<Pair<Matcher, TupleEntry>> functionCall ) { // coerce to string String value = functionCall.getArguments().getString( 0 ); // make safe if( value == null ) value = ""; TupleEntry output = functionCall.getContext().getRhs(); Matcher matcher = functionCall.getContext().getLhs().reset( value ); if( replaceAll ) output.setString( 0, matcher.replaceAll( replacement ) ); else output.setString( 0, matcher.replaceFirst( replacement ) ); functionCall.getOutputCollector().add( output ); }
private void setGroupsOnTuple( Matcher matcher, TupleEntry output, int count ) { if( count == 0 ) { try { output.setString( 0, matcher.group( 0 ) ); } catch( Exception exception ) { throw new CascadingException( "unable to set tuple value at field: " + output.getFields().get( 0 ) + ", from regex group: 0", exception ); } } else { for( int i = 0; i < count; i++ ) { try { output.setString( i, matcher.group( i + 1 ) ); // skip group 0 } catch( Exception exception ) { throw new CascadingException( "unable to set tuple value at field: " + output.getFields().get( i ) + ", from regex group: " + ( i + 1 ), exception ); } } } }
protected void sourceHandleInput( SourceCall<Object[], RecordReader> sourceCall ) throws IOException { TupleEntry result = sourceCall.getIncomingEntry(); int index = 0; Object[] context = sourceCall.getContext(); // coerce into canonical forms if( getSourceFields().size() == 2 ) result.setLong( index++, ( (LongWritable) context[ 0 ] ).get() ); result.setString( index, makeEncodedString( context ) ); }
protected void sourceHandleInput( SourceCall<Object[], RecordReader> sourceCall ) throws IOException { TupleEntry result = sourceCall.getIncomingEntry(); int index = 0; Object[] context = sourceCall.getContext(); // coerce into canonical forms if( getSourceFields().size() == 2 ) result.setLong( index++, ( (LongWritable) context[ 0 ] ).get() ); result.setString( index, makeEncodedString( context ) ); }
@Override public void operate( FlowProcess flowProcess, FunctionCall<Pair<SimpleDateFormat, TupleEntry>> functionCall ) { long ts = functionCall.getArguments().getLong( 0 ); Calendar calendar = getCalendar(); calendar.setTimeInMillis( ts ); String formatted = functionCall.getContext().getLhs().format( calendar.getTime() ); functionCall.getContext().getRhs().setString( 0, formatted ); functionCall.getOutputCollector().add( functionCall.getContext().getRhs() ); } }
@Override public void operate( FlowProcess flowProcess, FunctionCall<Pair<Matcher, TupleEntry>> functionCall ) { String value = functionCall.getArguments().getString( 0 ); if( value == null ) value = ""; Matcher matcher = functionCall.getContext().getLhs().reset( value ); while( matcher.find() ) { functionCall.getContext().getRhs().setString( 0, matcher.group() ); functionCall.getOutputCollector().add( functionCall.getContext().getRhs() ); } } }
@Override public boolean source( FlowProcess<? extends Properties> flowProcess, SourceCall<LineNumberReader, InputStream> sourceCall ) throws IOException { // first line is 0, this matches offset being zero, so when throwing out the first line for comments int lineNumber = sourceCall.getContext().getLineNumber(); String line = sourceCall.getContext().readLine(); if( line == null ) return false; TupleEntry incomingEntry = sourceCall.getIncomingEntry(); if( getSourceFields().size() == 1 ) { incomingEntry.setObject( 0, line ); } else { incomingEntry.setInteger( 0, lineNumber ); incomingEntry.setString( 1, line ); } return true; }
@Override public void operate( FlowProcess flowProcess, FunctionCall<Pair<Pattern, TupleEntry>> functionCall ) { String value = functionCall.getArguments().getString( 0 ); if( value == null ) value = ""; String[] split = functionCall.getContext().getLhs().split( value ); for( String string : split ) { TupleEntry tupleEntry = functionCall.getContext().getRhs(); tupleEntry.setString( 0, string ); functionCall.getOutputCollector().add( tupleEntry ); } } }
@Test public void testCoerceIterable() { final SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MMM/yyyy:HH:mm:ss:SSS Z" ); CoercibleType coercible = new DateType( "dd/MMM/yyyy:HH:mm:ss:SSS Z", TimeZone.getDefault() ); Date date = new Date(); String stringDate = dateFormat.format( date ); Tuple tuple = Tuple.size( 4 ); Fields fields = Fields.size( 4 ).applyTypes( coercible, coercible, coercible, String.class ); TupleEntry results = new TupleEntry( fields, tuple ); results.setObject( 0, date ); results.setLong( 1, date.getTime() ); results.setString( 2, stringDate ); results.setString( 3, stringDate ); Iterable<String> iterable = results.asIterableOf( String.class ); int count = 0; for( String s : iterable ) { assertEquals( stringDate, s ); count++; } assertEquals( count, results.size() ); }