Escribir en tabla de destino
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Ejecuta una consulta en el conjunto de datos públicos de natalidad y escribe los resultados en una tabla de destino
Explora más
Para obtener documentación en la que se incluye esta muestra de código, consulta lo siguiente:
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]