列出工作
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
列出專案中的所有工作。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","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)."]]