[
https://issues.apache.org/jira/browse/AVRO-4124?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18046386#comment-18046386
]
Philipp Nowak commented on AVRO-4124:
-------------------------------------
For anyone finding this on Google, *here is a workaround* that worked for me to
make the big-decimal logical type work via the avro-maven-plugin:
{code:java}
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.12.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
<goal>idl</goal>
</goals>
<configuration>
<enableDecimalLogicalType>true</enableDecimalLogicalType>
<customConversions>
<!-- ref:
https://github.com/apache/avro/blob/99c7379ce62d002daca4d94b975afabc725df024/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java#L431
/ AVRO-4124 -->
<conversion>org.apache.avro.Conversions$BigDecimalConversion</conversion>
</customConversions>
</configuration>
</execution>
</executions>
</plugin>{code}
Why does this work?
* I found that the conversion for {{decimal}} is registered like this:
[https://github.com/apache/avro/blob/99c7379ce62d002daca4d94b975afabc725df024/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java#L431]
* There is no such registration for BigDecimal. During the initial
implementation:
[https://github.com/apache/avro/pull/2282/files#diff-f676b1f77f65cebe90fecda20258df525c0cfeaca931f7327589bc75214ec6c0]
the conversion was implemented but not registered in the SpecificCompiler
(maybe that was intentional, I don't know)
* Once the conversion is registered, the java type is correctly recognised and
the *field type is generated as {{java.math.BigDecimal}}* ** instead of
{{java.nio.ByteBuffer}} :)
> [java][avro-maven-plugin] LogicalType "big-decimal" generate
> java.nio.ByteBuffer instead of BigDecimal
> ------------------------------------------------------------------------------------------------------
>
> Key: AVRO-4124
> URL: https://issues.apache.org/jira/browse/AVRO-4124
> Project: Apache Avro
> Issue Type: Bug
> Components: java
> Affects Versions: 1.12.0
> Environment: Java
> Maven
> Springboot 2
> Reporter: BertrandM
> Priority: Major
> Attachments: Quote.avsc
>
>
> Hi, i face to an issue during code generation and i did not find solution or
> existing Jira ticket about this subject
>
> *Context:*
> On our projet we was on avro 1.11.0 with java. To define big-decimal, we
> used Decimal logical type with scale and precision
> {code:java}
> LogicalTypes.decimal(BIGDECIMAL_PRECISION,
> BIGDECIMAL_SCALE).addToSchema(Schema.create(Schema.Type.BYTES)); {code}
> But we face some issue du to undetermined large number and we had update
> scale and precision multiple time.
> Avro generator correctly generate java properties as BigDecimal in avro pojo
> Since 1.12.0 ([Specification | Apache
> Avro)|https://avro.apache.org/docs/1.12.0/specification/#decimal] we can
> define big-decimal logical type to avoid fixed scale/precision.
> So to avoid schema change again, i try to use the new logical type as we are
> in java ecosystem. Update dependencies add this logical type in schema
> builder :
> {code:java}
> codeLogicalTypes.bigDecimal().addToSchema(Schema.create(Schema.Type.BYTES));
> {code}
>
> *Problem :*
> By using Big-decimal LogicalType, i thought that maven plugin will continue
> to generate BigDecimal but now i have ByteBuffer instead.
> _Maven configuration :_
> {code:java}
> <plugin>
> <groupId>org.apache.avro</groupId>
> <artifactId>avro-maven-plugin</artifactId>
> <version>1.12.0</version>
> <executions>
> <execution>
> <phase>generate-sources</phase>
> <goals>
> <goal>schema</goal>
> </goals>
> <configuration>
>
> <sourceDirectory>${project.basedir}/src/main/resources/schemas/avro/merged</sourceDirectory>
>
> <outputDirectory>${project.build.directory}/generated-sources/src/main/java/</outputDirectory>
> <enableDecimalLogicalType>true</enableDecimalLogicalType>
> </configuration>
> </execution>
> </executions>
> </plugin>
> {code}
> _Avro schema:_
> {code:java}
> {
> "name" : "value",
> "type" : {
> "type" : "bytes",
> "logicalType" : "big-decimal"
> }
> } {code}
> _[^Quote.avsc]_
>
> _Java CLass:_
> {code:java}
> private java.nio.ByteBuffer value;
> {code}
> I searched in github repository but did not find where is the problem or if i
> miss something on configuration
>
> Thanks for help
--
This message was sent by Atlassian Jira
(v8.20.10#820010)