Run a query script.

## Code sample

### 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.api.gax.paging.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.paging.Page.html;
    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.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;

    // Sample to run query script.
    public class QueryScript {

      public static void main(String[] args) {
        String script =
            "-- Declare a variable to hold names as an array.\n"
                + "DECLARE top_names ARRAY<STRING>;\n"
                + "-- Build an array of the top 100 names from the year 2017.\n"
                + "SET top_names = (\n"
                + "  SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)\n"
                + "  FROM `bigquery-public-data`.usa_names.usa_1910_current\n"
                + "  WHERE year = 2017\n"
                + ");\n"
                + "-- Which names appear as words in Shakespeare's plays?\n"
                + "SELECT\n"
                + "  name AS shakespeare_name\n"
                + "FROM UNNEST(top_names) AS name\n"
                + "WHERE name IN (\n"
                + "  SELECT word\n"
                + "  FROM `bigquery-public-data`.samples.shakespeare\n"
                + ");";
        queryScript(script);
      }

      public static void queryScript(String script) {
        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();

          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(script).build();
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html createJob = 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(queryConfig));
          // Wait for the whole script to finish.
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobInfo.html jobInfo = createJob.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html#com_google_cloud_bigquery_Job_waitFor_com_google_cloud_bigquery_BigQueryRetryConfig_com_google_cloud_RetryOption____();
          String parentJobId = jobInfo.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.JobInfo.html#com_google_cloud_bigquery_JobInfo_getJobId__().getJob();

          // Fetch jobs created by the SQL script.
          Page<Job> childJobs = bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_listJobs_com_google_cloud_bigquery_BigQuery_JobListOption____(BigQuery.JobListOption.parentJobId(parentJobId));
          childJobs
              .iterateAll()
              .forEach(job -> System.out.printf("Child Job Id: ", job.getJobId().getJob()));

          System.out.println("Query script performed successfully.");
        } catch (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html | InterruptedException e) {
          System.out.println("Query not performed \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()

    # Run a SQL script.
    sql_script = """
    -- Declare a variable to hold names as an array.
    DECLARE top_names ARRAY<STRING>;

    -- Build an array of the top 100 names from the year 2017.
    SET top_names = (
    SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)
    FROM `bigquery-public-data.usa_names.usa_1910_2013`
    WHERE year = 2000
    );

    -- Which names appear as words in Shakespeare's plays?
    SELECT
    name AS shakespeare_name
    FROM UNNEST(top_names) AS name
    WHERE name IN (
    SELECT word
    FROM `bigquery-public-data.samples.shakespeare`
    );
    """
    parent_job = client.https://docs.cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html(sql_script)

    # Wait for the whole script to finish.
    rows_iterable = parent_job.result()
    print("Script created {} child jobs.".format(parent_job.num_child_jobs))

    # Fetch result rows for the final sub-job in the script.
    rows = list(rows_iterable)
    print(
        "{} of the top 100 names from year 2000 also appear in Shakespeare's works.".format(
            len(rows)
        )
    )

    # Fetch jobs created by the SQL script.
    child_jobs_iterable = client.https://docs.cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_list_jobs(parent_job=parent_job)
    for child_job in child_jobs_iterable:
        child_rows = list(child_job.result())
        print(
            "Child job with ID {} produced {} row(s).".format(
                child_job.job_id, len(child_rows)
            )
        )

### Rust

    use google_cloud_bigquery::client::BigQuery;

    pub async fn sample(project_id: &str) -> anyhow::Result<()> {
        let client = BigQuery::builder().build().await?;

        let mut rows = client
            .query(
                r#"
    DECLARE target_state STRING DEFAULT 'TX';
    SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013`
    WHERE state = target_state
    LIMIT 10;
    "#,
            )
            .with_project_id(project_id)
            .set_location("US")
            .until_done()
            .await?
            .read();

        while let Some(row) = rows.next().await.transpose()? {
            let name: String = row.get("name")?;
            println!("Name: {name}");
        }
        Ok(())
    }

## 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).