Export a model Stay organized with collections Save and categorize content based on your preferences.
Export an existing model to an existing Cloud Storage bucket.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
C#
Before trying this sample, follow theC# setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryC# API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
usingGoogle.Cloud.BigQuery.V2;usingSystem;publicclassBigQueryExtractModel{publicvoidExtractModel(stringprojectId,stringdatasetId,stringmodelId,stringdestinationUri){BigQueryClientclient=BigQueryClient.Create(projectId);BigQueryJobjob=client.CreateModelExtractJob(projectId:projectId,datasetId:datasetId,modelId:modelId,destinationUri:destinationUri);job=job.PollUntilCompleted().ThrowOnAnyError();// Waits for the job to complete.System.IO.File.AppendAllText("log.txt",$"Exported model to {destinationUri}");Console.Write($"Exported model to {destinationUri}");}}Go
Before trying this sample, follow theGo setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryGo API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
import("context""fmt""cloud.google.com/go/bigquery")// exportModel demonstrates how to export an existing// BigQuery ML Model to Google Cloud Storage.funcexportModel(projectID,datasetID,modelID,gcsURIstring)error{// projectID := "my-project-id"// datasetID := "dataset-id"// modelID := "model-id"// gcsURI := "gs://mybucket/path/to/model"ctx:=context.Background()client,err:=bigquery.NewClient(ctx,projectID)iferr!=nil{returnfmt.Errorf("bigquery.NewClient: %w",err)}deferclient.Close()gcsRef:=bigquery.NewGCSReference(gcsURI)extractor:=client.DatasetInProject(projectID,datasetID).Model(modelID).ExtractorTo(gcsRef)// You can choose to run the job in a specific location for more complex data locality scenarios.// Ex: In this example, source dataset and GCS bucket are in the US.extractor.Location="US"job,err:=extractor.Run(ctx)iferr!=nil{returnerr}status,err:=job.Wait(ctx)iferr!=nil{returnerr}iferr:=status.Err();err!=nil{returnerr}returnnil}Java
Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
importcom.google.cloud.bigquery.BigQuery;importcom.google.cloud.bigquery.BigQueryException;importcom.google.cloud.bigquery.BigQueryOptions;importcom.google.cloud.bigquery.ExtractJobConfiguration;importcom.google.cloud.bigquery.Job;importcom.google.cloud.bigquery.JobInfo;importcom.google.cloud.bigquery.ModelId;// Sample to extract model to GCS bucketpublicclassExtractModel{publicstaticvoidmain(String[]args)throwsInterruptedException{// TODO(developer): Replace these variables before running the sample.StringprojectName="bigquery-public-data";StringdatasetName="samples";StringmodelName="model";StringbucketName="MY-BUCKET-NAME";StringdestinationUri="gs://"+bucketName+"/path/to/file";extractModel(projectName,datasetName,modelName,destinationUri);}publicstaticvoidextractModel(StringprojectName,StringdatasetName,StringmodelName,StringdestinationUri)throwsInterruptedException{try{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.BigQuerybigquery=BigQueryOptions.getDefaultInstance().getService();ModelIdmodelId=ModelId.of(projectName,datasetName,modelName);ExtractJobConfigurationextractConfig=ExtractJobConfiguration.newBuilder(modelId,destinationUri).build();Jobjob=bigquery.create(JobInfo.of(extractConfig));// Blocks until this job completes its execution, either failing or succeeding.JobcompletedJob=job.waitFor();if(completedJob==null){System.out.println("Job not executed since it no longer exists.");return;}elseif(completedJob.getStatus().getError()!=null){System.out.println("BigQuery was unable to extract due to an error: \n"+job.getStatus().getError());return;}System.out.println("Model extract successful");}catch(BigQueryExceptionex){System.out.println("Model extraction job was interrupted. \n"+ex.toString());}}}Ruby
Before trying this sample, follow theRuby setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryRuby API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
require"google/cloud/bigquery"### Exports a model to a Google Cloud Storage bucket.## @param dataset_id [String] The ID of the dataset that contains the model.# @param model_id [String] The ID of the model to export.# @param destination_uri [String] The Google Cloud Storage bucket to export the model to.defexport_modeldataset_id,model_id,destination_uribigquery=Google::Cloud::Bigquery.newdataset=bigquery.datasetdataset_idmodel=dataset.modelmodel_idputs"Extracting model#{model.model_id} to#{destination_uri}"job=model.extract_jobdestination_urijob.wait_until_done!ifjob.failed?puts"Error extracting model:#{job.error}"elseputs"Model extracted successfully"endendWhat's next
To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.