List jobs
Stay organized with collections
Save and categorize content based on your preferences.
List all jobs in a project.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, Node.js, and Python to list jobs within a Google Cloud project using the BigQuery API.\u003c/p\u003e\n"],["\u003cp\u003eThe samples demonstrate how to authenticate to BigQuery and use client libraries to retrieve and iterate through a list of jobs, limiting it to the 10 most recent ones by default, but also providing options to list jobs in the last 6 months.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples showcase how to manage and list BigQuery jobs, including options to filter jobs by creation time, user, and state, along with a way to view the job state.\u003c/p\u003e\n"],["\u003cp\u003eEach code example includes setup instructions and links to the relevant BigQuery API reference documentation, as well as instructions to setup the Application Default Credentials.\u003c/p\u003e\n"]]],[],null,["# List jobs\n\nList all jobs in a project.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Manage jobs](/bigquery/docs/managing-jobs)\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 import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/bigquery\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // listJobs demonstrates iterating through the BigQuery jobs collection.\n func listJobs(w io.Writer, projectID string) error {\n \t// projectID := \"my-project-id\"\n \t// jobID := \"my-job-id\"\n \tctx := context.Background()\n\n \tclient, err := bigquery.NewClient(ctx, projectID)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"bigquery.NewClient: %w\", err)\n \t}\n \tdefer client.Close()\n\n \tit := client.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Client_Jobs(ctx)\n \t// List up to 10 jobs to demonstrate iteration.\n \tfor i := 0; i \u003c 10; i++ {\n \t\tj, err := it.Next()\n \t\tif err == iterator.Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn err\n \t\t}\n \t\tstate := \"Unknown\"\n \t\tswitch j.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Job_LastStatus().https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_State {\n \t\tcase bigquery.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_StateUnspecified_Pending_Running_Done:\n \t\t\tstate = \"Pending\"\n \t\tcase bigquery.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_StateUnspecified_Pending_Running_Done:\n \t\t\tstate = \"Running\"\n \t\tcase bigquery.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_StateUnspecified_Pending_Running_Done:\n \t\t\tstate = \"Done\"\n \t\t}\n \t\tfmt.Fprintf(w, \"Job %s in state %s\\n\", j.https://cloud.google.com/go/docs/reference/cloud.google.com/go/bigquery/latest/index.html#cloud_google_com_go_bigquery_Job_ID(), state)\n \t}\n \treturn nil\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java 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 Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\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 import com.google.api.gax.paging.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.paging.Page.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Job.html;\n\n // Sample to get list of jobs\n public class ListJobs {\n\n public static void main(String[] args) {\n listJobs();\n }\n\n public static void listJobs() {\n try {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html bigquery = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html.getDefaultInstance().getService();\n\n Page\u003cJob\u003e jobs = bigquery.https://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.pageSize(10));\n if (jobs == null) {\n System.out.println(\"Dataset does not contain any jobs.\");\n return;\n }\n jobs.getValues().forEach(job -\u003e System.out.printf(\"Success! Job ID: %s\", job.getJobId()));\n } catch (https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html e) {\n System.out.println(\"Jobs not listed in dataset due to error: \\n\" + e.toString());\n }\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js 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 Node.js API\nreference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).\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 // Import the Google Cloud client library\n const {BigQuery} = require('https://cloud.google.com/nodejs/docs/reference/bigquery/latest/overview.html');\n const bigquery = new https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html();\n\n async function listJobs() {\n // Lists all jobs in current GCP project.\n\n // List the 10 most recent jobs in reverse chronological order.\n // Omit the max_results parameter to list jobs from the past 6 months.\n const options = {maxResults: 10};\n const [jobs] = await bigquery.https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html(options);\n\n console.log('Jobs:');\n jobs.forEach(job =\u003e console.log(https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html.id));\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python 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 Python API\nreference documentation](/python/docs/reference/bigquery/latest).\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 from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n\n import datetime\n\n # Construct a BigQuery client object.\n client = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html()\n\n # List the 10 most recent jobs in reverse chronological order.\n # Omit the max_results parameter to list jobs from the past 6 months.\n print(\"Last 10 jobs:\")\n for job in client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_list_jobs(max_results=10): # API request(s)\n print(\"{}\".format(https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.html.job_id))\n\n # The following are examples of additional optional parameters:\n\n # Use min_creation_time and/or max_creation_time to specify a time window.\n print(\"Jobs from the last ten minutes:\")\n ten_mins_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=10)\n for job in client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_list_jobs(min_creation_time=ten_mins_ago):\n print(\"{}\".format(https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.html.job_id))\n\n # Use all_users to include jobs run by all users in the project.\n print(\"Last 10 jobs run by all users:\")\n for job in client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_list_jobs(max_results=10, all_users=True):\n print(\"{} run by user: {}\".format(https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.html.job_id, https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.html.user_email))\n\n # Use state_filter to filter by job state.\n print(\"Last 10 jobs done:\")\n for job in client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_list_jobs(max_results=10, state_filter=\"DONE\"):\n print(\"{}\".format(https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.html.job_id))\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=bigquery)."]]