awelless commented on code in PR #10077:
URL: https://github.com/apache/nifi/pull/10077#discussion_r2309552176


##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-kinesis/src/main/java/org/apache/nifi/processors/aws/kinesis/ReaderRecordProcessor.java:
##########
@@ -0,0 +1,245 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.aws.kinesis;
+
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.serialization.MalformedRecordException;
+import org.apache.nifi.serialization.RecordReader;
+import org.apache.nifi.serialization.RecordReaderFactory;
+import org.apache.nifi.serialization.RecordSetWriter;
+import org.apache.nifi.serialization.RecordSetWriterFactory;
+import org.apache.nifi.serialization.WriteResult;
+import org.apache.nifi.serialization.record.Record;
+import org.apache.nifi.serialization.record.RecordSchema;
+import software.amazon.kinesis.retrieval.KinesisClientRecord;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.util.ArrayList;
+import java.util.List;
+
+import static java.util.Collections.emptyMap;
+import static 
org.apache.nifi.processors.aws.kinesis.ConsumeKinesisAttributes.RECORD_COUNT;
+import static 
org.apache.nifi.processors.aws.kinesis.ConsumeKinesisAttributes.RECORD_ERROR_MESSAGE;
+
+final class ReaderRecordProcessor {
+
+    private final RecordReaderFactory recordReaderFactory;
+    private final RecordSetWriterFactory recordWriterFactory;
+    private final ComponentLog logger;
+
+    ReaderRecordProcessor(
+            final RecordReaderFactory recordReaderFactory,
+            final RecordSetWriterFactory recordWriterFactory,
+            final ComponentLog logger) {
+        this.recordReaderFactory = recordReaderFactory;
+        this.recordWriterFactory = recordWriterFactory;
+        this.logger = logger;
+    }
+
+    ProcessingResult processRecords(
+            final ProcessSession session,
+            final String streamName,
+            final String shardId,
+            final List<KinesisClientRecord> records) {
+        final List<FlowFile> successFlowFiles = new ArrayList<>();
+        final List<FlowFile> failureFlowFiles = new ArrayList<>();
+
+        ActiveFlowFile activeFlowFile = null;
+
+        for (final KinesisClientRecord kinesisRecord : records) {
+            final byte[] data = new byte[kinesisRecord.data().remaining()];
+            kinesisRecord.data().get(data);
+
+            try (final InputStream in = new ByteArrayInputStream(data);
+                 final RecordReader reader = 
recordReaderFactory.createRecordReader(emptyMap(), in, data.length, logger)) {
+
+                Record record;
+                while ((record = reader.nextRecord()) != null) {
+                    final RecordSchema writeSchema = 
recordWriterFactory.getSchema(emptyMap(), record.getSchema());
+
+                    if (activeFlowFile == null) {
+                        activeFlowFile = ActiveFlowFile.startNewFile(logger, 
session, recordWriterFactory, writeSchema, streamName, shardId);
+                    } else if (!writeSchema.equals(activeFlowFile.schema())) {
+                        // If the write schema has changed, we need to 
complete the current FlowFile and start a new one.

Review Comment:
   Makes sense. 
   I mentioned the performance implications in the processor's 
`additionalDetails`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to