Pub/Sub to Redis template

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

Parameter Description
inputSubscription The Pub/Sub subscription to read the input from, in the format of 'projects/my-project-id/subscriptions/my-subscription-id'. For example: projects/my-project-id/subscriptions/my-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.
sslEnabled Optional: The Redis database SSL parameter. Defaults to false.
redisSinkType Optional: The Redis sink. Supported values are STRING_SINK, HASH_SINK, STREAMS_SINK, and LOGGING_SINK. Defaults to STRING_SINK.
connectionTimeout Optional: The Redis connection timeout in milliseconds. For example: 2000. Defaults to 2000.
ttl Optional: The key expiration time in seconds. The ttl default for HASH_SINK is -1, which means it never expires.
javascriptTextTransformGcsPath Optional: The Cloud Storage path pattern for the JavaScript code that contains your user-defined functions. For example: gs://your-bucket/your-function.js.
javascriptTextTransformFunctionName Optional: The name of the function to call from your JavaScript file. Use only letters, digits, and underscores. For example: transform or transform_udf1.
javascriptTextTransformReloadIntervalMinutes Optional: 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 is 0.

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

  1. Go to the Dataflow Create job from template page.
  2. Go to Create job from template
  3. In the Job name field, enter a unique job name.
  4. 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.

  5. From the Dataflow template drop-down menu, select the Pub/Sub to Redis template.
  6. In the provided parameter fields, enter your parameter values.
  7. 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 choice
  • VERSION: the version of the template that you want to use

    You can use the following values:

  • REGION_NAME: the region where you want to deploy your Dataflow job—for example, us-central1
  • INPUT_SUBSCRIPTION: the Pub/Sub input subscription
  • REDIS_HOST: the Redis DB Host
  • REDIS_PORT: the Redis DB Port
  • REDIS_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 job
  • JOB_NAME: a unique job name of your choice
  • VERSION: the version of the template that you want to use

    You can use the following values:

  • LOCATION: the region where you want to deploy your Dataflow job—for example, us-central1
  • INPUT_SUBSCRIPTION: the Pub/Sub input subscription
  • REDIS_HOST: the Redis DB Host
  • REDIS_PORT: the Redis DB Port
  • REDIS_PASSWORD: the Redis DB Password

What's next