Démontrer la traduction de requêtes par lot
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Utilise le service de migration BigQuery pour traduire les requêtes stockées dans Cloud Storage.
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code sample demonstrates how to use the BigQuery Migration API in Go to perform a batch SQL translation task.\u003c/p\u003e\n"],["\u003cp\u003eThe application creates a migration workflow to translate SQL queries from a source dialect (Teradata) to a target dialect (BigQuery).\u003c/p\u003e\n"],["\u003cp\u003eThe code sets up a translation task that reads input SQL files from a Cloud Storage location and writes the translated output to another Cloud Storage location.\u003c/p\u003e\n"],["\u003cp\u003eThe workflow execution is monitored, and the application will report on the final status of the task and any errors encountered.\u003c/p\u003e\n"]]],[],null,["# Demonstrate batch query translation\n\nUses the BigQuery Migration service to translate queries stored in Cloud Storage\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Go API\nreference documentation](https://godoc.org/cloud.google.com/go/bigquery).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n\n // The bigquery_migration_quickstart application demonstrates basic usage of the\n // BigQuery migration API by executing a workflow that performs a batch SQL\n // translation task.\n package main\n\n import (\n \t\"context\"\n \t\"flag\"\n \t\"fmt\"\n \t\"log\"\n \t\"time\"\n\n \tmigration \"cloud.google.com/go/bigquery/migration/apiv2\"\n \t\"cloud.google.com/go/bigquery/migration/apiv2/migrationpb\"\n )\n\n func main() {\n \t// Define command line flags for controlling the behavior of this quickstart.\n \tprojectID := flag.String(\"project_id\", \"\", \"Cloud Project ID.\")\n \tlocation := flag.String(\"location\", \"us\", \"BigQuery Migration location used for interactions.\")\n \toutputPath := flag.String(\"output\", \"\", \"Cloud Storage path for translated resources.\")\n \t// Parse flags and do some minimal validation.\n \tflag.Parse()\n \tif *projectID == \"\" {\n \t\tlog.Fatal(\"empty --project_id specified, please provide a valid project ID\")\n \t}\n \tif *location == \"\" {\n \t\tlog.Fatal(\"empty --location specified, please provide a valid location\")\n \t}\n \tif *outputPath == \"\" {\n \t\tlog.Fatalf(\"empty --output specified, please provide a valid cloud storage path\")\n \t}\n\n \tctx := context.Background()\n \tmigClient, err := migration.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/migration/apiv2.html#cloud_google_com_go_bigquery_migration_apiv2_Client_NewClient(ctx)\n \tif err != nil {\n \t\tlog.Fatalf(\"migration.NewClient: %v\", err)\n \t}\n \tdefer migClient.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/migration/apiv2.html#cloud_google_com_go_bigquery_migration_apiv2_Client_Close()\n\n \tworkflow, err := executeTranslationWorkflow(ctx, migClient, *projectID, *location, *outputPath)\n \tif err != nil {\n \t\tlog.Fatalf(\"workflow execution failed: %v\\n\", err)\n \t}\n\n \treportWorkflowStatus(workflow)\n }\n\n // executeTranslationWorkflow constructs a migration workflow that performs batch SQL translation.\n func executeTranslationWorkflow(ctx context.Context, client *migration.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/migration/apiv2.html#cloud_google_com_go_bigquery_migration_apiv2_Client, projectID, location, outPath string) (*migrationpb.MigrationWorkflow, error) {\n\n \t// Construct the workflow creation request. In this workflow, we have only a single translation task present.\n \treq := &migrationpb.CreateMigrationWorkflowRequest{\n \t\tParent: fmt.Sprintf(\"projects/%s/locations/%s\", projectID, location),\n \t\tMigrationWorkflow: &migrationpb.MigrationWorkflow{\n \t\t\tDisplayName: \"example SQL conversion\",\n \t\t\tTasks: map[string]*migrationpb.MigrationTask{\n \t\t\t\t\"example_conversion\": {\n \t\t\t\t\tType: \"Translation_Teradata2BQ\",\n \t\t\t\t\tTaskDetails: &migrationpb.MigrationTask_TranslationConfigDetails{\n \t\t\t\t\t\tTranslationConfigDetails: &migrationpb.TranslationConfigDetails{\n \t\t\t\t\t\t\tSourceLocation: &migrationpb.TranslationConfigDetails_GcsSourcePath{\n \t\t\t\t\t\t\t\tGcsSourcePath: \"gs://cloud-samples-data/bigquery/migration/translation/input/\",\n \t\t\t\t\t\t\t},\n \t\t\t\t\t\t\tTargetLocation: &migrationpb.TranslationConfigDetails_GcsTargetPath{\n \t\t\t\t\t\t\t\tGcsTargetPath: outPath,\n \t\t\t\t\t\t\t},\n \t\t\t\t\t\t\tSourceDialect: &migrationpb.Dialect{\n \t\t\t\t\t\t\t\tDialectValue: &migrationpb.Dialect_TeradataDialect{\n \t\t\t\t\t\t\t\t\tTeradataDialect: &migrationpb.TeradataDialect{\n \t\t\t\t\t\t\t\t\t\tMode: migrationpb.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/migration/apiv2/migrationpb.html#cloud_google_com_go_bigquery_migration_apiv2_migrationpb_TeradataDialect_MODE_UNSPECIFIED_TeradataDialect_SQL_TeradataDialect_BTEQ,\n \t\t\t\t\t\t\t\t\t},\n \t\t\t\t\t\t\t\t},\n \t\t\t\t\t\t\t},\n \t\t\t\t\t\t\tTargetDialect: &migrationpb.Dialect{\n \t\t\t\t\t\t\t\tDialectValue: &migrationpb.Dialect_BigqueryDialect{},\n \t\t\t\t\t\t\t},\n \t\t\t\t\t\t},\n \t\t\t\t\t},\n \t\t\t\t},\n \t\t\t},\n \t\t},\n \t}\n\n \t// Create the workflow using the request.\n \tworkflow, err := client.CreateMigrationWorkflow(ctx, req)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"CreateMigrationWorkflow: %w\", err)\n \t}\n \tfmt.Printf(\"workflow created: %s\", workflow.GetName())\n\n \t// This is an asyncronous process, so we now poll the workflow\n \t// until completion or a suitable timeout has elapsed.\n \ttimeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)\n \tdefer cancel()\n \tfor {\n \t\tselect {\n \t\tcase \u003c-timeoutCtx.Done():\n \t\t\treturn nil, fmt.Errorf(\"task %s didn't complete due to context expiring\", workflow.GetName())\n \t\tdefault:\n \t\t\tpolledWorkflow, err := client.GetMigrationWorkflow(timeoutCtx, &migrationpb.GetMigrationWorkflowRequest{\n \t\t\t\tName: workflow.GetName(),\n \t\t\t})\n \t\t\tif err != nil {\n \t\t\t\treturn nil, fmt.Errorf(\"polling ended in error: %w\", err)\n \t\t\t}\n \t\t\tif polledWorkflow.GetState() == migrationpb.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/migration/apiv2/migrationpb.html#cloud_google_com_go_bigquery_migration_apiv2_migrationpb_MigrationWorkflow_STATE_UNSPECIFIED_MigrationWorkflow_DRAFT_MigrationWorkflow_RUNNING_MigrationWorkflow_PAUSED_MigrationWorkflow_COMPLETED {\n \t\t\t\t// polledWorkflow contains the most recent metadata about the workflow, so we return that.\n \t\t\t\treturn polledWorkflow, nil\n \t\t\t}\n \t\t\t// workflow still isn't complete, so sleep briefly before polling again.\n \t\t\ttime.Sleep(5 * time.Second)\n \t\t}\n \t}\n }\n\n // reportWorkflowStatus prints information about the workflow execution in a more human readable format.\n func reportWorkflowStatus(workflow *migrationpb.MigrationWorkflow) {\n \tfmt.Printf(\"Migration workflow %s ended in state %s.\\n\", workflow.GetName(), workflow.GetState().String())\n \tfor k, task := range workflow.GetTasks() {\n \t\tfmt.Printf(\" - Task %s had id %s\", k, task.GetId())\n \t\tif task.GetProcessingError() != nil {\n \t\t\tfmt.Printf(\" with processing error: %s\", task.GetProcessingError().GetReason())\n \t\t}\n \t\tfmt.Println()\n \t}\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquerymigration)."]]