|
| 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 | +# [START bigquery_migration_create_workflow] |
| 16 | +def create_migration_workflow( |
| 17 | + gcs_input_path: str, gcs_output_path: str, project_id: str |
| 18 | +) -> None: |
| 19 | + """Creates a migration workflow of a Batch SQL Translation and prints the response.""" |
| 20 | + |
| 21 | + from google.cloud import bigquery_migration_v2 |
| 22 | + |
| 23 | + parent = f"projects/{project_id}/locations/us" |
| 24 | + |
| 25 | + # Construct a BigQuery Migration client object. |
| 26 | + client = bigquery_migration_v2.MigrationServiceClient() |
| 27 | + |
| 28 | + # Set the source dialect to Teradata SQL. |
| 29 | + source_dialect = bigquery_migration_v2.Dialect() |
| 30 | + source_dialect.teradata_dialect = bigquery_migration_v2.TeradataDialect( |
| 31 | + mode=bigquery_migration_v2.TeradataDialect.Mode.SQL |
| 32 | + ) |
| 33 | + |
| 34 | + # Set the target dialect to BigQuery dialect. |
| 35 | + target_dialect = bigquery_migration_v2.Dialect() |
| 36 | + target_dialect.bigquery_dialect = bigquery_migration_v2.BigQueryDialect() |
| 37 | + |
| 38 | + # Prepare the config proto. |
| 39 | + translation_config = bigquery_migration_v2.TranslationConfigDetails( |
| 40 | + gcs_source_path=gcs_input_path, |
| 41 | + gcs_target_path=gcs_output_path, |
| 42 | + source_dialect=source_dialect, |
| 43 | + target_dialect=target_dialect, |
| 44 | + ) |
| 45 | + |
| 46 | + # Prepare the task. |
| 47 | + migration_task = bigquery_migration_v2.MigrationTask( |
| 48 | + type_="Translation_Teradata2BQ", translation_config_details=translation_config |
| 49 | + ) |
| 50 | + |
| 51 | + # Prepare the workflow. |
| 52 | + workflow = bigquery_migration_v2.MigrationWorkflow( |
| 53 | + display_name="demo-workflow-python-example-Teradata2BQ" |
| 54 | + ) |
| 55 | + |
| 56 | + workflow.tasks["translation-task"] = migration_task # type: ignore |
| 57 | + |
| 58 | + # Prepare the API request to create a migration workflow. |
| 59 | + request = bigquery_migration_v2.CreateMigrationWorkflowRequest( |
| 60 | + parent=parent, |
| 61 | + migration_workflow=workflow, |
| 62 | + ) |
| 63 | + |
| 64 | + response = client.create_migration_workflow(request=request) |
| 65 | + |
| 66 | + print("Created workflow:") |
| 67 | + print(response.display_name) |
| 68 | + print("Current state:") |
| 69 | + print(response.State(response.state)) |
| 70 | + |
| 71 | + |
| 72 | +# [END bigquery_migration_create_workflow] |
0 commit comments