[
https://issues.apache.org/jira/browse/NIFI-3873?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16014396#comment-16014396
]
ASF GitHub Bot commented on NIFI-3873:
--------------------------------------
Github user mattyb149 commented on a diff in the pull request:
https://github.com/apache/nifi/pull/1788#discussion_r117057133
--- Diff:
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-content-viewer/src/main/java/org/apache/nifi/web/StandardContentViewerController.java
---
@@ -103,11 +108,27 @@ protected void doGet(HttpServletRequest request,
HttpServletResponse response) t
} else if ("application/avro-binary".equals(contentType)
|| "avro/binary".equals(contentType) ||
"application/avro+binary".equals(contentType)) {
final StringBuilder sb = new StringBuilder();
sb.append("[");
- final DatumReader<GenericData.Record> datumReader =
new GenericDatumReader<>();
+ // Use Avro conversions to display logical type values
in human readable way.
+ final GenericData genericData = new GenericData(){
+ @Override
+ protected void toString(Object datum,
StringBuilder buffer) {
+ // Since these types are not quoted and
produce a malformed JSON string, quote it here.
+ if (datum instanceof LocalDate || datum
instanceof LocalTime || datum instanceof DateTime) {
+
buffer.append("\"").append(datum).append("\"");
+ return;
+ }
+ super.toString(datum, buffer);
+ }
+ };
+ genericData.addLogicalTypeConversion(new
Conversions.DecimalConversion());
+ genericData.addLogicalTypeConversion(new
TimeConversions.DateConversion());
+ genericData.addLogicalTypeConversion(new
TimeConversions.TimeConversion());
+ genericData.addLogicalTypeConversion(new
TimeConversions.TimestampConversion());
+ final DatumReader<GenericData.Record> datumReader =
new GenericDatumReader<>(null, null, genericData);
try (final DataFileStream<GenericData.Record>
dataFileReader = new DataFileStream<>(content.getContentStream(), datumReader))
{
while (dataFileReader.hasNext()) {
final GenericData.Record record =
dataFileReader.next();
- final String formattedRecord =
record.toString();
+ final String formattedRecord =
genericData.toString(record);
--- End diff --
The comment in the other PR was that toString() is not guaranteed to be
stable or output what you'd expect. The two approaches output different JSON
for example (although my StandardJsonEncoder subclass emulates the current
GenericData.toString()). However toString() has been fine in the past and for
that other PR (which was closed without modification), so I'm ok with keeping
toString() for now as well. If it ever "breaks", we'd have to change it in a
few places anyway.
> Content viewer to display Avro logical types more human readable way
> --------------------------------------------------------------------
>
> Key: NIFI-3873
> URL: https://issues.apache.org/jira/browse/NIFI-3873
> Project: Apache NiFi
> Issue Type: Improvement
> Components: Extensions
> Affects Versions: 1.2.0
> Reporter: Koji Kawamura
> Assignee: Koji Kawamura
>
> Avro logical types such as decimal and date are stored with its underlying
> type, for example, a logical 'decimal' is stored with bytes, while a logical
> 'date' with int.
> Currently, those logical values are displayed with its underlying type and
> value. It can be more user friendly if we can display it as logically
> converted human readable values.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)