Github user apiri commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/213#discussion_r56201575
  
    --- Diff: 
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-processors/src/main/java/org/apache/nifi/processors/aws/kinesis/firehose/PutKinesisFirehose.java
 ---
    @@ -0,0 +1,180 @@
    +/*
    + * 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.firehose;
    +
    +import java.io.ByteArrayOutputStream;
    +import java.nio.ByteBuffer;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.Collections;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.commons.lang3.StringUtils;
    +import org.apache.nifi.annotation.behavior.InputRequirement;
    +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
    +import org.apache.nifi.annotation.behavior.SupportsBatching;
    +import org.apache.nifi.annotation.behavior.WritesAttribute;
    +import org.apache.nifi.annotation.behavior.WritesAttributes;
    +import org.apache.nifi.annotation.documentation.CapabilityDescription;
    +import org.apache.nifi.annotation.documentation.Tags;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.flowfile.FlowFile;
    +import org.apache.nifi.processor.ProcessContext;
    +import org.apache.nifi.processor.ProcessSession;
    +import com.amazonaws.services.kinesisfirehose.AmazonKinesisFirehoseClient;
    +import com.amazonaws.services.kinesisfirehose.model.PutRecordBatchRequest;
    +import 
com.amazonaws.services.kinesisfirehose.model.PutRecordBatchResponseEntry;
    +import com.amazonaws.services.kinesisfirehose.model.PutRecordBatchResult;
    +import com.amazonaws.services.kinesisfirehose.model.Record;
    +
    +@SupportsBatching
    +@InputRequirement(Requirement.INPUT_REQUIRED)
    +@Tags({"amazon", "aws", "firehose", "kinesis", "put", "stream"})
    +@CapabilityDescription("Sends the contents to a specified Amazon Kinesis 
Firehose. "
    +    + "In order to send data to firehose, the firehose delivery stream 
name has to be specified.")
    +@WritesAttributes({
    +    @WritesAttribute(attribute = "aws.kinesis.firehose.error.message", 
description = "Error message on posting message to AWS Kinesis Firehose"),
    +    @WritesAttribute(attribute = "aws.kinesis.firehose.error.code", 
description = "Error code for the message when posting to AWS Kinesis 
Firehose"),
    +    @WritesAttribute(attribute = "aws.kinesis.firehose.record.id", 
description = "Record id of the message posted to Kinesis Firehose")})
    +public class PutKinesisFirehose extends AbstractKinesisFirehoseProcessor {
    +
    +    /**
    +     * Kinesis put record response error message
    +     */
    +    public static final String AWS_KINESIS_FIREHOSE_ERROR_MESSAGE = 
"aws.kinesis.firehose.error.message";
    +
    +    /**
    +     * Kinesis put record response error code
    +     */
    +    public static final String AWS_KINESIS_FIREHOSE_ERROR_CODE = 
"aws.kinesis.firehose.error.code";
    +
    +    /**
    +     * Kinesis put record response record id
    +     */
    +    public static final String AWS_KINESIS_FIREHOSE_RECORD_ID = 
"aws.kinesis.firehose.record.id";
    +
    +    public static final List<PropertyDescriptor> properties = 
Collections.unmodifiableList(
    +            Arrays.asList(KINESIS_FIREHOSE_DELIVERY_STREAM_NAME, 
BATCH_SIZE, MAX_MESSAGE_BUFFER_SIZE_MB, REGION, ACCESS_KEY, SECRET_KEY, 
CREDENTIALS_FILE, AWS_CREDENTIALS_PROVIDER_SERVICE, TIMEOUT,
    +                  PROXY_HOST,PROXY_HOST_PORT));
    +
    +    /**
    +     * Max buffer size 1000kb
    +     */
    +    public static final int MAX_MESSAGE_SIZE = 1000 * 1024;
    +
    +    @Override
    +    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
    +        return properties;
    +    }
    +
    +    @Override
    +    public void onTrigger(final ProcessContext context, final 
ProcessSession session) {
    +        FlowFile flowFileCandidate = session.get();
    +        if ( flowFileCandidate == null )
    +            return;
    +
    +        long currentBufferSizeBytes = flowFileCandidate.getSize();
    +
    +        final int batchSize = context.getProperty(BATCH_SIZE).asInteger();
    +        final int maxBufferSizeBytes = 
context.getProperty(MAX_MESSAGE_BUFFER_SIZE_MB).asInteger() * 1024 * 1000;
    +
    +        // Get max batch size messages with size limit defined by 
maxBufferSizeBytes
    +        List<FlowFile> flowFiles = new ArrayList<FlowFile>(batchSize);
    +        flowFiles.add(flowFileCandidate);
    +        for (int i = 1; (i < batchSize) && (currentBufferSizeBytes <= 
maxBufferSizeBytes); i++) {
    +            flowFileCandidate = session.get();
    +            if ( flowFileCandidate == null )
    +                break;
    +            currentBufferSizeBytes += flowFileCandidate.getSize();
    +            flowFiles.add(flowFileCandidate);
    +        }
    +
    +        final String firehoseStreamName = 
context.getProperty(KINESIS_FIREHOSE_DELIVERY_STREAM_NAME).getValue();
    +        final AmazonKinesisFirehoseClient client = getClient();
    +
    +        try {
    +            List<Record> records = new ArrayList<>();
    +
    +            List<FlowFile> failedFlowFiles = new ArrayList<>();
    +            List<FlowFile> successfulFlowFiles = new ArrayList<>();
    +
    +            // Prepare batch of records
    +            for (int i = 0; i < flowFiles.size(); i++) {
    +                FlowFile flowFile = flowFiles.get(i);
    +
    +                // If flow file too large send it to failure
    --- End diff --
    
    This condition check should still happen before the above buffering 
considerations.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to