Help with Generics for StringInvert Fuction

9 views
Skip to first unread message

m.neuma...@gmail.com

unread,
May 31, 2014, 11:04:56 AM5/31/14
to stratosp...@googlegroups.com
Hej,

After getting some help with the generics in my Count code I have one that's a bit more advanced.

I use String identifier in most of my jobs unfortunately most of them are URL so they have a long Prefix. Since Stratophere does Prefixsort I need to invert the Strings for optimal performance. (Jobs with inverted Strings run 4 times faster on my setting)

For DataSet of Strings I use the following Funktion:

private static class invertMap extends MapFunction<String, String> {
StringBuilder build = new StringBuilder();
private static final long serialVersionUID = 1L;
@Override
public String map(String value) throws Exception {
build.setLength(0);
return build.reverse().toString();
}
}
public static DataSet<String> invertKeys(DataSet<String> keys){
return keys.map(new invertMap());
}

I would like to write something similar that takes an arbitrary Tuple as input together with the field ID that contains String to be inverted. For a Tuple2 it would look the following:

private static class invertInTupleMap extends
MapFunction<Tuple2<String, String>, Tuple2<String, String>> {
private StringBuilder build = new StringBuilder();
private final int pos;

public invertInTupleMap(int pos) {
this.pos = pos;
}

@Override
public Tuple2<String, String> map(Tuple2<String, String> value)
throws Exception {
build.setLength(0);
build.append(value.getField(pos)).reverse();
value.setField(build.toString(), pos);
return value;
}
}

public static DataSet<Tuple2<String,String>> invertKeysInTuple(DataSet<Tuple2<String,String>> keys, int pos) {
return keys.map(new invertInTupleMap(pos));
}

QUESTION1: How would I make the call Generic so I can feed it any kind of Tuple with any kind of Fields in it (besides the Field at pos that needs to be String for obvious reasons). All ways I tried so far gave me lots of red curly lines with no useful explanation.
Is this possible at all?

cheers Martin

Fabian Hueske

unread,
May 31, 2014, 11:24:48 AM5/31/14
to stratosp...@googlegroups.com
Hej Martin,

the following code should work.

Best, Fabian

------

DataSet<Tuple2<String, Integer>> myData = ...
DataSet<Tuple2<String, Integer>> inverted = myData.map(new Inverter<Tuple2<String,Integer>>(0));

public static final class Inverter<T extends Tuple> extends MapFunction<T, T> {

        private int fieldId;
        private StringBuilder sb;
       
        public Inverter(int fieldId) {
            this.sb = new StringBuilder();
            this.fieldId = fieldId;
        }
       
        @Override
        public T map(T value) throws Exception {
            String sVal = ((Tuple)value).getField(this.fieldId);
            sb.setLength(0);
            sb.append(sVal);
            sb.reverse();
            ((Tuple)value).setField(sb.toString(), fieldId);
            return value;
        }
       
    }



--
You received this message because you are subscribed to the Google Groups "stratosphere-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stratosphere-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/stratosphere-dev.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages