|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Dict, Sequence, Optional, Any |
| 16 | +from logging import Logger |
| 17 | +import argparse |
| 18 | +import pprint |
| 19 | + |
| 20 | +from pyspark.sql import SparkSession, DataFrameWriter |
| 21 | + |
| 22 | +from dataproc_templates import BaseTemplate |
| 23 | +from dataproc_templates.util.argument_parsing import add_spark_options |
| 24 | +from dataproc_templates.util.dataframe_writer_wrappers import persist_dataframe_to_cloud_storage |
| 25 | +import dataproc_templates.util.template_constants as constants |
| 26 | + |
| 27 | + |
| 28 | +__all__ = ['BigQueryToMemorystoreTemplate'] |
| 29 | + |
| 30 | + |
| 31 | +class BigQueryToMemorystoreTemplate(BaseTemplate): |
| 32 | + """ |
| 33 | + Dataproc template implementing exports from BigQuery to Memorystore |
| 34 | + """ |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def parse_args(args: Optional[Sequence[str]] = None) -> Dict[str, Any]: |
| 38 | + parser: argparse.ArgumentParser = argparse.ArgumentParser() |
| 39 | + |
| 40 | + parser.add_argument( |
| 41 | + f'--{constants.BQ_MEMORYSTORE_INPUT_TABLE}', |
| 42 | + dest=constants.BQ_MEMORYSTORE_INPUT_TABLE, |
| 43 | + required=True, |
| 44 | + help='BigQuery Input table name' |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + f'--{constants.BQ_MEMORYSTORE_OUTPUT_HOST}', |
| 48 | + dest=constants.BQ_MEMORYSTORE_OUTPUT_HOST, |
| 49 | + required=True, |
| 50 | + help='Redis Memorystore host', |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + f'--{constants.BQ_MEMORYSTORE_OUTPUT_PORT}', |
| 54 | + dest=constants.BQ_MEMORYSTORE_OUTPUT_PORT, |
| 55 | + required=False, |
| 56 | + default=6379, |
| 57 | + help='Redis Memorystore port. Defaults to 6379', |
| 58 | + ) |
| 59 | + #todo: add defaults |
| 60 | + parser.add_argument( |
| 61 | + f'--{constants.BQ_MEMORYSTORE_OUTPUT_TABLE}', |
| 62 | + dest=constants.BQ_MEMORYSTORE_OUTPUT_TABLE, |
| 63 | + required=True, |
| 64 | + help='Redis Memorystore target table name', |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + f'--{constants.BQ_MEMORYSTORE_OUTPUT_KEY_COLUMN}', |
| 68 | + dest=constants.BQ_MEMORYSTORE_OUTPUT_KEY_COLUMN, |
| 69 | + required=True, |
| 70 | + help='Redis Memorystore key column for target table', |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + f'--{constants.BQ_MEMORYSTORE_OUTPUT_MODEL}', |
| 74 | + dest=constants.BQ_MEMORYSTORE_OUTPUT_MODEL, |
| 75 | + required=False, |
| 76 | + default=constants.BQ_MEMORYSTORE_OUTPUT_MODEL_HASH, |
| 77 | + help=( |
| 78 | + 'Memorystore persistence model for Dataframe' |
| 79 | + '(one of: hash, binary) ' |
| 80 | + '(Defaults to hash)' |
| 81 | + ), |
| 82 | + choices=[ |
| 83 | + constants.BQ_MEMORYSTORE_OUTPUT_MODEL_HASH, |
| 84 | + constants.BQ_MEMORYSTORE_OUTPUT_MODEL_BINARY |
| 85 | + ] |
| 86 | + ) |
| 87 | + parser.add_argument( |
| 88 | + f'--{constants.BQ_MEMORYSTORE_OUTPUT_MODE}', |
| 89 | + dest=constants.BQ_MEMORYSTORE_OUTPUT_MODE, |
| 90 | + required=False, |
| 91 | + default=constants.OUTPUT_MODE_APPEND, |
| 92 | + help=( |
| 93 | + 'Output write mode ' |
| 94 | + '(one of: append,overwrite,ignore,errorifexists) ' |
| 95 | + '(Defaults to append)' |
| 96 | + ), |
| 97 | + choices=[ |
| 98 | + constants.OUTPUT_MODE_OVERWRITE, |
| 99 | + constants.OUTPUT_MODE_APPEND, |
| 100 | + constants.OUTPUT_MODE_IGNORE, |
| 101 | + constants.OUTPUT_MODE_ERRORIFEXISTS |
| 102 | + ] |
| 103 | + ) |
| 104 | + parser.add_argument( |
| 105 | + f'--{constants.BQ_MEMORYSTORE_OUTPUT_TTL}', |
| 106 | + dest=constants.BQ_MEMORYSTORE_OUTPUT_TTL, |
| 107 | + required=False, |
| 108 | + default=0, |
| 109 | + help=( |
| 110 | + 'Data time to live in seconds. Data doesn\'t expire if ttl is less than 1' |
| 111 | + '(Defaults to 0)' |
| 112 | + ) |
| 113 | + ) |
| 114 | + parser.add_argument( |
| 115 | + f'--{constants.BQ_MEMORYSTORE_OUTPUT_DBNUM}', |
| 116 | + dest=constants.BQ_MEMORYSTORE_OUTPUT_DBNUM, |
| 117 | + required=False, |
| 118 | + default=0, |
| 119 | + help=( |
| 120 | + 'Database / namespace for logical key separation' |
| 121 | + '(Defaults to 0)' |
| 122 | + ) |
| 123 | + ) |
| 124 | + |
| 125 | + known_args: argparse.Namespace |
| 126 | + known_args, _ = parser.parse_known_args(args) |
| 127 | + |
| 128 | + return vars(known_args) |
| 129 | + |
| 130 | + def run(self, spark: SparkSession, args: Dict[str, Any]) -> None: |
| 131 | + |
| 132 | + logger: Logger = self.get_logger(spark=spark) |
| 133 | + |
| 134 | + # Arguments |
| 135 | + input_table: str = args[constants.BQ_MEMORYSTORE_INPUT_TABLE] |
| 136 | + |
| 137 | + output_table: str = args[constants.BQ_MEMORYSTORE_OUTPUT_TABLE] |
| 138 | + host: str = args[constants.BQ_MEMORYSTORE_OUTPUT_HOST] |
| 139 | + port: str = args[constants.BQ_MEMORYSTORE_OUTPUT_PORT] |
| 140 | + key_column: str = args[constants.BQ_MEMORYSTORE_OUTPUT_KEY_COLUMN] |
| 141 | + model: str = args[constants.BQ_MEMORYSTORE_OUTPUT_MODEL] |
| 142 | + ttl: int = args[constants.BQ_MEMORYSTORE_OUTPUT_TTL] |
| 143 | + dbnum: int = args[constants.BQ_MEMORYSTORE_OUTPUT_DBNUM] |
| 144 | + output_mode: str = args[constants.BQ_MEMORYSTORE_OUTPUT_MODE] |
| 145 | + |
| 146 | + logger.info( |
| 147 | + "Starting Bigquery to Memorystore Spark job with parameters:\n" |
| 148 | + f"{pprint.pformat(args)}" |
| 149 | + ) |
| 150 | + |
| 151 | + # Read |
| 152 | + input_data = spark.read \ |
| 153 | + .format(constants.FORMAT_BIGQUERY) \ |
| 154 | + .option(constants.TABLE, input_table) \ |
| 155 | + .load() |
| 156 | + |
| 157 | + # Write |
| 158 | + input_data.write \ |
| 159 | + .format(constants.FORMAT_MEMORYSTORE) \ |
| 160 | + .option(constants.TABLE, output_table) \ |
| 161 | + .option(constants.MEMORYSTORE_KEY_COLUMN, key_column) \ |
| 162 | + .option(constants.MEMORYSTORE_MODEL, model) \ |
| 163 | + .option(constants.MEMORYSTORE_HOST, host) \ |
| 164 | + .option(constants.MEMORYSTORE_PORT, port) \ |
| 165 | + .option(constants.MEMORYSTORE_DBNUM, dbnum) \ |
| 166 | + .option(constants.MEMORYSTORE_TTL, ttl) \ |
| 167 | + .mode(output_mode) \ |
| 168 | + .save() |
0 commit comments