crunch-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Danny Morgan <>
Subject RE: Trouble with Avro records
Date Thu, 21 Aug 2014 20:46:18 GMT
Thanks Josh, if it is any consolation Crunch has saved me countless hours and cpu cycles over
how we used to do things so I'm glad to be benefiting from your ill-advised decisions :-)


Date: Thu, 21 Aug 2014 13:33:40 -0700
Subject: Re: Trouble with Avro records

On Thu, Aug 21, 2014 at 1:28 PM, Danny Morgan <> wrote:

Awesome adding:


Did the trick, thanks Josh!

Do you mind clarifying when to use  Avros.records() 
versus Avros.specifics() or Avros.reflects()?

Oops, yes-- sorry about that. The records() method is defined on both the WritableTypeFamily
and the AvroTypeFamily as a method that is supposed to handle "arbitrary" data types that
aren't supported by the built-in POJO and Crunch Tuple methods. Fortunately/unfortunately,
the arbitrary data types depends on the backing implementation: for Writables, records() only
really supports classes that implement Writable, and for Avros, records() checks to see if
the class implements SpecificRecord (at which point it handles it using Avros.specifics) and
if it does not, passes it on to Avros.reflects.

I think that, like many thinks in Crunch, records() reflects an early and somewhat ill-advised
decision I made when I was first creating the API that I would likely do away with if I had
it to do over again. :)


Thanks Again!


Date: Thu, 21 Aug 2014 13:20:20 -0700
Subject: Re: Trouble with Avro records

Okay. I suspect this is the problem:

Gabriel fixed this for the upcoming 0.11 release, but there are a couple of workarounds in
the comments. One is to put the avro jar into your job jar file, instead of relying on the
one that is in the hadoop lib. The other is to configure AvroMode.setSpecificClassLoader w/the
class loader for your Visit class before kicking off the job.


On Thu, Aug 21, 2014 at 1:10 PM, Danny Morgan <> wrote:

Hi Josh,



Date: Thu, 21 Aug 2014 13:04:29 -0700
Subject: Re: Trouble with Avro records


That feels like an AvroMode-related exception; which version of Crunch are you using?


On Thu, Aug 21, 2014 at 12:45 PM, Danny Morgan <> wrote:

Hi Guys,

Love crunch but having some trouble recently using Avro records. I think someone needs to
write a Crunch book.

I'm trying to aggregate hourly visits to a page by each user. I do one pass to parse the records
and then I try to "group by" unique users and hour and count the number of times they visited
as well as their first visit time in the hour. Here's the Avro schema

{"namespace": "com.test",
 "type": "record",
 "name": "Visit",
 "fields": [
     {"name": "dateid", "type": "int"},            // Year Month Day Hour in PST as an integer
e.g. 2014081103

     {"name": "userid", "type": "string"}, 
     {"name": "vcount",  "type": ["long", "null"]},
     {"name": "firsttimestamp",  "type": ["long", "null"]} // Unixtime stamp of first visit


Here I do the parsing, at first vcount and firstvisit aren't set.

PTable<Visit, Pair<Long, Long>> visits = parsed.parallelDo("visits-parsing", new
          Avros.tableOf(Avros.specifics(Visit.class), Avros.pairs(Avros.longs(), Avros.longs())));

The relevant line from VisitsExtractor:
emitter.emit(Pair.of(visit, Pair.of(1L, log.timestamp())));

Everything up to this point works fine, now I want to count up the unique visitors and the
minimum timestamp.

        PTable<Visit, Pair<Long, Long>> agg = visits.groupByKey().combineValues(Aggregators.pairAggregator(Aggregators.SUM_LONGS(),

The above seems to work fine too, now I want to create new Visit classes and fill in the count
and minimum timestamp fields.

        PCollection<Visit> count_visits = agg.parallelDo("visits-count", new DoFn<Pair<Visit,
Pair<Long, Long>>, Visit>() {
          public void process(Pair<Visit, Pair<Long, Long>> p, Emitter<Visit>
emitter) {

            Visit v = Visit.newBuilder(p.first()).build();
       }, Avros.specifics(Visit.class));


     count_visits.write(To.textFile(outputPath), WriteMode.OVERWRITE);

Here's the error:
2014-08-21 15:09:26,245 ERROR run.CrunchReducer ( - Reducer
java.lang.ClassCastException: org.apache.avro.generic.GenericData$Record cannot be cast to

        at com.test.Logs$1.process(
        at com.test.Logs$1.process(

So I'm pretty sure the problem is this line:

Visit v = Visit.newBuilder(p.first()).build();

specifically p.first() should be a Visit type but I guess it isn't. I assume the output of
the groupBy operation in the reducers is serializing the key but not using the correct Avro
type to do it?

Also I don't think I understand when I should be using Avros.records() versus Avros.specifics()
when I have a generated avro file.






View raw message