You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Weishung Chung <we...@gmail.com> on 2011/04/27 17:47:57 UTC

runtime error when casting Array to String[]

Hello fellow Avro users,

Do you guys have this problem when casting Array<String> to String[]?
Am I missing something here :(

I got a runtime exception as follows

Exception in thread "main" java.lang.ArrayStoreException:
org.apache.avro.util.Utf8
at java.util.AbstractCollection.toArray(AbstractCollection.java:171)

Array<String> idsArray = (Array<String>) rec.get("ids");
String[] ids = (String[]) idsArray.toArray(new String[idsArray.size()]);

Thank you

Re: runtime error when casting Array to String[]

Posted by Weishung Chung <we...@gmail.com>.
Thanks alot, good idea :)


On Wed, Apr 27, 2011 at 2:09 PM, Scott Carey <sc...@richrelevance.com>wrote:

>
>
> On 4/27/11 11:26 AM, "Markus Weimer" <we...@yahoo-inc.com> wrote:
>
> >HI,
> >
> >> I am working on tools for Avro that will make Alternate object mappings
> >>easier.  I hope this will land in 1.6.0.  Custom specific compiled
> >>variants will more easily leverage all the work unrelated to object type
> >>mapping.
> >
> >Nice! Will that allow me to have an array of floats in the schema and
> >receive it as a float[] in the compiled java class?
> >
>
> It will allow users to create custom mappings easier, one of which might
> be that avro arrays that map to java array types instead of List.  A few
> basic mapping types might find their way into Avro if they are contributed.
>
> The Specific compiler has been templatized for a while now, and users can
> create their own code templates that produces classes as they like, but
> writing a custom SpecificDatum(Reader|Writer) requires knowing a lot about
> things unrelated to the mapping of Avro types to Java types.
>
> Creating a custom mapping isn't always trivial, but having it be a
> separate concern from schema translation and resolution, and low level
> encoding/decoding operations will allow for greater code re-use and
> modular composition.
>
> >Thanks,
> >
> >Markus
>
>

Re: runtime error when casting Array to String[]

Posted by Scott Carey <sc...@richrelevance.com>.

On 4/27/11 11:26 AM, "Markus Weimer" <we...@yahoo-inc.com> wrote:

>HI,
>
>> I am working on tools for Avro that will make Alternate object mappings
>>easier.  I hope this will land in 1.6.0.  Custom specific compiled
>>variants will more easily leverage all the work unrelated to object type
>>mapping.
>
>Nice! Will that allow me to have an array of floats in the schema and
>receive it as a float[] in the compiled java class?
>

It will allow users to create custom mappings easier, one of which might
be that avro arrays that map to java array types instead of List.  A few
basic mapping types might find their way into Avro if they are contributed.

The Specific compiler has been templatized for a while now, and users can
create their own code templates that produces classes as they like, but
writing a custom SpecificDatum(Reader|Writer) requires knowing a lot about
things unrelated to the mapping of Avro types to Java types.

Creating a custom mapping isn't always trivial, but having it be a
separate concern from schema translation and resolution, and low level
encoding/decoding operations will allow for greater code re-use and
modular composition.

>Thanks,
>
>Markus


Re: runtime error when casting Array to String[]

Posted by Markus Weimer <we...@yahoo-inc.com>.
HI,

> I am working on tools for Avro that will make Alternate object mappings easier.  I hope this will land in 1.6.0.  Custom specific compiled variants will more easily leverage all the work unrelated to object type mapping.

Nice! Will that allow me to have an array of floats in the schema and receive it as a float[] in the compiled java class?

Thanks,

Markus

Re: runtime error when casting Array to String[]

Posted by Scott Carey <sc...@richrelevance.com>.
I am working on tools for Avro that will make Alternate object mappings
easier.  I hope this will land in 1.6.0.  Custom specific compiled
variants will more easily leverage all the work unrelated to object type
mapping.

On 4/27/11 11:06 AM, "Markus Weimer" <we...@yahoo-inc.com> wrote:

>
>whenever you ask for an "array" in the schema, the avro specific compiler
>gives you a java.util.List, which cannot be cast to a Java array. I'm
>personally pretty interested in a way around this, as my data usually
>consists of primitive type arrays, where the overhead for a Object
>Collection can be prohibitive.
>
>Take care,
>
>Markus


Re: runtime error when casting Array to String[]

Posted by Markus Weimer <we...@yahoo-inc.com>.
Hi,

> Do you guys have this problem when casting Array<String> to String[]?
> Am I missing something here :(

whenever you ask for an "array" in the schema, the avro specific compiler gives you a java.util.List, which cannot be cast to a Java array. I'm personally pretty interested in a way around this, as my data usually consists of primitive type arrays, where the overhead for a Object Collection can be prohibitive.

Take care,

Markus