Skip to content

Firehose: Dynamic partitioning

0

For a specific web service in API Gateway, we send incoming JSON requests to SQS, which then forwards them to an EventBridge event bus. A rule on the event bus directs the messages to Firehose for further processing before storing them in an S3 bucket.

We want to implement dynamic partitioning in Firehose based on the field .detail.body.events[0].enrichedEventData.vatNo. However, due to improper JSON formatting in the event body, Firehose encounters an error while extracting this field for partitioning.

Here is a sample of the incoming event: { "version": "0", "id": "c83b15ec-5e6f-ff1a-baef-908e7eeb9c5b", "detail-type": "businessTransaction", "source": "Pipe dev-sqs-eb-pipe", "account": "009160064670", "time": "2025-01-21T08:22:39Z", "region": "eu-central-1", "resources": [], "detail": { "messageId": "cf5c18a2-4329-4cb4-bcd3-9df43585ec4d", "body": "{"unsentEventCount":1,"fdmId":"FOD01000001","events":[{"enrichedEventData":{"vatNo":"BE0000000097"}}]}" } }

Would you have any recommendations on the best approach, or is there an AWS-native way to handle this directly in Firehose?

IMPORTANT: I don't want to use a lambda

Looking forward to your insights.

2 Answers
1
Accepted Answer

To implement dynamic partitioning in Firehose based on the field .detail.body.events[0].enrichedEventData.vatNo from your sample event, you can use Firehose's built-in data transformation capabilities without resorting to a Lambda function. Here's an approach you can take:

  1. Enable dynamic partitioning when setting up your Firehose delivery stream to Amazon S3.

  2. For the partitioning key, you'll need to use a JQ expression to extract the desired field. Given the nested structure of your JSON, you can use the following JQ expression:

    (.detail.body | fromjson).events[0].enrichedEventData.vatNo

    This expression first parses the "body" field (which is a string containing JSON) using the fromjson function, then navigates to the desired "vatNo" field.

  3. Set up your S3 bucket prefix to use this partitioning key. For example:

    vatNo=!{partitionKeyFromQuery:vatNo}/year=!{timestamp:yyyy}/month=!{timestamp:MM}/day=!{timestamp:dd}/

This approach will partition your data in S3 based on the VAT number, as well as by year, month, and day.

To handle potential errors:

  1. Ensure you specify an S3 error bucket prefix when creating your Firehose stream. This is mandatory when enabling dynamic partitioning.

  2. If some records fail to be partitioned (e.g., due to missing or malformed data), they will be delivered to this error bucket. You can then analyze these records to identify and resolve any issues.

  3. Consider using a default value in your JQ expression to handle cases where the VAT number might be missing:

    ((.detail.body | fromjson).events[0].enrichedEventData.vatNo // "unknown")

    This will use "unknown" as the partition key if the VAT number is not present.

By using this approach, you can implement dynamic partitioning directly in Firehose without the need for a Lambda function, while also handling potential errors and edge cases in your data.

Sources
Partition streaming data in Amazon Data Firehose - Amazon Data Firehose
Enable dynamic partitioning in Amazon Data Firehose - Amazon Data Firehose
Amazon Kinesis Data Firehose FAQs - Streaming Data Pipeline - Amazon Web Services
Troubleshoot dynamic partitioning errors - Amazon Data Firehose
Kinesis Firehose Dynamic Partitoning with inline jq expression | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Thank you for your answer. I want to set this in my template.yaml but I got an error (#: extraneous key [DynamicPartitioningConfiguration] is not permitted) BusinessDPTransactionsFirehose: Type: AWS::KinesisFirehose::DeliveryStream Properties: DeliveryStreamName: !Join - '-' - - !Ref AWS::StackName - 'dp' - businessTransactionsDeliveryStream DeliveryStreamType: DirectPut S3DestinationConfiguration: BucketArn: !GetAtt BusinessTransactionsBucket.Arn RoleArn: !GetAtt FireHoseToS3BucketRole.Arn Prefix: "vatNo=!{partitionKeyFromQuery:vatNo}/" ErrorOutputPrefix: "error/" BufferingHints: # needs to be in between 60 and 900, 60 to have faster results in S3 for testing IntervalInSeconds: 60 # needs to be between 1 and 128, maximum right now because intervalInSeconds will allow fast delivery during testing SizeInMBs: 128 DynamicPartitioningConfiguration: Enabled: true RetryOptions: DurationInSeconds: 300 ProcessingConfiguration: Enabled: true Processors: - Type: MetadataExtraction Parameters: - ParameterName: MetadataExtractionQuery ParameterValue: "(.detail.body | fromjson).events[0].enrichedEventData.vatNo" - ParameterName: JsonParsingEngine ParameterValue: "JQ-1.6"

0

Thank for the help! It works :)

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.