markap14 commented on a change in pull request #3282: NIFI-5983: handling parse
problems in recordReader implementations
URL: https://github.com/apache/nifi/pull/3282#discussion_r253565293
##########
File path:
nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/avro/AvroRecordReader.java
##########
@@ -33,14 +35,21 @@
@Override
public Record nextRecord(final boolean coerceTypes, final boolean
dropUnknownFields) throws IOException, MalformedRecordException {
- GenericRecord record = nextAvroRecord();
- if (record == null) {
- return null;
+ try {
+ GenericRecord record = nextAvroRecord();
+ if (record == null) {
+ return null;
+ }
+
+ final RecordSchema schema = getSchema();
+ final Map<String, Object> values =
AvroTypeUtil.convertAvroRecordToMap(record, schema);
+ return new MapRecord(schema, values);
+ } catch (IOException e) {
+ throw e;
+ } catch (MalformedRecordException e) {
+ throw e;
+ } catch (Throwable e) {
+ throw new MalformedRecordException(String.format("Error while
getting next record. Root cause: %s", Throwables.getRootCause(e).getMessage()),
e);
Review comment:
We should avoid `String.format` and instead just concatenate the two
Strings. `String.format` is quite expensive, parsing a Regular Expression
behind the scenes, then evaluating it against the String and performing
substitution. If formatting of numbers of different precisions, etc. is
required then it makes sense to do so but in a case where it's just
concatenating Strings I would generally steer clear of it.
I would also avoid using `Throwables.getRootCause(e).getMessage()` and
instead just use `Throwables.getRootCause(e)`. Often times, especially for
RuntimeException's, such as NullPointerException, NumberFormatException, etc,
there is no message, or the message only makes sense in the context of the
Exception. For example, if a value is expected to be a number but is the value
"hello", we could get a `NumberFormatException` with a message of `hello`. In
this case, the Exception would read: `Error while getting next record. Root
cause: hello`. Instead, keeping the `toString()` of the Exception would yield
`Error while getting next record. Root cause: NumberFormatException: hello`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services