Label hinzufügen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Einer Tabelle ein Label hinzufügen
Weitere Informationen
Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:
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 content provides code samples in Go, Java, Node.js, and Python demonstrating how to add a label to an existing BigQuery table.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample requires setting up Application Default Credentials for authentication, as well as following the language-specific quickstart guide for BigQuery client libraries.\u003c/p\u003e\n"],["\u003cp\u003eThe examples add the label "color: green" to the table metadata using the BigQuery API.\u003c/p\u003e\n"],["\u003cp\u003eThe content references documentation for adding and updating labels in BigQuery, and provides links to the API documentation for each programming language.\u003c/p\u003e\n"],["\u003cp\u003eIt recommends using the google cloud sample browser to view code samples for other Google Cloud Products.\u003c/p\u003e\n"]]],[],null,["Add a label to a table.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Adding labels to resources](/bigquery/docs/adding-labels)\n- [Updating labels](/bigquery/docs/updating-labels)\n\nCode sample \n\nGo\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\n \t\"cloud.google.com/go/bigquery\"\n )\n\n // addTableLabel demonstrates adding Label metadata to a BigQuery table.\n func addTableLabel(projectID, datasetID, tableID string) error {\n \t// projectID := \"my-project-id\"\n \t// datasetID := \"mydataset\"\n \t// tableID := \"mytable\"\n \tctx := context.Background()\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 \ttbl := client.Dataset(datasetID).Table(tableID)\n \tmeta, err := tbl.Metadata(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n\n \tupdate := bigquery.TableMetadataToUpdate{}\n \tupdate.SetLabel(\"color\", \"green\")\n \tif _, err := tbl.Update(ctx, update, meta.ETag); err != nil {\n \t\treturn err\n \t}\n \treturn nil\n }\n\nJava\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.BigQuery;\n import com.google.cloud.bigquery.BigQueryException;\n import com.google.cloud.bigquery.BigQueryOptions;\n import com.google.cloud.bigquery.Table;\n import com.google.cloud.bigquery.TableId;\n import java.util.HashMap;\n import java.util.Map;\n\n // Sample to adds a label to an existing table\n public class LabelTable {\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 labelTable(datasetName, tableName);\n }\n\n public static void labelTable(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 BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();\n\n // This example table starts with existing label { color: 'green' }\n Table table = bigquery.getTable(TableId.of(datasetName, tableName));\n // Add label to table\n Map\u003cString, String\u003e labels = new HashMap\u003c\u003e();\n labels.put(\"color\", \"green\");\n\n table.toBuilder().setLabels(labels).build().update();\n System.out.println(\"Label added successfully\");\n } catch (BigQueryException e) {\n System.out.println(\"Label was not added. \\n\" + e.toString());\n }\n }\n }\n\nNode.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('@google-cloud/bigquery');\n const bigquery = new BigQuery();\n\n async function labelTable() {\n // Adds a label to an existing table.\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 const dataset = bigquery.dataset(datasetId);\n const [table] = await dataset.table(tableId).get();\n\n // Retrieve current table metadata\n const [metadata] = await table.getMetadata();\n\n // Add label to table metadata\n metadata.labels = {color: 'green'};\n const [apiResponse] = await table.setMetadata(metadata);\n\n console.log(`${tableId} labels:`);\n console.log(apiResponse.labels);\n }\n\nPython\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 bigquery\n\n client = bigquery.Client()\n\n # TODO(dev): Change table_id to the full name of the table you want to create.\n table_id = \"your-project.your_dataset.your_table_name\"\n\n table = client.get_table(table_id) # API request\n\n labels = {\"color\": \"green\"}\n table.labels = labels\n\n table = client.update_table(table, [\"labels\"]) # API request\n\n print(f\"Added {table.labels} to {table_id}.\")\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]