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

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 for HASH_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 is myTransform. 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 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