Google Sheets Stream
Since Camel 2.23
Only consumer is supported
The Google Sheets component provides access to Sheets via the Google Sheets Web APIs.
Google Sheets uses the OAuth 2.0 protocol for authenticating a Google account and authorizing access to user data. Before you can use this component, you will need to create an account and generate OAuth credentials. Credentials consist of a clientId, clientSecret, and a refreshToken. A handy resource for generating a long-lived refreshToken is the OAuth playground.
In the case of a service account, credentials consist of a JSON-file (serviceAccountKey). You can also use delegation domain-wide authority (delegate) and one, several, or all possible Sheets API Auth Scopes.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-google-sheets</artifactId>
<!-- use the same version as your Camel core version -->
<version>x.x.x</version>
</dependency> URI Format
The Google Sheets Component uses the following URI format:
google-sheets-stream:spreadsheetId?[options]
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 Google Sheets Stream component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The application name that the client sends in the User-Agent header of each request, such as camel-google-sheets/1.0. | String | ||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
The OAuth 2.0 client ID of the application. When clientId and clientSecret are both set, the client uses OAuth 2.0 with accessToken and refreshToken; otherwise it uses serviceAccountKey, or the Application Default Credentials when that is not set either. | String | ||
The shared configuration, whose options the endpoints use as defaults. | GoogleSheetsStreamConfiguration | ||
The email address of the user that the service account impersonates, with domain-wide delegation. Used only with serviceAccountKey. | String | ||
Whether the spreadsheet that a poll gets includes the grid data: the cells of each sheet with their values and formats. Only used when range is not set. | false | boolean | |
Whether the values of a range are grouped by rows (ROWS) or by columns (COLUMNS). Only used when range is set. Enum values:
| ROWS | String | |
The maximum number of rows (or columns, by majorDimension) kept of each range: the values after it are dropped from the ValueRange, or with splitResults, no message is sent for them. Only used when range is set. The default 0 means no limit. | int | ||
The ranges whose values each poll gets, in A1 notation and separated by commas, such as Sheet1!A1:C10. When not set, each poll gets the whole spreadsheet instead. | String | ||
The OAuth 2.0 scopes to request, separated by commas, such as https://www.googleapis.com/auth/spreadsheets.readonly (see SheetsScopes). They apply to serviceAccountKey and to the Application Default Credentials; with clientId and clientSecret the scopes are those granted with the refresh token, and this option is not used. No scopes are requested by default. | String | ||
Whether to send one message per row (or column, by majorDimension) of each range, with the List of its values as body, instead of one message per range with its ValueRange. Only used when range is set. | false | boolean | |
How the values of a range are rendered: FORMATTED_VALUE as calculated and formatted by the format of the cell, UNFORMATTED_VALUE as calculated but not formatted, FORMULA as the formula of the cell (not calculated). Only used when range is set. Enum values:
| FORMATTED_VALUE | 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 | |
The factory that creates the Google Sheets client of each endpoint. By default, BatchGoogleSheetsClientFactory. | GoogleSheetsClientFactory | ||
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 | |
The OAuth 2.0 access token, used with clientId and clientSecret. An access token typically expires after an hour, so set refreshToken for long-running use. | String | ||
The OAuth 2.0 client secret of the application, used together with clientId. | String | ||
The OAuth 2.0 refresh token, used with clientId and clientSecret to obtain a new access token whenever the current one expires. Needed when the application runs for a long time. | String | ||
The JSON key file of a service account, used when clientId and clientSecret are not set. It is loaded from the classpath by default; use a prefix such as file: for another location. | String |
Endpoint Options
The Google Sheets Stream endpoint is configured using URI syntax:
google-sheets-stream:spreadsheetId
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The application name that the client sends in the User-Agent header of each request, such as camel-google-sheets/1.0. | String | ||
The OAuth 2.0 client ID of the application. When clientId and clientSecret are both set, the client uses OAuth 2.0 with accessToken and refreshToken; otherwise it uses serviceAccountKey, or the Application Default Credentials when that is not set either. | String | ||
The email address of the user that the service account impersonates, with domain-wide delegation. Used only with serviceAccountKey. | String | ||
Whether the spreadsheet that a poll gets includes the grid data: the cells of each sheet with their values and formats. Only used when range is not set. | false | boolean | |
Whether the values of a range are grouped by rows (ROWS) or by columns (COLUMNS). Only used when range is set. Enum values:
| ROWS | String | |
The maximum number of rows (or columns, by majorDimension) kept of each range: the values after it are dropped from the ValueRange, or with splitResults, no message is sent for them. Only used when range is set. The default 0 means no limit. | int | ||
The ranges whose values each poll gets, in A1 notation and separated by commas, such as Sheet1!A1:C10. When not set, each poll gets the whole spreadsheet instead. | String | ||
The OAuth 2.0 scopes to request, separated by commas, such as https://www.googleapis.com/auth/spreadsheets.readonly (see SheetsScopes). They apply to serviceAccountKey and to the Application Default Credentials; with clientId and clientSecret the scopes are those granted with the refresh token, and this option is not used. No scopes are requested by default. | String | ||
If the polling consumer did not poll any files, you can enable this option to send an empty message (no body) instead. | false | boolean | |
Whether to send one message per row (or column, by majorDimension) of each range, with the List of its values as body, instead of one message per range with its ValueRange. Only used when range is set. | false | boolean | |
How the values of a range are rendered: FORMATTED_VALUE as calculated and formatted by the format of the cell, UNFORMATTED_VALUE as calculated but not formatted, FORMULA as the formula of the cell (not calculated). Only used when range is set. Enum values:
| FORMATTED_VALUE | String | |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. | ExceptionHandler | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern | ||
A pluggable org.apache.camel.PollingConsumerPollingStrategy allowing you to provide your custom implementation to control error handling usually occurred during the poll operation before an Exchange have been created and being routed in Camel. | PollingConsumerPollStrategy | ||
The number of subsequent error polls (failed due some error) that should happen before the backoffMultipler should kick-in. | int | ||
The number of subsequent idle polls that should happen before the backoffMultipler should kick-in. | int | ||
To let the scheduled polling consumer backoff if there has been a number of subsequent idles/errors in a row. The multiplier is then the number of polls that will be skipped before the next actual attempt is happening again. When this option is in use then backoffIdleThreshold and/or backoffErrorThreshold must also be configured. | int | ||
Milliseconds before the next poll. | 500 | long | |
If greedy is enabled, then the ScheduledPollConsumer will run immediately again, if the previous run polled 1 or more messages. | false | boolean | |
How long a poll may run before the health check of the consumer reports DOWN. The health check is otherwise based on the polls that are done, so it stays UP while a poll hangs, for example on a read without timeout from a connection that went silent. A poll includes routing the messages it polled, so set this above the longest poll you expect. The poll is not interrupted. The value is in millis (not affected by timeUnit). The default 0 turns this off. | 0 | long | |
Milliseconds before the first poll starts. | 1000 | long | |
Specifies a maximum limit of number of fires. So if you set it to 1, the scheduler will only fire once. If you set it to 5, it will only fire five times. A value of zero or negative means fire forever. | 0 | long | |
The consumer logs a start/complete log line when it polls. This option allows you to configure the logging level for that. Enum values:
| TRACE | LoggingLevel | |
Allows for configuring a custom/shared thread pool to use for the consumer. By default each consumer has its own single threaded thread pool. | ScheduledExecutorService | ||
To use a cron scheduler from either camel-spring or camel-quartz component. Use value spring or quartz for built in scheduler. | none | Object | |
To configure additional properties when using a custom scheduler or any of the Quartz, Spring based scheduler. This is a multi-value option with prefix: scheduler. | Map | ||
Whether the scheduler should be auto started. | true | boolean | |
Time unit for initialDelay and delay options. Enum values:
| MILLISECONDS | TimeUnit | |
Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details. | true | boolean | |
The OAuth 2.0 access token, used with clientId and clientSecret. An access token typically expires after an hour, so set refreshToken for long-running use. | String | ||
The OAuth 2.0 client secret of the application, used together with clientId. | String | ||
The OAuth 2.0 refresh token, used with clientId and clientSecret to obtain a new access token whenever the current one expires. Needed when the application runs for a long time. | String | ||
The JSON key file of a service account, used when clientId and clientSecret are not set. It is loaded from the classpath by default; use a prefix such as file: for another location. | String |
Message Headers
The Google Sheets Stream component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelGoogleSheetsSpreadsheetId (consumer) Constant: | The ID of the spreadsheet. | String | |
CamelGoogleSheetsSpreadsheetUrl (consumer) Constant: | The URL of the spreadsheet. Set only when the whole spreadsheet is polled (range not set). | String | |
CamelGoogleSheetsMajorDimension (consumer) Constant: | The major dimension of the values of the range: ROWS or COLUMNS. | String | |
CamelGoogleSheetsRange (consumer) Constant: | The range the values cover, in A1 notation. | String | |
CamelGoogleSheetsRangeIndex (consumer) Constant: | The position of the range among the ranges of the poll, starting at 1. | int | |
CamelGoogleSheetsValueIndex (consumer) Constant: | The position of the row (or column, by majorDimension) in its range, starting at 1. Set only with splitResults. | int |
Message Body
The message body at each side of the Google Sheets Stream 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 |
|---|---|---|
Consumer |
| One message per poll with the Spreadsheet, or, when range is set, one message per range with its ValueRange. splitResults with range gives one message per row (or column, by majorDimension) with the List of its values. The Spreadsheet and the ValueRange are GenericJson, which is also a Map of the JSON fields.
|
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.
Usage
The consumer polls a range of the spreadsheet. With splitResults, each row is a message, and the CamelGoogleSheetsValueIndex header is its position in the range:
- route:
from:
uri: "google-sheets-stream:{{google.spreadsheetId}}"
parameters:
range: "Orders!A2:C"
majorDimension: ROWS
splitResults: true
clientId: "{{google.clientId}}"
clientSecret: "{{google.clientSecret}}"
refreshToken: "{{google.refreshToken}}"
applicationName: camel-example
steps:
- log:
message: "Row ${header.CamelGoogleSheetsValueIndex}: ${body}" More information
For more information on the endpoints and options see API documentation at: https://developers.google.com/sheets/api/reference/rest/