Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
P. Oscar Boykin
@johnynek
as an arg?
Benjamin
@bdparrish
it already does
that was throwing this...
P. Oscar Boykin
@johnynek
can you pass that to ClassTag$.MODULE$.apply( )?
Benjamin
@bdparrish
Error:(113, 70) java: method toBinary in class com.twitter.bijection.avro.SpecificAvroCodecs cannot be applied to given types;
  required: scala.reflect.ClassTag<T>
  found: scala.reflect.ClassTag<T>
  reason: inference variable T has incompatible bounds
    equality constraints: T
    upper bounds: org.apache.avro.specific.SpecificRecordBase
P. Oscar Boykin
@johnynek
can you show me the method we are dealing with (all the code)?
Benjamin
@bdparrish
Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

final ClassTag<T> classTag = ClassTag$.MODULE$.apply(clazz);

Injection<T, byte[]> binaryInjection = SpecificAvroCodecs.toBinary(classTag);
P. Oscar Boykin
@johnynek
what is this for: Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
Benjamin
@bdparrish
public class Subscriber<T extends SpecificRecordBase> {

   // ... brevity ...

   public List<T> listen() throws Exception {
        long timeout = properties.getLong(PrConstants.PR_KAFKA_CONSUMER_POLL_TIMEOUT, 100);

        List<T> records = new ArrayList<>();

        ConsumerRecords<byte[], byte[]> consumedRecords = consumer.poll(timeout);

        for (ConsumerRecord<byte[], byte[]> consumedRecord : consumedRecords) {
            final ClassTag<SpecificRecordBase> classTag = ClassTag$.MODULE$.apply(SpecificRecordBase.class);

            Injection<SpecificRecordBase, byte[]> binaryInjection = SpecificAvroCodecs.toBinary(classTag);

            T avro = (T) binaryInjection.invert(consumedRecord.value());

            records.add(avro);
        }

        return records;
    }
}
P. Oscar Boykin
@johnynek
I think you need:
public class Subscriber<T extends SpecificRecordBase>(Class<T> klass) …
and pass that to:
final ClassTag<T extends SpecificRecordBase> classTag = ClassTag$.MODULE$.apply(klass);
or you need to get that concrete class somehow
due to erasure, I don’t think there is anyway around a user passing it (or an instance)
(in scala, that is what ClassTag[T] does, it only works if T is not abstract or you already have a ClassTag[T] to pass)
Benjamin
@bdparrish
ok
might have to move to something else
none of that works with Java
but i get your point
P. Oscar Boykin
@johnynek
why can’t you just pass the Class<T> in?
Benjamin
@bdparrish
i can pass that in
P. Oscar Boykin
@johnynek
ok.
Benjamin
@bdparrish
but i can't get out the right object
P. Oscar Boykin
@johnynek
what do you mean?
what is the right object?
Benjamin
@bdparrish
I am still getting a ClassTag<T> from ClassTag$.MODULE$.apply(clazz);
which I cannot pass into SpecificAvroCodecs.toBinary(classTag);
same original issue
P. Oscar Boykin
@johnynek
what is the type of clazz?
Benjamin
@bdparrish
Class<T>
P. Oscar Boykin
@johnynek
okay, so there are two issues here:
1) having the right class at runtime.
2) having the types line up at compile time.
Benjamin
@bdparrish
yep
P. Oscar Boykin
@johnynek
if you don’t have both working it fails. We got #2, but not #1.
so, your attempt here will fix #1 but not #2.
#2 can be fixed by casting if we don’t break #1
before we used casting, but actually were breaking #1
so, you can just do: (ClassTag<T extends SpecificRecordBase>)ClassTag$.MODULE$.apply(clazz) and I think it will work.
(I don’t do much java these days… :(
if that cast does not work, one will...
Benjamin
@bdparrish
cannot cast like that
P. Oscar Boykin
@johnynek
like, you could just do: (ClassTag<SpecificRecordBase>)
Benjamin
@bdparrish
the extends is already defined in the class
P. Oscar Boykin
@johnynek
as long as you keep the clazz being passed in.
Benjamin
@bdparrish
using ClassTag<SpecificRecordBase> got me the other error...
Exception in thread "main" java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at com.twitter.bijection.avro.SpecificAvroCodecs$.toBinary(AvroCodecs.scala:106)
    at com.twitter.bijection.avro.SpecificAvroCodecs.toBinary(AvroCodecs.scala)
Michael Hsueh
@mkhsueh
Howdy- nice work on Bijection! Currently using it for Avro handling; has anyone gotten this error deserializing Joda DateTime? java.lang.Long cannot be cast to org.joda.time.DateTime
was able to serialize a record with this value: {"name": "eventTime", "type": {"type": "long", "logicalType": "timestamp-millis"}}, but calling injection.invert(the serialized byte[]).get() throws that error