Scrittura nella tabella di destinazione
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Esegui una query sul set di dati pubblico sulla natalità e scrivi i risultati in una tabella di destinazione.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","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)."]]