Tabelle vorhanden
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Eine Funktion, mit der geprüft wird, ob eine Tabelle vorhanden ist.
Codebeispiel
Nächste Schritte
Wenn Sie nach Codebeispielen für andere Google Cloud -Produkte suchen und filtern möchten, können Sie den Google Cloud -Beispielbrowser verwenden.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis webpage provides code samples in Go, Java, Node.js, and Python for checking if a table exists in BigQuery.\u003c/p\u003e\n"],["\u003cp\u003eEach code example demonstrates how to use the BigQuery client library in its respective language to verify the existence of a specified table within a dataset.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples require setting up authentication with Application Default Credentials and following the BigQuery quickstart guide for the corresponding language.\u003c/p\u003e\n"],["\u003cp\u003eThe provided examples utilize BigQuery's API methods to retrieve table metadata or check for the table's presence, handling potential "not found" errors.\u003c/p\u003e\n"],["\u003cp\u003eThe page suggests additional resources, such as the BigQuery API reference documentation for each language and the Google Cloud sample browser, for finding other relevant code examples.\u003c/p\u003e\n"]]],[],null,["# Table exists\n\nA function to check whether a table exists.\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\"errors\"\n \t\"fmt\"\n \t\"net/http\"\n\n \t\"cloud.google.com/go/bigquery\"\n \t\"google.golang.org/api/googleapi\"\n )\n\n // tableExists checks whether a table exists in a given dataset.\n func tableExists(projectID, datasetID, tableID string) error {\n \t// projectID := \"my-project-id\"\n \t// datasetID := \"mydatasetid\"\n \t// tableID := \"mytableid\"\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 \ttableRef := client.Dataset(datasetID).Table(tableID)\n \tif _, err = tableRef.Metadata(ctx); err != nil {\n \t\tif e, ok := err.(*googleapi.Error); ok {\n \t\t\tif e.Code == http.StatusNotFound {\n \t\t\t\treturn errors.New(\"dataset or table not found\")\n \t\t\t}\n \t\t}\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.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-biglake/latest/com.google.cloud.bigquery.biglake.v1.Table.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.TableId.html;\n\n // Sample to check table exist\n public class TableExists {\n\n public static void main(String[] args) {\n // TODO(developer): Replace these variables before running the sample.\n String datasetName = \"MY_DATASET_NAME\";\n String tableName = \"MY_TABLE_NAME\";\n tableExists(datasetName, tableName);\n }\n\n public static void tableExists(String datasetName, String tableName) {\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 https://cloud.google.com/java/docs/reference/google-cloud-biglake/latest/com.google.cloud.bigquery.biglake.v1.Table.html table = bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_getTable_com_google_cloud_bigquery_TableId_com_google_cloud_bigquery_BigQuery_TableOption____(TableId.of(datasetName, tableName));\n if (table != null\n && table\n .exists()) { // table will be null if it is not found and setThrowNotFound is not set\n // to `true`\n System.out.println(\"Table already exist\");\n } else {\n System.out.println(\"Table not found\");\n }\n } catch (https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html e) {\n System.out.println(\"Table not found. \\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 tableExists() {\n // Checks whether table named \"my_table\" in \"my_dataset\" exists.\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample\n */\n // const datasetId = \"my_dataset\";\n // const tableId = \"my_table\";\n\n // Retrieve table reference\n const dataset = bigquery.dataset(datasetId);\n\n try {\n await dataset.table(tableId).get();\n console.log(`Table ${tableId} exists.`);\n } catch (e) {\n console.log(e.message);\n }\n }\n tableExists();\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 from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n from google.cloud.exceptions import NotFound\n\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 # TODO(developer): Set table_id to the ID of the table to determine existence.\n # table_id = \"your-project.your_dataset.your_table\"\n\n try:\n client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_get_table(table_id) # Make an API request.\n print(\"Table {} already exists.\".format(table_id))\n except NotFound:\n print(\"Table {} is not found.\".format(table_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)."]]