incubator-crunch-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gauthier Ambard <gauthier.amb...@gmail.com>
Subject String concatenation
Date Mon, 30 Jul 2012 15:47:05 GMT
Hi all,

I am using crunch to process some texts (strings): once I have processed
them, I want to concatenate them.

I haven't found any method for that (concatenate strings) in crunch (or I
have miss it, which could be very possible). I thought it would be a good
idea to have a method for that in CombineFn and so I have made mine.

Have I missed an obvious/easier/already implemented/faster way to do it?
Have I made errors in my implementation?

Thanks
Gauthier

My code:
    public static final <K> CombineFn<K, String> STRING_CONCAT() {
        return aggregatorFactory(STRING_CONCAT);
    }

    public static class StringConcat implements Aggregator<String> {
        private StringBuilder sum = new StringBuilder();

        public void reset() {
            sum = new StringBuilder();
        }

        public void update(final String next) {
            sum.append(next).append(' ');
        }

        public Iterable<String> results() {
            return ImmutableList.of(sum.toString());
        }
    }

    public static AggregatorFactory<String> STRING_CONCAT = new
AggregatorFactory<String>() {
        public Aggregator<String> create() {
            return new StringConcat();
        }
    };

Mime
View raw message