问题描述
我正在使用。
我的架构有地图类型:
{"name": "MyData",
"type" : {"type": "map",
"values":{
"type": "record",
"name": "Person",
"fields":[
{"name": "name", "type": "string"},
{"name": "age", "type": "int"},
]
}
}
}
编译架构后,生成的Java类使用 CharSequence
作为 Map $的键 c $ c>
MyData
。
After compile the schema, the genated Java class use CharSequence
as the key for the Map
MyData
.
在地图
中使用 CharSequence
非常不方便作为键,有没有办法在?
It is very inconvenient to use CharSequence
in Map
as key, is there a way to generate String
type key for Map
in Apache Avro?
PS
问题是,例如 dataMap.containsKey(SOME_KEY)
将返回 false
即使那里有这样的密钥,只是因为它是 CharSequence
。此外,使用现有密钥放置映射条目不会重新使用旧密钥。这就是为什么我说使用 CharSequence
作为密钥是不方便的。
Problem is that, for example dataMap.containsKey("SOME_KEY")
will returns false
even though there is such key there, just because it is CharSequence
. Besides, put an map entry with a existing key doesn't relpace the old one. That's why I say it is inconvenient to use CharSequence
as key.
推荐答案
是相关的。仍然使用CharSequence的要点是向后兼容性。
This JIRA discussion is relevant. The main point of CharSequence still being used is backwards-compatability.
和Charles Forsythe指出的一样,通过在模式中设置字符串属性,已经为必要时添加了一个变通方法。
And like Charles Forsythe pointed out, there has been added a workaround for when String is necessary, by setting the string property in the schema.
{ "type": "string", "avro.java.string": "String" }
这里的默认类型是他们自己的Utf8类。除了手动规范和,甚至有一个avro-tools编译选项, -string
选项:
The default type here is their own Utf8 class. In addition to manual specification and the pom.xml setting, there is even an avro-tools compile option for it, the -string
option:
java -jar avro-tools.1.7.5.jar compile -string schema /path/to/schema .
这篇关于Apache Avro:map使用CharSequence作为密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!