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,*.yamlfiles, 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 24 options, which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Component configuration. | Comprehend2Configuration | ||
The Amazon Resource Name (ARN) of the endpoint to use for document classification. Required for classifyDocument operation. | String | ||
The language code of the input text. Required for all operations except detectDominantLanguage. Use a 2-letter ISO 639-1 code (e.g., 'en' for English, 'es' for Spanish). | String | ||
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 | |
Required The operation to perform. Enum values:
| detectDominantLanguage | Comprehend2Operations | |
Set the need for overriding the endpoint. This option needs to be used in combination with the uriEndpointOverride option. | false | boolean | |
If we want to use a POJO request as body or not. | false | boolean | |
The region in which the Comprehend client needs to work. When using this parameter, the configuration will expect the lowercase name of the region (for example, ap-east-1) You’ll need to use the name Region.EU_WEST_1.id(). Enum values:
| String | ||
Set the overriding uri endpoint. This option needs to be used in combination with overrideEndpoint option. | String | ||
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 | |
Autowired To use an existing configured AWS Comprehend client. | ComprehendClient | ||
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
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 | |
To define a proxy host when instantiating the Comprehend client. | String | ||
To define a proxy port when instantiating the Comprehend client. | Integer | ||
To define a proxy protocol when instantiating the Comprehend client. Enum values:
| HTTPS | Protocol | |
Amazon AWS Access Key. | String | ||
If using a profile credentials provider, this parameter will set the profile name. | String | ||
Amazon AWS Secret Key. | String | ||
Amazon AWS Session Token used when the user needs to assume an IAM role. | String | ||
If we want to trust all certificates in case of overriding the endpoint. | false | boolean | |
Set whether the Comprehend client should expect to load credentials through a default credentials provider or to expect static credentials to be passed in. | false | boolean | |
Set whether the Comprehend client should expect to load credentials through a profile credentials provider. | false | boolean | |
Set whether the Comprehend client should expect to use Session Credentials. This is useful in a situation in which the user needs to assume an IAM role for doing operations in Comprehend. | false | boolean |
Endpoint Options
The AWS Comprehend endpoint is configured using URI syntax:
aws2-comprehend:label
With the following path and query parameters:
Query Parameters (20 parameters)
| Name | Description | Default | Type |
|---|---|---|---|
The Amazon Resource Name (ARN) of the endpoint to use for document classification. Required for classifyDocument operation. | String | ||
The language code of the input text. Required for all operations except detectDominantLanguage. Use a 2-letter ISO 639-1 code (e.g., 'en' for English, 'es' for Spanish). | String | ||
Required The operation to perform. Enum values:
| detectDominantLanguage | Comprehend2Operations | |
Set the need for overriding the endpoint. This option needs to be used in combination with the uriEndpointOverride option. | false | boolean | |
If we want to use a POJO request as body or not. | false | boolean | |
The region in which the Comprehend client needs to work. When using this parameter, the configuration will expect the lowercase name of the region (for example, ap-east-1) You’ll need to use the name Region.EU_WEST_1.id(). Enum values:
| String | ||
Set the overriding uri endpoint. This option needs to be used in combination with overrideEndpoint option. | String | ||
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 | |
Autowired To use an existing configured AWS Comprehend client. | ComprehendClient | ||
To define a proxy host when instantiating the Comprehend client. | String | ||
To define a proxy port when instantiating the Comprehend client. | Integer | ||
To define a proxy protocol when instantiating the Comprehend client. Enum values:
| HTTPS | Protocol | |
Amazon AWS Access Key. | String | ||
If using a profile credentials provider, this parameter will set the profile name. | String | ||
Amazon AWS Secret Key. | String | ||
Amazon AWS Session Token used when the user needs to assume an IAM role. | String | ||
If we want to trust all certificates in case of overriding the endpoint. | false | boolean | |
Set whether the Comprehend client should expect to load credentials through a default credentials provider or to expect static credentials to be passed in. | false | boolean | |
Set whether the Comprehend client should expect to load credentials through a profile credentials provider. | false | boolean | |
Set whether the Comprehend client should expect to use Session Credentials. This is useful in a situation in which the user needs to assume an IAM role for doing operations in Comprehend. | false | boolean |
Message Headers
The AWS Comprehend component supports 7 message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelAwsComprehendOperation (producer) Constant: | The operation we want to perform. | String | |
CamelAwsComprehendLanguageCode (producer) Constant: | The language code of the input text. | String | |
CamelAwsComprehendEndpointArn (producer) Constant: | The Amazon Resource Name (ARN) of the endpoint to use for document classification. | String | |
CamelAwsComprehendDetectedLanguage (producer) Constant: | The detected dominant language. | String | |
CamelAwsComprehendDetectedLanguageScore (producer) Constant: | The detected dominant language score. | Float | |
CamelAwsComprehendDetectedSentiment (producer) Constant: | The detected sentiment. | String | |
CamelAwsComprehendDetectedSentimentScore (producer) Constant: | The detected sentiment scores. | SentimentScore |
Required Comprehend component options
You have to provide the amazonComprehendClient 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.accessKeyIdandaws.secretKey -
Environment variables -
AWS_ACCESS_KEY_IDandAWS_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_URIis 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 |
|---|---|
| Detects the dominant language of the input text. Returns language codes with confidence scores. |
| Identifies entities in the text such as persons, locations, organizations, dates, quantities, and more. |
| Extracts key phrases (noun phrases) that represent the main points in the text. |
| Analyzes the sentiment of the text and returns POSITIVE, NEGATIVE, NEUTRAL, or MIXED with confidence scores. |
| Performs syntactic analysis, providing part-of-speech tags for each word in the text. |
| Detects personally identifiable information (PII) such as emails, phone numbers, SSN, credit cards, etc. |
| Analyzes text for toxic content including profanity, hate speech, threats, and more. |
| Classifies a document using a custom classification model (requires a trained endpoint). |
| Checks if the text contains PII and returns labels indicating the types of PII found. |
Language Code Requirement
Most operations (except detectDominantLanguage) 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:
from("direct:detectLanguage")
.setBody(constant("This is a sample text written in English."))
.to("aws2-comprehend://test?operation=detectDominantLanguage&useDefaultCredentialsProvider=true®ion=us-east-1")
.log("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:
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®ion=us-east-1")
.log("Found ${body.size()} entities")
.split(body())
.log("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:
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®ion=us-east-1")
.log("Sentiment: ${header.CamelAwsComprehendDetectedSentiment}")
.log("Sentiment scores: ${header.CamelAwsComprehendDetectedSentimentScore}"); The sentiment will be one of: POSITIVE, NEGATIVE, NEUTRAL, or MIXED.
Detect Key Phrases
Extract key phrases from text:
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®ion=us-east-1")
.split(body())
.log("Key phrase: ${body.text} (score: ${body.score})"); Detect PII Entities
Identify personally identifiable information in text:
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®ion=us-east-1")
.split(body())
.log("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:
from("direct:detectSyntax")
.setBody(constant("The quick brown fox jumps over the lazy dog."))
.to("aws2-comprehend://test?operation=detectSyntax&languageCode=en&useDefaultCredentialsProvider=true®ion=us-east-1")
.split(body())
.log("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:
from("direct:detectToxic")
.setBody(constant("This is a friendly and polite message."))
.to("aws2-comprehend://test?operation=detectToxicContent&languageCode=en&useDefaultCredentialsProvider=true®ion=us-east-1")
.log("Toxicity analysis: ${body}"); Returns toxicity scores for categories like PROFANITY, HATE_SPEECH, THREAT, INSULT, etc.
Using a POJO as body
For more control, you can pass AWS SDK request objects directly by setting pojoRequest=true:
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®ion=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:
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®ion=us-east-1")
.log("Detected entities: ${body}"); Using with Static Credentials
If you need to use explicit credentials:
from("direct:withCredentials")
.setBody(constant("Bonjour, comment allez-vous?"))
.to("aws2-comprehend://test?operation=detectDominantLanguage&accessKey=YOUR_ACCESS_KEY&secretKey=YOUR_SECRET_KEY®ion=eu-west-1")
.log("Detected language: ${header.CamelAwsComprehendDetectedLanguage}"); Content Moderation Pipeline Example
A practical example combining multiple Comprehend operations for content moderation:
from("direct:moderateContent")
.setHeader("originalText", simple("${body}"))
// First, detect the language
.to("aws2-comprehend://test?operation=detectDominantLanguage&useDefaultCredentialsProvider=true®ion=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®ion=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:
from("kafka:customer-feedback")
.log("Processing feedback: ${body}")
// Analyze sentiment
.to("aws2-comprehend://test?operation=detectSentiment&languageCode=en&useDefaultCredentialsProvider=true®ion=us-east-1")
.setHeader("sentiment", simple("${header.CamelAwsComprehendDetectedSentiment}"))
// Extract key phrases
.to("aws2-comprehend://test?operation=detectKeyPhrases&languageCode=en&useDefaultCredentialsProvider=true®ion=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.
Spring Boot Auto-Configuration
When using aws2-comprehend with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-aws2-comprehend-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> The component supports 25 options, which are listed below.