[
https://issues.apache.org/jira/browse/AVRO-3050?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17289183#comment-17289183
]
Ryan Skraba commented on AVRO-3050:
-----------------------------------
Hello! Are you looking to convert {{d1fixed}} to a UTF-8 string? You can
directly pass it to a jsonEncoder:
{code}
final String utf8String;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Encoder encoder = EncoderFactory.get().jsonEncoder(fixedSchema, baos,
false);
encoder.writeFixed(d1fixed.bytes());
encoder.flush();
utf8String = new String(baos.toByteArray(), StandardCharsets.UTF_8);
}
// The string contains exactly eighty eight characters corresponding to the
following unescaped string:
//
"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u00000¨"
{code}
If you have a more complicated structure, you might want to use a
GenericDatumWriter that wraps a JsonEncoder. Is this what you are looking for?
You could also construct the String yourself, either using a JSON toolkit (like
Jackson's JsonGenerator.writeString), or by writing the hex values of the bytes
manually.
> Request for Documentation for Fixed Decimal Type in JSON data representation
> ----------------------------------------------------------------------------
>
> Key: AVRO-3050
> URL: https://issues.apache.org/jira/browse/AVRO-3050
> Project: Apache Avro
> Issue Type: Task
> Components: java, logical types
> Affects Versions: 1.8.0
> Reporter: Divya
> Priority: Major
>
> We are trying to use a fixed type for logicalType Decimal. We create JSON and
> then convert it into Parquet(Avro). There, we are trying to call
> conversion.toFixed(), but it is returning in bytes (Array.toString(bytes)).
> We are using Avro 1.8 version.
>
> BigDecimal d1 = new BigDecimal("124.56")
> LogicalType decimal = LogicalTypes.decimal(9, 2);
> Schema fixedSchema = Schema.createFixed(Schema.Type.FIXED.name, null, null,
> 16);
> Conversion<BigDecimal> conversion = new Conversions.DecimalConversion();
> GenericFixed d1fixed = conversion.toFixed(d1, fixedSchema, decimal);
>
>
> Is there any method that we can use to convert it into 'Fixed text' that
> JSONDecoder is expecting? Any help will be appreciated.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)