如何生成字符串类型,而不是CharSequence的使用Avro的领域? [英] How to generate fields of type String instead of CharSequence using Avro?

查看:911
本文介绍了如何生成字符串类型,而不是CharSequence的使用Avro的领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写出了一部部的Avro架构中,一些领域的**需字符串但Avro公司已产生类型的字段中键入的CharSequence 。

I wrote one Avro schema in which some of the fields ** need to be ** of type String but Avro has generated those fields of type CharSequence.

我无法找到任何方式告诉Avro公司制作类型的这些领域字符串

I am not able to find any way to tell Avro to make those fields of type String.

我试图用

"fields": [
    {
        "name":"startTime",
        "type":"string",
        "avro.java.stringImpl":"String"
    },
    {
        "name":"endTime",
        "type":"string",
        "avro.java.string":"String"
    }
]

但两个领域的Avro正在生成类型的字段的CharSequence

有没有其他的方法,使类型的这些领域字符串

Is there any other way to make those fields of type String?

推荐答案

如果你想你的字符串字段是 java.lang.String中的实例,那么你只需要配置编译器:

If you want all you string fields be instances of java.lang.String then you only have to configure the compiler:

java -jar /path/to/avro-tools-1.7.7.jar compile -string schema 

如果您正在使用Maven插件

or if you are using the Maven plugin

<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
  <version>1.7.7</version>
  <configuration>
    <stringType>String</stringType>
  </configuration>
  [...]
</plugin>        

如果你想要一个特定的字段​​是java.lang.String类型的,那么......你不能。它不被编译器支持。您可以使用Java的类与反映API,但编译器不关心。

If you want one specific field to be of type java.lang.String then... you can't. It is not supported by the compiler. You can use "java-class" with the reflect API but the compiler does not care.

如果您想了解更多,您可以设置SpecificCompiler线372,Avro的1.7.7断点。你可以看到,在调用 addStringType()的模式已经在道具所需要的信息前场。如果您通过这个架构以 SpecificCompiler.javaType()然后它会做你想要什么。但随后 addStringType 通过静态的替代架构。我将最有可能请在邮件列表上的问题,因为我不明白这一点。

If you want to learn more, you can set a breakpoint in SpecificCompiler line 372, Avro 1.7.7. You can see that before the call to addStringType() the schema have the required information in the props field. If you pass this schema to SpecificCompiler.javaType() then it will do what you want. But then addStringType replaces your schema by a static one. I will most likely ask the question on the mailing list since I don't see the point.

这篇关于如何生成字符串类型,而不是CharSequence的使用Avro的领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆