Run a BigQuery job (query, load, extract, or copy) in a specified location with additional configuration.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Running jobs programmatically](https://docs.cloud.google.com/bigquery/docs/running-jobs)

## Code sample

### C#


Before trying this sample, follow the C# setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery C# API
reference documentation](https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).


    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.html;
    using System;
    using System.Collections.Generic;

    public class BigQueryCreateJob
    {
        public BigQueryJob CreateJob(string projectId = "your-project-id")
        {
            string query = @"
                SELECT country_name from `bigquery-public-data.utility_us.country_code_iso";

            // Initialize client that will be used to send requests.
            https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryClient.html client = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryClient.html#Google_Cloud_BigQuery_V2_BigQueryClient_Create_System_String_Google_Apis_Auth_OAuth2_GoogleCredential_(projectId);

            https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.QueryOptions.html queryOptions = new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.QueryOptions.html
            {
                JobLocation = "us",
                JobIdPrefix = "code_sample_",
                Labels = new Dictionary<string, string>
                {
                    ["example-label"] = "example-value"
                },
                MaximumBytesBilled = 1000000
            };

            https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryJob.html queryJob = client.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryClient.html#Google_Cloud_BigQuery_V2_BigQueryClient_CreateQueryJob_System_String_System_Collections_Generic_IEnumerable_Google_Cloud_BigQuery_V2_BigQueryParameter__Google_Cloud_BigQuery_V2_QueryOptions_(
                sql: query,
                parameters: null,
                options: queryOptions);

            Console.WriteLine($"Started job: {queryJob.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.BigQueryJob.html#Google_Cloud_BigQuery_V2_BigQueryJob_Reference.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.BigQuery.V2/latest/Google.Cloud.BigQuery.V2.JobCreationOptions.html#Google_Cloud_BigQuery_V2_JobCreationOptions_JobId}");
            return queryJob;
        }
    }

### Go


Before trying this sample, follow the Go setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Go API
reference documentation](https://godoc.org/cloud.google.com/go/bigquery).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import (
    	"context"
    	"fmt"

    	"cloud.google.com/go/bigquery"

    	"github.com/google/uuid"
    )

    // createJob demonstrates running an arbitrary SQL statement as a query job.
    func createJob(projectID, sql string) error {
    	// sql := "SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`:"
    	ctx := context.Background()
    	client, err := bigquery.NewClient(ctx, projectID)
    	if err != nil {
    		return fmt.Errorf("bigquery.NewClient: %w", err)
    	}
    	defer client.Close()

    	// Demonstrate adding a label to the job.
    	q := client.Query(sql)
    	q.Labels = map[string]string{"example-label": "example-value"}

    	// The library will create job IDs for you automatically, but this can be overridden by
    	// setting the Job ID explicitly.  Job IDs are unique within a project and cannot be
    	// reused.
    	q.JobID = fmt.Sprintf("my_job_prefix_%s", uuid.New().String())

    	// Start job execution.
    	job, err := q.Run(ctx)
    	if err != nil {
    		return err
    	}
    	status, err := job.Wait(ctx)
    	if err != nil {
    		return err
    	}
    	if err := status.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_JobStatus_Err(); err != nil {
    		return err
    	}
    	return nil
    }

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobId.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobInfo.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html;
    import com.google.common.collect.ImmutableMap;
    import java.util.UUID;

    // Sample to create a job
    public class CreateJob {

      public static void main(String[] args) {
        // TODO(developer): Replace these variables before running the sample.
        String query = "SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`";
        createJob(query);
      }

      public static void createJob(String query) {
        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.
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html bigquery = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html.getDefaultInstance().getService();

          // Specify a job configuration to set optional job resource properties.
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html queryConfig =
              https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.QueryJobConfiguration.html.newBuilder(query)
                  .setLabels(ImmutableMap.of("example-label", "example-value"))
                  .build();

          // The location and job name are optional,
          // if both are not specified then client will auto-create.
          String jobName = "jobId_" + UUID.randomUUID().toString();
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobId.html jobId = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobId.html.newBuilder().setLocation("us").setJob(jobName).build();

          // Create a job with job ID
          bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_create_com_google_cloud_bigquery_DatasetInfo_com_google_cloud_bigquery_BigQuery_DatasetOption____(JobInfo.of(jobId, queryConfig));

          // Get a job that was just created
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html job = bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_getJob_com_google_cloud_bigquery_JobId_com_google_cloud_bigquery_BigQuery_JobOption____(jobId);
          if (job.getJobId().getJob().equals(jobId.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobId.html#com_google_cloud_bigquery_JobId_getJob__())) {
            System.out.print("Job created successfully." + job.getJobId().getJob());
          } else {
            System.out.print("Job was not created");
          }
        } catch (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html e) {
          System.out.print("Job was not created. \n" + e.toString());
        }
      }
    }

### Python


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/bigquery/latest

    # Construct a BigQuery client object.
    client = https://docs.cloud.google.com/python/docs/reference/bigquery/latest.https://docs.cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html()

    query_job = client.https://docs.cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_create_job(
        # Specify a job configuration, providing a query
        # and/or optional job resource properties, as needed.
        # The job instance can be a LoadJob, CopyJob, ExtractJob, QueryJob
        # Here, we demonstrate a "query" job.
        # References:
        #     https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.client.Client.html#google.cloud.bigquery.client.Client.create_job
        #     https://cloud.google.com/bigquery/docs/reference/rest/v2/Job
        #
        # Example use cases for .create_job() include:
        #    * to retry failed jobs
        #    * to generate jobs with an experimental API property that hasn't
        #      been added to one of the manually written job configuration
        #      classes yet
        #
        # NOTE: unless it is necessary to create a job in this way, the
        # preferred approach is to use one of the dedicated API calls:
        #   client.query()
        #   client.extract_table()
        #   client.copy_table()
        #   client.load_table_file(), client.load_table_from_dataframe(), etc
        job_config={
            "query": {
                "query": """
                         SELECT country_name
                         FROM `bigquery-public-data.utility_us.country_code_iso`
                         LIMIT 5
                         """,
            },
            "labels": {"example-label": "example-value"},
            "maximum_bytes_billed": 10000000,
        }
    )  # Make an API request.

    print(f"Started job: {https://docs.cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dbapi.Cursor.html#google_cloud_bigquery_dbapi_Cursor_query_job.job_id}")

### Rust

    use google_cloud_bigquery_v2::client::JobService;
    use google_cloud_bigquery_v2::model::{Job, JobConfiguration, JobConfigurationQuery, JobReference};

    pub async fn sample(project_id: &str) -> anyhow::Result<String> {
        let job_service = JobService::builder().build().await?;
        let job = Job::new()
            .set_job_reference(
                JobReference::new()
                    .set_project_id(project_id)
                    .set_job_id(format!("my_job_prefix_{}", rand::random::<u64>()))
                    .set_location("US"),
            )
            .set_configuration(
                JobConfiguration::new()
                    .set_labels([("example-label", "example-value")])
                    .set_query(
                        JobConfigurationQuery::new()
                            .set_query("SELECT 1")
                            .set_use_legacy_sql(false)
                            .set_maximum_bytes_billed(10000000_i64),
                    ),
            );

        let job = job_service
            .insert_job()
            .set_project_id(project_id)
            .set_job(job)
            .into_job_poller()
            .until_done()
            .await?;
        let job_id = job.job_reference.unwrap().job_id;
        println!("Job completed successfully: {}", job_id);

        Ok(job_id)
    }

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigquery).