写入目标表
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对出生率公共数据集运行查询,并将结果写入目标表。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。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\u003eA new dataset named "natality_regression" is created within Google BigQuery.\u003c/p\u003e\n"],["\u003cp\u003eA query is executed on the \u003ccode\u003ebigquery-public-data.samples.natality\u003c/code\u003e public dataset, selecting specific fields like weight, age, and gestation.\u003c/p\u003e\n"],["\u003cp\u003eThe results of the query are stored in a new table called "regression_input" within the "natality_regression" dataset.\u003c/p\u003e\n"],["\u003cp\u003eThe destination table, which was created in the query results, is moved to the user's default project.\u003c/p\u003e\n"],["\u003cp\u003eThe query targets a selection of data where all included fields are not null, ensuring data integrity.\u003c/p\u003e\n"]]],[],null,["# Write to destination table\n\nRun a query on the natality public dataset and write the results to a destination table.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Use Dataproc, BigQuery, and Apache Spark ML for Machine Learning](/dataproc/docs/tutorials/bigquery-sparkml)\n\nCode sample\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 \"\"\"Create a Google BigQuery linear regression input table.\n\n In the code below, the following actions are taken:\n * A new dataset is created \"natality_regression.\"\n * A query is run against the public dataset,\n bigquery-public-data.samples.natality, selecting only the data of\n interest to the regression, the output of which is stored in a new\n \"regression_input\" table.\n * The output table is moved over the wire to the user's default project via\n the built-in BigQuery Connector for Spark that bridges BigQuery and\n Cloud Dataproc.\n \"\"\"\n\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n\n # Create a new Google BigQuery client using Google Cloud Platform project\n # defaults.\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 # Prepare a reference to a new dataset for storing the query results.\n dataset_id = \"natality_regression\"\n dataset_id_full = f\"{client.project}.{dataset_id}\"\n\n dataset = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dataset.Dataset.html(dataset_id_full)\n\n # Create the new BigQuery dataset.\n dataset = client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_create_dataset(dataset)\n\n # Configure the query job.\n job_config = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.QueryJobConfig.html()\n\n # Set the destination table to where you want to store query results.\n # As of google-cloud-bigquery 1.11.0, a fully qualified table ID can be\n # used in place of a TableReference.\n job_config.destination = f\"{dataset_id_full}.regression_input\"\n\n # Set up a query in Standard SQL, which is the default for the BigQuery\n # Python client library.\n # The query selects the fields of interest.\n query = \"\"\"\n SELECT\n weight_pounds, mother_age, father_age, gestation_weeks,\n weight_gain_pounds, apgar_5min\n FROM\n `bigquery-public-data.samples.natality`\n WHERE\n weight_pounds IS NOT NULL\n AND mother_age IS NOT NULL\n AND father_age IS NOT NULL\n AND gestation_weeks IS NOT NULL\n AND weight_gain_pounds IS NOT NULL\n AND apgar_5min IS NOT NULL\n \"\"\"\n\n # Run the query.\n client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_query_and_wait(query, job_config=job_config) # Waits for the query to finish\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)."]]