The Pub/Sub to Redis template is a streaming pipeline that reads messages from a Pub/Sub subscription and writes the message payload to Redis. The most common use case of this template is to export logs to Redis Enterprise for advanced search-based log analysis in real time.
- Before writing to Redis, you can apply a JavaScript user-defined function to the message payload.
- Any messages that experience processing failures are forwarded to a Pub/Sub unprocessed topic for further troubleshooting and reprocessing.
- For added security, enable an SSL connection when setting up your database endpoint connection. This template doesn't support mutual TLS.
Pipeline requirements
- The source Pub/Sub subscription must exist prior to running the pipeline.
- The Pub/Sub unprocessed topic must exist prior to running the pipeline.
- The Redis database endpoint must be accessible from the Dataflow workers' subnetwork.
Template parameters
Required parameters
- inputSubscription: The Pub/Sub subscription to read the input from. For example,
projects/<PROJECT_ID>/subscriptions/<SUBSCRIPTION_ID>
. - redisHost: The Redis database host. For example,
your.cloud.db.redislabs.com
. Defaults to: 127.0.0.1. - redisPort: The Redis database port. For example,
12345
. Defaults to: 6379. - redisPassword: The Redis database password. Defaults to
empty
.
Optional parameters
- sslEnabled: The Redis database SSL parameter. Defaults to: false.
- redisSinkType: The Redis sink. Supported values are
STRING_SINK, HASH_SINK, STREAMS_SINK, and LOGGING_SINK
. For example,STRING_SINK
. Defaults to: STRING_SINK. - connectionTimeout: The Redis connection timeout in milliseconds. For example,
2000
. Defaults to: 2000. - ttl: The key expiration time in seconds. The
ttl
default forHASH_SINK
is -1, which means it never expires. - javascriptTextTransformGcsPath: The Cloud Storage URI of the .js file that defines the JavaScript user-defined function (UDF) to use. For example,
gs://my-bucket/my-udfs/my_file.js
. - javascriptTextTransformFunctionName: The name of the JavaScript user-defined function (UDF) to use. For example, if your JavaScript function code is
myTransform(inJson) { /*...do stuff...*/ }
, then the function name ismyTransform
. For sample JavaScript UDFs, see UDF Examples (https://github.com/GoogleCloudPlatform/DataflowTemplates#udf-examples). - javascriptTextTransformReloadIntervalMinutes: Specifies how frequently to reload the UDF, in minutes. If the value is greater than 0, Dataflow periodically checks the UDF file in Cloud Storage, and reloads the UDF if the file is modified. This parameter allows you to update the UDF while the pipeline is running, without needing to restart the job. If the value is
0
, UDF reloading is disabled. The default value is0
.
User-defined function
Optionally, you can extend this template by writing a user-defined function (UDF). The template calls the UDF for each input element. Element payloads are serialized as JSON strings. For more information, see Create user-defined functions for Dataflow templates.
Function specification
The UDF has the following specification:
- Input: JSON string
- Output: a string or a stringified JSON object
Run the template
Console
- Go to the Dataflow Create job from template page. Go to Create job from template
- In the Job name field, enter a unique job name.
- Optional: For Regional endpoint, select a value from the drop-down menu. The default
region is
us-central1
.For a list of regions where you can run a Dataflow job, see Dataflow locations.
- From the Dataflow template drop-down menu, select the Pub/Sub to Redis template.
- In the provided parameter fields, enter your parameter values.
- Click Run job.
gcloud
In your shell or terminal, run the template:
gcloud dataflow flex-template run JOB_NAME \ --template-file-gcs-location=gs://dataflow-templates-REGION_NAME/VERSION/flex/Cloud_PubSub_to_Redis \ --project=PROJECT_ID \ --region=REGION_NAME \ --parameters \ inputSubscription=INPUT_SUBSCRIPTION,\ redisHost=REDIS_HOST,\ redisPort=REDIS_PORT,\ redisPassword=REDIS_PASSWORD,\
Replace the following:
JOB_NAME
: a unique job name of your choiceVERSION
: the version of the template that you want to useYou can use the following values:
latest
to use the latest version of the template, which is available in the non-dated parent folder in the bucket— gs://dataflow-templates-REGION_NAME/latest/- the version name, like
2023-09-12-00_RC00
, to use a specific version of the template, which can be found nested in the respective dated parent folder in the bucket— gs://dataflow-templates-REGION_NAME/
REGION_NAME
: the region where you want to deploy your Dataflow job—for example,us-central1
INPUT_SUBSCRIPTION
: the Pub/Sub input subscriptionREDIS_HOST
: the Redis DB HostREDIS_PORT
: the Redis DB PortREDIS_PASSWORD
: the Redis DB Password
API
To run the template using the REST API, send an HTTP POST request. For more information on the
API and its authorization scopes, see
projects.templates.launch
.
POST https://dataflow.googleapis.com/v1b3/projects/PROJECT_ID/locations/LOCATION/flexTemplates:launch { "launchParameter": { "jobName": "JOB_NAME", "parameters": { "inputSubscription": "INPUT_SUBSCRIPTION", "redisHost": "REDIS_HOST", "redisPort": "REDIS_PORT", "redisPassword": "REDIS_PASSWORD", }, "containerSpecGcsPath": "gs://dataflow-templates-LOCATION/VERSION/flex/Cloud_PubSub_to_Redis", "environment": { "maxWorkers": "10" } } }
Replace the following:
PROJECT_ID
: the Google Cloud project ID where you want to run the Dataflow jobJOB_NAME
: a unique job name of your choiceVERSION
: the version of the template that you want to useYou can use the following values:
latest
to use the latest version of the template, which is available in the non-dated parent folder in the bucket— gs://dataflow-templates-REGION_NAME/latest/- the version name, like
2023-09-12-00_RC00
, to use a specific version of the template, which can be found nested in the respective dated parent folder in the bucket— gs://dataflow-templates-REGION_NAME/
LOCATION
: the region where you want to deploy your Dataflow job—for example,us-central1
INPUT_SUBSCRIPTION
: the Pub/Sub input subscriptionREDIS_HOST
: the Redis DB HostREDIS_PORT
: the Redis DB PortREDIS_PASSWORD
: the Redis DB Password
What's next
- Learn about Dataflow templates.
- See the list of Google-provided templates.