Camel Components

AWS Comprehend

Since Camel 4.18

Only producer is supported

The AWS2 Comprehend component supports natural language processing (NLP) operations using AWS Comprehend. It provides capabilities such as:

  • Language detection

  • Entity recognition (people, places, organizations, dates, etc.)

  • Key phrase extraction

  • Sentiment analysis

  • Syntax analysis (part-of-speech tagging)

  • PII (Personally Identifiable Information) detection

  • Toxic content detection

  • Custom document classification

Prerequisites

You must have a valid Amazon Web Services developer account, and be signed up to use Amazon Comprehend. More information is available at Amazon Comprehend.

URI Format

aws2-comprehend://label[?options]

You can append query options to the URI in the following format:

?options=value&option2=value&…​

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The AWS Comprehend component supports the following options which are listed below.

Name Description Default Type

configuration (producer)

The configuration that the endpoints of this component start from. Each endpoint gets a copy of it, and the options of the endpoint replace its values.

Comprehend2Configuration

endpointArn (producer)

The ARN of the Amazon Comprehend endpoint that classifies the text, for example the endpoint of a custom document classifier. It is required for classifyDocument and not used by the other operations. The CamelAwsComprehendEndpointArn header replaces this option. It is not used when pojoRequest is true.

String

languageCode (producer)

The language of the text, as a language code of Amazon Comprehend: en, es, fr, de, it, pt, ar, hi, ja, ko, zh or zh-TW. Some operations support fewer of them, such as detectSyntax and detectToxicContent. It is required for every operation except detectDominantLanguage and classifyDocument, which do not use it. The CamelAwsComprehendLanguageCode header replaces this option. It is not used when pojoRequest is true.

String

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

operation (producer)

Required The operation that the producer performs. The CamelAwsComprehendOperation header replaces this option.

Enum values:

  • detectDominantLanguage

  • detectEntities

  • detectKeyPhrases

  • detectSentiment

  • detectSyntax

  • detectPiiEntities

  • detectToxicContent

  • classifyDocument

  • containsPiiEntities

detectDominantLanguage

Comprehend2Operations

overrideEndpoint (producer)

Whether to send the requests to the URI of the uriEndpointOverride option instead of the endpoint of the region. It has no effect when uriEndpointOverride is not set.

false

boolean

pojoRequest (producer)

Whether the body is the request of the AWS SDK for the operation, such as a DetectSentimentRequest for detectSentiment. If true, the producer sends that request as it is, without the languageCode and endpointArn options or their headers, and a body of another type fails. If false, the producer builds the request from the text of the body, the options and the headers.

false

boolean

region (producer)

The AWS region of Amazon Comprehend, as a region ID such as eu-west-1, which is the value of Region.EU_WEST_1.id(). If not set, the default region provider chain of the AWS SDK is used. Amazon Comprehend is not available in every region.

Enum values:

  • ap-south-2

  • ap-south-1

  • eu-south-1

  • eu-south-2

  • us-gov-east-1

  • me-central-1

  • il-central-1

  • ca-central-1

  • eu-central-1

  • us-iso-west-1

  • eu-central-2

  • eu-isoe-west-1

  • us-west-1

  • us-west-2

  • af-south-1

  • eu-north-1

  • eu-west-3

  • eu-west-2

  • eu-west-1

  • ap-northeast-3

  • ap-northeast-2

  • ap-northeast-1

  • me-south-1

  • sa-east-1

  • ap-east-1

  • cn-north-1

  • ca-west-1

  • us-gov-west-1

  • ap-southeast-1

  • ap-southeast-2

  • us-iso-east-1

  • ap-southeast-3

  • ap-southeast-4

  • us-east-1

  • us-east-2

  • cn-northwest-1

  • us-isob-east-1

  • aws-global

  • aws-cn-global

  • aws-us-gov-global

  • aws-iso-global

  • aws-iso-b-global

String

uriEndpointOverride (producer)

The URI of the endpoint to use instead of the endpoint of the region, for example http://localhost:4566 for a local emulator. It is used only when overrideEndpoint is true.

String

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

comprehendClient (advanced)

Autowired To use a custom software.amazon.awssdk.services.comprehend.ComprehendClient, which is autowired when the registry has a single one. When this option is set, the credentials, region, proxy, endpoint override and trustAllCertificates options are not used. The endpoint does not close a client that is set or autowired.

ComprehendClient

healthCheckConsumerEnabled (health)

Used for enabling or disabling all consumer based health checks from this component.

true

boolean

healthCheckProducerEnabled (health)

Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true.

true

boolean

proxyHost (proxy)

The host name of the proxy that the HTTP client of Amazon Comprehend connects through. The proxy is used only when the proxyPort option is set too.

String

proxyPort (proxy)

The port of the proxy that the HTTP client of Amazon Comprehend connects through. The proxy is used only when the proxyHost option is set too.

Integer

proxyProtocol (proxy)

The protocol that the HTTP client of Amazon Comprehend uses to connect to the proxy. It is used only when the proxyHost and proxyPort options are set.

Enum values:

  • HTTP

  • HTTPS

HTTPS

Protocol

accessKey (security)

The access key ID of the AWS credentials, used together with the secretKey option. The endpoint requires it, unless comprehendClient is set or useDefaultCredentialsProvider or useProfileCredentialsProvider is true. It is also required when useSessionCredentials is true.

String

profileCredentialsName (security)

The name of the profile in the AWS shared credentials and config files, used when useProfileCredentialsProvider is true. If not set, the AWS SDK uses its default profile, which the AWS_PROFILE environment variable can change.

String

secretKey (security)

The secret access key of the AWS credentials, used together with the accessKey option. The endpoint requires it, unless comprehendClient is set or useDefaultCredentialsProvider or useProfileCredentialsProvider is true. It is also required when useSessionCredentials is true.

String

sessionToken (security)

The session token of temporary AWS credentials, for example of an assumed IAM role. It is used only when useSessionCredentials is true, together with the accessKey and secretKey options. All three are then required: if one of the three is missing, creating the client fails.

String

trustAllCertificates (security)

Whether the HTTP client of Amazon Comprehend trusts every TLS certificate without checking it. It applies to any endpoint, not only to an overridden one, so use it only for testing, for example against a local emulator.

false

boolean

useDefaultCredentialsProvider (security)

Whether to load the credentials with the default credentials provider chain of the AWS SDK, which looks in system properties, environment variables, the shared credentials and config files, and the container or instance role. It takes precedence over the other credentials options.

false

boolean

useProfileCredentialsProvider (security)

Whether to load the credentials from a profile of the AWS shared credentials and config files, the one of the profileCredentialsName option. It is not used when useDefaultCredentialsProvider is true, and it takes precedence over the session and static credentials.

false

boolean

useSessionCredentials (security)

Whether to use temporary credentials made of the accessKey, secretKey and sessionToken options, for example those of an assumed IAM role. It is not used when useDefaultCredentialsProvider or useProfileCredentialsProvider is true.

false

boolean

Endpoint Options

The AWS Comprehend endpoint is configured using URI syntax:

aws2-comprehend:label

With the following path and query parameters:

Path Parameters

Name Description Default Type

label (producer)

Required A logical name of the endpoint, which you choose freely. The producer does not use it: the operation and its input are given by the options, the headers and the body.

String

Query Parameters

Name Description Default Type

endpointArn (producer)

The ARN of the Amazon Comprehend endpoint that classifies the text, for example the endpoint of a custom document classifier. It is required for classifyDocument and not used by the other operations. The CamelAwsComprehendEndpointArn header replaces this option. It is not used when pojoRequest is true.

String

languageCode (producer)

The language of the text, as a language code of Amazon Comprehend: en, es, fr, de, it, pt, ar, hi, ja, ko, zh or zh-TW. Some operations support fewer of them, such as detectSyntax and detectToxicContent. It is required for every operation except detectDominantLanguage and classifyDocument, which do not use it. The CamelAwsComprehendLanguageCode header replaces this option. It is not used when pojoRequest is true.

String

operation (producer)

Required The operation that the producer performs. The CamelAwsComprehendOperation header replaces this option.

Enum values:

  • detectDominantLanguage

  • detectEntities

  • detectKeyPhrases

  • detectSentiment

  • detectSyntax

  • detectPiiEntities

  • detectToxicContent

  • classifyDocument

  • containsPiiEntities

detectDominantLanguage

Comprehend2Operations

overrideEndpoint (producer)

Whether to send the requests to the URI of the uriEndpointOverride option instead of the endpoint of the region. It has no effect when uriEndpointOverride is not set.

false

boolean

pojoRequest (producer)

Whether the body is the request of the AWS SDK for the operation, such as a DetectSentimentRequest for detectSentiment. If true, the producer sends that request as it is, without the languageCode and endpointArn options or their headers, and a body of another type fails. If false, the producer builds the request from the text of the body, the options and the headers.

false

boolean

region (producer)

The AWS region of Amazon Comprehend, as a region ID such as eu-west-1, which is the value of Region.EU_WEST_1.id(). If not set, the default region provider chain of the AWS SDK is used. Amazon Comprehend is not available in every region.

Enum values:

  • ap-south-2

  • ap-south-1

  • eu-south-1

  • eu-south-2

  • us-gov-east-1

  • me-central-1

  • il-central-1

  • ca-central-1

  • eu-central-1

  • us-iso-west-1

  • eu-central-2

  • eu-isoe-west-1

  • us-west-1

  • us-west-2

  • af-south-1

  • eu-north-1

  • eu-west-3

  • eu-west-2

  • eu-west-1

  • ap-northeast-3

  • ap-northeast-2

  • ap-northeast-1

  • me-south-1

  • sa-east-1

  • ap-east-1

  • cn-north-1

  • ca-west-1

  • us-gov-west-1

  • ap-southeast-1

  • ap-southeast-2

  • us-iso-east-1

  • ap-southeast-3

  • ap-southeast-4

  • us-east-1

  • us-east-2

  • cn-northwest-1

  • us-isob-east-1

  • aws-global

  • aws-cn-global

  • aws-us-gov-global

  • aws-iso-global

  • aws-iso-b-global

String

uriEndpointOverride (producer)

The URI of the endpoint to use instead of the endpoint of the region, for example http://localhost:4566 for a local emulator. It is used only when overrideEndpoint is true.

String

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

comprehendClient (advanced)

Autowired To use a custom software.amazon.awssdk.services.comprehend.ComprehendClient, which is autowired when the registry has a single one. When this option is set, the credentials, region, proxy, endpoint override and trustAllCertificates options are not used. The endpoint does not close a client that is set or autowired.

ComprehendClient

proxyHost (proxy)

The host name of the proxy that the HTTP client of Amazon Comprehend connects through. The proxy is used only when the proxyPort option is set too.

String

proxyPort (proxy)

The port of the proxy that the HTTP client of Amazon Comprehend connects through. The proxy is used only when the proxyHost option is set too.

Integer

proxyProtocol (proxy)

The protocol that the HTTP client of Amazon Comprehend uses to connect to the proxy. It is used only when the proxyHost and proxyPort options are set.

Enum values:

  • HTTP

  • HTTPS

HTTPS

Protocol

accessKey (security)

The access key ID of the AWS credentials, used together with the secretKey option. The endpoint requires it, unless comprehendClient is set or useDefaultCredentialsProvider or useProfileCredentialsProvider is true. It is also required when useSessionCredentials is true.

String

profileCredentialsName (security)

The name of the profile in the AWS shared credentials and config files, used when useProfileCredentialsProvider is true. If not set, the AWS SDK uses its default profile, which the AWS_PROFILE environment variable can change.

String

secretKey (security)

The secret access key of the AWS credentials, used together with the accessKey option. The endpoint requires it, unless comprehendClient is set or useDefaultCredentialsProvider or useProfileCredentialsProvider is true. It is also required when useSessionCredentials is true.

String

sessionToken (security)

The session token of temporary AWS credentials, for example of an assumed IAM role. It is used only when useSessionCredentials is true, together with the accessKey and secretKey options. All three are then required: if one of the three is missing, creating the client fails.

String

trustAllCertificates (security)

Whether the HTTP client of Amazon Comprehend trusts every TLS certificate without checking it. It applies to any endpoint, not only to an overridden one, so use it only for testing, for example against a local emulator.

false

boolean

useDefaultCredentialsProvider (security)

Whether to load the credentials with the default credentials provider chain of the AWS SDK, which looks in system properties, environment variables, the shared credentials and config files, and the container or instance role. It takes precedence over the other credentials options.

false

boolean

useProfileCredentialsProvider (security)

Whether to load the credentials from a profile of the AWS shared credentials and config files, the one of the profileCredentialsName option. It is not used when useDefaultCredentialsProvider is true, and it takes precedence over the session and static credentials.

false

boolean

useSessionCredentials (security)

Whether to use temporary credentials made of the accessKey, secretKey and sessionToken options, for example those of an assumed IAM role. It is not used when useDefaultCredentialsProvider or useProfileCredentialsProvider is true.

false

boolean

Message Headers

The AWS Comprehend component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelAwsComprehendOperation (producer)

Constant: OPERATION

The operation that the producer performs, which replaces the operation option. The value is the name of the operation, such as detectSentiment.

String

CamelAwsComprehendLanguageCode (producer)

Constant: LANGUAGE_CODE

The language code of the text, such as en, which replaces the languageCode option. detectDominantLanguage and classifyDocument do not use it, and it is not used when pojoRequest is true.

String

CamelAwsComprehendEndpointArn (producer)

Constant: ENDPOINT_ARN

The ARN of the Amazon Comprehend endpoint that classifies the text, which replaces the endpointArn option. Only classifyDocument uses it, and it is not used when pojoRequest is true.

String

CamelAwsComprehendDetectedLanguage (producer)

Constant: DETECTED_LANGUAGE

The RFC 5646 language code, such as en, of the first language in the response of detectDominantLanguage. The header is not set when no language is detected.

String

CamelAwsComprehendDetectedLanguageScore (producer)

Constant: DETECTED_LANGUAGE_SCORE

The confidence score, from 0 to 1, of the language in the CamelAwsComprehendDetectedLanguage header.

Float

CamelAwsComprehendDetectedSentiment (producer)

Constant: DETECTED_SENTIMENT

The sentiment that detectSentiment found in the text: POSITIVE, NEGATIVE, NEUTRAL or MIXED.

String

CamelAwsComprehendDetectedSentimentScore (producer)

Constant: DETECTED_SENTIMENT_SCORE

The confidence scores of detectSentiment for each sentiment: positive, negative, neutral and mixed.

SentimentScore

Message Body

The message body at each side of the AWS Comprehend component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

text, bytes, stream or file

The text to analyze, for every operation: bytes, a stream or a file are read as text, a Map or a POJO is analyzed as its toString(), so take the text out of it first, and no body fails. With pojoRequest=true the request of the AWS SDK for the operation, such as a DetectSentimentRequest: another body fails.

Producer output

list

A List of what is found: the languages for detectDominantLanguage, the default, with the first one and its score in headers, and the entities, key phrases, syntax tokens, PII entities or toxic labels of the other operations. The DetectSentimentResponse of the AWS SDK for detectSentiment, with the sentiment and its scores in headers, and the ClassifyDocumentResponse for classifyDocument. The text is not kept: store it in a variable to analyze it again.

  • With operation=detectSentiment: pojo (software.amazon.awssdk.services.comprehend.model.DetectSentimentResponse)

  • With operation=classifyDocument: pojo (software.amazon.awssdk.services.comprehend.model.ClassifyDocumentResponse)

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Required Comprehend component options

You have to provide the comprehendClient in the Registry or your accessKey and secretKey to access the Amazon Comprehend service.

Usage

Static credentials, Default Credential Provider and Profile Credentials Provider

You have the possibility of avoiding the usage of explicit static credentials by specifying the useDefaultCredentialsProvider option and set it to true.

The order of evaluation for Default Credentials Provider is the following:

  • Java system properties - aws.accessKeyId and aws.secretKey

  • Environment variables - AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

  • Web Identity Token from AWS STS.

  • The shared credentials and config files.

  • Amazon ECS container credentials - loaded from the Amazon ECS if the environment variable AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is set.

  • Amazon EC2 Instance profile credentials.

You have also the possibility of using Profile Credentials Provider, by specifying the useProfileCredentialsProvider option to true and profileCredentialsName to the profile name.

Only one of static, default and profile credentials could be used at the same time.

For more information about this you can look at AWS credentials documentation

Comprehend Producer operations

Camel-AWS Comprehend component provides the following operations on the producer side:

Operation Description

detectDominantLanguage

Detects the dominant language of the input text. Returns language codes with confidence scores.

detectEntities

Identifies entities in the text such as persons, locations, organizations, dates, quantities, and more.

detectKeyPhrases

Extracts key phrases (noun phrases) that represent the main points in the text.

detectSentiment

Analyzes the sentiment of the text and returns POSITIVE, NEGATIVE, NEUTRAL, or MIXED with confidence scores.

detectSyntax

Performs syntactic analysis, providing part-of-speech tags for each word in the text.

detectPiiEntities

Detects personally identifiable information (PII) such as emails, phone numbers, SSN, credit cards, etc.

detectToxicContent

Analyzes text for toxic content including profanity, hate speech, threats, and more.

classifyDocument

Classifies a document using a custom classification model (requires a trained endpoint).

containsPiiEntities

Checks if the text contains PII and returns labels indicating the types of PII found.

Language Code Requirement

Most operations (except detectDominantLanguage and classifyDocument) require a language code to be specified. You can set it:

  • As an endpoint option: languageCode=en

  • As a message header: CamelAwsComprehendLanguageCode

Common language codes: en (English), es (Spanish), fr (French), de (German), it (Italian), pt (Portuguese), ja (Japanese), ko (Korean), zh (Chinese), ar (Arabic).

Examples

Detect Dominant Language

Automatically detect the language of the input text:

  • Java

  • XML

  • YAML

from("direct:detectLanguage")
    .setBody(constant("This is a sample text written in English."))
    .to("aws2-comprehend://test?operation=detectDominantLanguage&useDefaultCredentialsProvider=true&region=us-east-1")
    .log("Detected language: ${header.CamelAwsComprehendDetectedLanguage}");
<route>
  <from uri="direct:detectLanguage"/>
  <setBody>
    <constant>This is a sample text written in English.</constant>
  </setBody>
  <to uri="aws2-comprehend://test?operation=detectDominantLanguage&amp;useDefaultCredentialsProvider=true&amp;region=us-east-1"/>
  <log message="Detected language: ${header.CamelAwsComprehendDetectedLanguage}"/>
</route>
- route:
    from:
      uri: direct:detectLanguage
      steps:
        - setBody:
            constant: "This is a sample text written in English."
        - to:
            uri: aws2-comprehend://test
            parameters:
              operation: detectDominantLanguage
              useDefaultCredentialsProvider: true
              region: us-east-1
        - log:
            message: "Detected language: ${header.CamelAwsComprehendDetectedLanguage}"

The result body will contain a list of DominantLanguage objects with language codes and confidence scores. The detected language code and score are also set as message headers.

Detect Entities

Extract named entities from text:

  • Java

  • XML

  • YAML

from("direct:detectEntities")
    .setBody(constant("Amazon was founded by Jeff Bezos in Seattle, Washington in 1994."))
    .to("aws2-comprehend://test?operation=detectEntities&languageCode=en&useDefaultCredentialsProvider=true&region=us-east-1")
    .log("Found ${body.size()} entities")
    .split(body())
        .log("Entity: ${body.text} - Type: ${body.type} - Score: ${body.score}");
<route>
  <from uri="direct:detectEntities"/>
  <setBody>
    <constant>Amazon was founded by Jeff Bezos in Seattle, Washington in 1994.</constant>
  </setBody>
  <to uri="aws2-comprehend://test?operation=detectEntities&amp;languageCode=en&amp;useDefaultCredentialsProvider=true&amp;region=us-east-1"/>
  <log message="Found ${body.size()} entities"/>
  <split>
    <simple>${body}</simple>
    <log message="Entity: ${body.text} - Type: ${body.type} - Score: ${body.score}"/>
  </split>
</route>
- route:
    from:
      uri: direct:detectEntities
      steps:
        - setBody:
            constant: "Amazon was founded by Jeff Bezos in Seattle, Washington in 1994."
        - to:
            uri: aws2-comprehend://test
            parameters:
              operation: detectEntities
              languageCode: en
              useDefaultCredentialsProvider: true
              region: us-east-1
        - log:
            message: "Found ${body.size()} entities"
        - split:
            simple: "${body}"
            steps:
              - log:
                  message: "Entity: ${body.text} - Type: ${body.type} - Score: ${body.score}"

This will detect entities like: * Jeff Bezos (PERSON) * Amazon (ORGANIZATION) * Seattle (LOCATION) * Washington (LOCATION) * 1994 (DATE)

Detect Sentiment

Analyze the sentiment of text:

  • Java

  • XML

  • YAML

from("direct:analyzeSentiment")
    .setBody(constant("I love this product! It works perfectly and exceeded my expectations."))
    .to("aws2-comprehend://test?operation=detectSentiment&languageCode=en&useDefaultCredentialsProvider=true&region=us-east-1")
    .log("Sentiment: ${header.CamelAwsComprehendDetectedSentiment}")
    .log("Sentiment scores: ${header.CamelAwsComprehendDetectedSentimentScore}");
<route>
  <from uri="direct:analyzeSentiment"/>
  <setBody>
    <constant>I love this product! It works perfectly and exceeded my expectations.</constant>
  </setBody>
  <to uri="aws2-comprehend://test?operation=detectSentiment&amp;languageCode=en&amp;useDefaultCredentialsProvider=true&amp;region=us-east-1"/>
  <log message="Sentiment: ${header.CamelAwsComprehendDetectedSentiment}"/>
  <log message="Sentiment scores: ${header.CamelAwsComprehendDetectedSentimentScore}"/>
</route>
- route:
    from:
      uri: direct:analyzeSentiment
      steps:
        - setBody:
            constant: "I love this product! It works perfectly and exceeded my expectations."
        - to:
            uri: aws2-comprehend://test
            parameters:
              operation: detectSentiment
              languageCode: en
              useDefaultCredentialsProvider: true
              region: us-east-1
        - log:
            message: "Sentiment: ${header.CamelAwsComprehendDetectedSentiment}"
        - log:
            message: "Sentiment scores: ${header.CamelAwsComprehendDetectedSentimentScore}"

The sentiment will be one of: POSITIVE, NEGATIVE, NEUTRAL, or MIXED.

Detect Key Phrases

Extract key phrases from text:

  • Java

  • XML

  • YAML

from("direct:detectKeyPhrases")
    .setBody(constant("Apache Camel is an open source integration framework that provides rule-based routing and mediation."))
    .to("aws2-comprehend://test?operation=detectKeyPhrases&languageCode=en&useDefaultCredentialsProvider=true&region=us-east-1")
    .split(body())
        .log("Key phrase: ${body.text} (score: ${body.score})");
<route>
  <from uri="direct:detectKeyPhrases"/>
  <setBody>
    <constant>Apache Camel is an open source integration framework that provides rule-based routing and mediation.</constant>
  </setBody>
  <to uri="aws2-comprehend://test?operation=detectKeyPhrases&amp;languageCode=en&amp;useDefaultCredentialsProvider=true&amp;region=us-east-1"/>
  <split>
    <simple>${body}</simple>
    <log message="Key phrase: ${body.text} (score: ${body.score})"/>
  </split>
</route>
- route:
    from:
      uri: direct:detectKeyPhrases
      steps:
        - setBody:
            constant: "Apache Camel is an open source integration framework that provides rule-based routing and mediation."
        - to:
            uri: aws2-comprehend://test
            parameters:
              operation: detectKeyPhrases
              languageCode: en
              useDefaultCredentialsProvider: true
              region: us-east-1
        - split:
            simple: "${body}"
            steps:
              - log:
                  message: "Key phrase: ${body.text} (score: ${body.score})"

Detect PII Entities

Identify personally identifiable information in text:

  • Java

  • XML

  • YAML

from("direct:detectPii")
    .setBody(constant("Contact John at john.doe@example.com or call 555-123-4567. His SSN is 123-45-6789."))
    .to("aws2-comprehend://test?operation=detectPiiEntities&languageCode=en&useDefaultCredentialsProvider=true&region=us-east-1")
    .split(body())
        .log("PII found: ${body.type} at position ${body.beginOffset}-${body.endOffset}");
<route>
  <from uri="direct:detectPii"/>
  <setBody>
    <constant>Contact John at john.doe@example.com or call 555-123-4567. His SSN is 123-45-6789.</constant>
  </setBody>
  <to uri="aws2-comprehend://test?operation=detectPiiEntities&amp;languageCode=en&amp;useDefaultCredentialsProvider=true&amp;region=us-east-1"/>
  <split>
    <simple>${body}</simple>
    <log message="PII found: ${body.type} at position ${body.beginOffset}-${body.endOffset}"/>
  </split>
</route>
- route:
    from:
      uri: direct:detectPii
      steps:
        - setBody:
            constant: "Contact John at john.doe@example.com or call 555-123-4567. His SSN is 123-45-6789."
        - to:
            uri: aws2-comprehend://test
            parameters:
              operation: detectPiiEntities
              languageCode: en
              useDefaultCredentialsProvider: true
              region: us-east-1
        - split:
            simple: "${body}"
            steps:
              - log:
                  message: "PII found: ${body.type} at position ${body.beginOffset}-${body.endOffset}"

This detects PII types such as EMAIL, PHONE, SSN, CREDIT_DEBIT_NUMBER, etc.

Detect Syntax (Part-of-Speech Tagging)

Analyze the grammatical structure of text:

  • Java

  • XML

  • YAML

from("direct:detectSyntax")
    .setBody(constant("The quick brown fox jumps over the lazy dog."))
    .to("aws2-comprehend://test?operation=detectSyntax&languageCode=en&useDefaultCredentialsProvider=true&region=us-east-1")
    .split(body())
        .log("Token: ${body.text} - POS: ${body.partOfSpeech.tag}");
<route>
  <from uri="direct:detectSyntax"/>
  <setBody>
    <constant>The quick brown fox jumps over the lazy dog.</constant>
  </setBody>
  <to uri="aws2-comprehend://test?operation=detectSyntax&amp;languageCode=en&amp;useDefaultCredentialsProvider=true&amp;region=us-east-1"/>
  <split>
    <simple>${body}</simple>
    <log message="Token: ${body.text} - POS: ${body.partOfSpeech.tag}"/>
  </split>
</route>
- route:
    from:
      uri: direct:detectSyntax
      steps:
        - setBody:
            constant: "The quick brown fox jumps over the lazy dog."
        - to:
            uri: aws2-comprehend://test
            parameters:
              operation: detectSyntax
              languageCode: en
              useDefaultCredentialsProvider: true
              region: us-east-1
        - split:
            simple: "${body}"
            steps:
              - log:
                  message: "Token: ${body.text} - POS: ${body.partOfSpeech.tag}"

Returns part-of-speech tags like NOUN, VERB, ADJ (adjective), DET (determiner), etc.

Detect Toxic Content

Analyze text for toxic content:

  • Java

  • XML

  • YAML

from("direct:detectToxic")
    .setBody(constant("This is a friendly and polite message."))
    .to("aws2-comprehend://test?operation=detectToxicContent&languageCode=en&useDefaultCredentialsProvider=true&region=us-east-1")
    .log("Toxicity analysis: ${body}");
<route>
  <from uri="direct:detectToxic"/>
  <setBody>
    <constant>This is a friendly and polite message.</constant>
  </setBody>
  <to uri="aws2-comprehend://test?operation=detectToxicContent&amp;languageCode=en&amp;useDefaultCredentialsProvider=true&amp;region=us-east-1"/>
  <log message="Toxicity analysis: ${body}"/>
</route>
- route:
    from:
      uri: direct:detectToxic
      steps:
        - setBody:
            constant: "This is a friendly and polite message."
        - to:
            uri: aws2-comprehend://test
            parameters:
              operation: detectToxicContent
              languageCode: en
              useDefaultCredentialsProvider: true
              region: us-east-1
        - log:
            message: "Toxicity analysis: ${body}"

Returns toxicity scores for categories like PROFANITY, HATE_SPEECH, VIOLENCE_OR_THREAT, INSULT, etc.

Using a POJO as body

For more control, you can pass AWS SDK request objects directly by setting pojoRequest=true:

Java-only: uses lambda expression and AWS SDK request builder API
from("direct:pojoRequest")
    .setBody(exchange -> DetectSentimentRequest.builder()
            .text("I am very happy with this service!")
            .languageCode("en")
            .build())
    .to("aws2-comprehend://test?operation=detectSentiment&pojoRequest=true&useDefaultCredentialsProvider=true&region=us-east-1")
    .log("Sentiment: ${header.CamelAwsComprehendDetectedSentiment}");

Setting Operation and Language via Headers

You can dynamically set the operation and language code using message headers:

Java-only: uses Java enum constant for operation header value
from("direct:dynamicOperation")
    .setHeader("CamelAwsComprehendOperation", constant(Comprehend2Operations.detectEntities))
    .setHeader("CamelAwsComprehendLanguageCode", constant("en"))
    .setBody(constant("Apple Inc. is headquartered in Cupertino, California."))
    .to("aws2-comprehend://test?useDefaultCredentialsProvider=true&region=us-east-1")
    .log("Detected entities: ${body}");

Using with Static Credentials

If you need to use explicit credentials:

  • Java

  • XML

  • YAML

from("direct:withCredentials")
    .setBody(constant("Bonjour, comment allez-vous?"))
    .to("aws2-comprehend://test?operation=detectDominantLanguage&accessKey=YOUR_ACCESS_KEY&secretKey=YOUR_SECRET_KEY&region=eu-west-1")
    .log("Detected language: ${header.CamelAwsComprehendDetectedLanguage}");
<route>
  <from uri="direct:withCredentials"/>
  <setBody>
    <constant>Bonjour, comment allez-vous?</constant>
  </setBody>
  <to uri="aws2-comprehend://test?operation=detectDominantLanguage&amp;accessKey=YOUR_ACCESS_KEY&amp;secretKey=YOUR_SECRET_KEY&amp;region=eu-west-1"/>
  <log message="Detected language: ${header.CamelAwsComprehendDetectedLanguage}"/>
</route>
- route:
    from:
      uri: direct:withCredentials
      steps:
        - setBody:
            constant: "Bonjour, comment allez-vous?"
        - to:
            uri: aws2-comprehend://test
            parameters:
              operation: detectDominantLanguage
              accessKey: YOUR_ACCESS_KEY
              secretKey: YOUR_SECRET_KEY
              region: eu-west-1
        - log:
            message: "Detected language: ${header.CamelAwsComprehendDetectedLanguage}"

Content Moderation Pipeline Example

A practical example combining multiple Comprehend operations for content moderation:

Java-only: uses choice/when/otherwise with inline comments
from("direct:moderateContent")
    .setHeader("originalText", simple("${body}"))
    // First, detect the language
    .to("aws2-comprehend://test?operation=detectDominantLanguage&useDefaultCredentialsProvider=true&region=us-east-1")
    .setHeader("detectedLanguage", simple("${header.CamelAwsComprehendDetectedLanguage}"))
    .log("Language detected: ${header.detectedLanguage}")
    // Restore original text and check for toxic content
    .setBody(simple("${header.originalText}"))
    .setHeader("CamelAwsComprehendLanguageCode", simple("${header.detectedLanguage}"))
    .to("aws2-comprehend://test?operation=detectToxicContent&useDefaultCredentialsProvider=true&region=us-east-1")
    .choice()
        .when(simple("${body[0].toxicity} > 0.5"))
            .log("WARNING: High toxicity detected!")
            .to("direct:flagContent")
        .otherwise()
            .log("Content is safe")
            .to("direct:approveContent");

Customer Feedback Analysis Example

Analyze customer feedback for sentiment and key topics:

Java-only: uses body() method reference and choice/when/otherwise with inline comments
from("kafka:customer-feedback")
    .log("Processing feedback: ${body}")
    // Keep the text, as each operation replaces the body with its result
    .setVariable("feedback", body())
    // Analyze sentiment
    .to("aws2-comprehend://test?operation=detectSentiment&languageCode=en&useDefaultCredentialsProvider=true&region=us-east-1")
    .setHeader("sentiment", simple("${header.CamelAwsComprehendDetectedSentiment}"))
    // Restore the text and extract key phrases
    .setBody(variable("feedback"))
    .to("aws2-comprehend://test?operation=detectKeyPhrases&languageCode=en&useDefaultCredentialsProvider=true&region=us-east-1")
    .setHeader("keyPhrases", body())
    .log("Sentiment: ${header.sentiment}, Key topics: ${header.keyPhrases}")
    // Route based on sentiment
    .choice()
        .when(simple("${header.sentiment} == 'NEGATIVE'"))
            .to("direct:handleNegativeFeedback")
        .when(simple("${header.sentiment} == 'POSITIVE'"))
            .to("direct:handlePositiveFeedback")
        .otherwise()
            .to("direct:handleNeutralFeedback");

Dependencies

Maven users will need to add the following dependency to their pom.xml.

pom.xml

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-aws2-comprehend</artifactId>
    <version>${camel-version}</version>
</dependency>

where ${camel-version} must be replaced by the actual version of Camel.