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/26 23:17:01 UTC

Is map schema's key an UTF8 or String type?

Hello Avro user,

I defined a map type in the schema with values of type double.
After reading the map from the schema, I realized that the key is of type
avro utils.utf8 and cannot be cast to java.lang.String

So, the following is not working when trying to loop through the entries of
the map.

for(Entry<String, Double> entry: map.entrySet())

Am I missing something ?

Thank you so much,

Re: Is map schema's key an UTF8 or String type?

Posted by Scott Carey <sc...@richrelevance.com>.
the GenericData.Array returned should be iterated over, and .toString() called on each element if you want to convert to a string.

GenericData.Array arr = rec.get("ids");
int size = arr.size();
String[] items = new String[size];
for (int i = 0; i < size; i++) {
  items[i] = arr.get(i).toString();
}

toArray will produce an array with elements of type Utf8 (or CharSequence, depending on the API you use).  Casting doesn't do any conversion on reference types.

On 4/27/11 8:22 AM, "Weishung Chung" <we...@gmail.com>> wrote:

Would it be possible to cast Array<String> to String[] ?

I was trying to cast it to String[] but i got a runtime error complaining about Utf8 to String cast :(
Array<String> ids = (Array<String>) rec.get("ids");
String[] outputIds = (String[]) outputIdsArray.toArray(new String[ids.size()]);
Am I missing something here?


On Tue, Apr 26, 2011 at 10:39 PM, Felix.徐 <yg...@gmail.com>> wrote:
Key of Map is UTF8 type.


2011/4/27 Weishung Chung <we...@gmail.com>>
Ok thank you for the answer :)


On Tue, Apr 26, 2011 at 4:24 PM, Scott Carey <sc...@richrelevance.com>> wrote:
Strings are of Java type CharSequence
toString() on CharSequence is consistent between String and Utf8.  However, make sure any map you use has only one implementation of CharSequence in it, since hashCode() and equals() on different CharSequence types may not be consistent.

On 4/26/11 2:17 PM, "Weishung Chung" <we...@gmail.com>> wrote:

Hello Avro user,

I defined a map type in the schema with values of type double.
After reading the map from the schema, I realized that the key is of type avro utils.utf8 and cannot be cast to java.lang.String

So, the following is not working when trying to loop through the entries of the map.

for(Entry<String, Double> entry: map.entrySet())

Am I missing something ?

Thank you so much,




Re: Is map schema's key an UTF8 or String type?

Posted by Weishung Chung <we...@gmail.com>.
Would it be possible to cast Array<String> to String[] ?

I was trying to cast it to String[] but i got a runtime error complaining
about Utf8 to String cast :(
Array<String> ids = (Array<String>) rec.get("ids");
String[] outputIds = (String[]) outputIdsArray.toArray(new
String[ids.size()]);
Am I missing something here?


On Tue, Apr 26, 2011 at 10:39 PM, Felix.徐 <yg...@gmail.com> wrote:

> Key of Map is UTF8 type.
>
>
> 2011/4/27 Weishung Chung <we...@gmail.com>
>
>> Ok thank you for the answer :)
>>
>>
>> On Tue, Apr 26, 2011 at 4:24 PM, Scott Carey <sc...@richrelevance.com>wrote:
>>
>>> Strings are of Java type CharSequence
>>> toString() on CharSequence is consistent between String and Utf8.
>>>  However, make sure any map you use has only one implementation of
>>> CharSequence in it, since hashCode() and equals() on different CharSequence
>>> types may not be consistent.
>>>
>>> On 4/26/11 2:17 PM, "Weishung Chung" <we...@gmail.com> wrote:
>>>
>>> Hello Avro user,
>>>
>>> I defined a map type in the schema with values of type double.
>>> After reading the map from the schema, I realized that the key is of type
>>> avro utils.utf8 and cannot be cast to java.lang.String
>>>
>>> So, the following is not working when trying to loop through the entries
>>> of the map.
>>>
>>> for(Entry<String, Double> entry: map.entrySet())
>>>
>>> Am I missing something ?
>>>
>>> Thank you so much,
>>>
>>>
>>
>

Re: Is map schema's key an UTF8 or String type?

Posted by "Felix.徐" <yg...@gmail.com>.
Key of Map is UTF8 type.

2011/4/27 Weishung Chung <we...@gmail.com>

> Ok thank you for the answer :)
>
>
> On Tue, Apr 26, 2011 at 4:24 PM, Scott Carey <sc...@richrelevance.com>wrote:
>
>> Strings are of Java type CharSequence
>> toString() on CharSequence is consistent between String and Utf8.
>>  However, make sure any map you use has only one implementation of
>> CharSequence in it, since hashCode() and equals() on different CharSequence
>> types may not be consistent.
>>
>> On 4/26/11 2:17 PM, "Weishung Chung" <we...@gmail.com> wrote:
>>
>> Hello Avro user,
>>
>> I defined a map type in the schema with values of type double.
>> After reading the map from the schema, I realized that the key is of type
>> avro utils.utf8 and cannot be cast to java.lang.String
>>
>> So, the following is not working when trying to loop through the entries
>> of the map.
>>
>> for(Entry<String, Double> entry: map.entrySet())
>>
>> Am I missing something ?
>>
>> Thank you so much,
>>
>>
>

Re: Is map schema's key an UTF8 or String type?

Posted by Weishung Chung <we...@gmail.com>.
Ok thank you for the answer :)

On Tue, Apr 26, 2011 at 4:24 PM, Scott Carey <sc...@richrelevance.com>wrote:

> Strings are of Java type CharSequence
> toString() on CharSequence is consistent between String and Utf8.  However,
> make sure any map you use has only one implementation of CharSequence in it,
> since hashCode() and equals() on different CharSequence types may not be
> consistent.
>
> On 4/26/11 2:17 PM, "Weishung Chung" <we...@gmail.com> wrote:
>
> Hello Avro user,
>
> I defined a map type in the schema with values of type double.
> After reading the map from the schema, I realized that the key is of type
> avro utils.utf8 and cannot be cast to java.lang.String
>
> So, the following is not working when trying to loop through the entries of
> the map.
>
> for(Entry<String, Double> entry: map.entrySet())
>
> Am I missing something ?
>
> Thank you so much,
>
>

Re: Is map schema's key an UTF8 or String type?

Posted by Scott Carey <sc...@richrelevance.com>.
Strings are of Java type CharSequence
toString() on CharSequence is consistent between String and Utf8.  However, make sure any map you use has only one implementation of CharSequence in it, since hashCode() and equals() on different CharSequence types may not be consistent.

On 4/26/11 2:17 PM, "Weishung Chung" <we...@gmail.com>> wrote:

Hello Avro user,

I defined a map type in the schema with values of type double.
After reading the map from the schema, I realized that the key is of type avro utils.utf8 and cannot be cast to java.lang.String

So, the following is not working when trying to loop through the entries of the map.

for(Entry<String, Double> entry: map.entrySet())

Am I missing something ?

Thank you so much,