获取表标签
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
检索给定表 ID 的表标签。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 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\u003eThis content provides code samples in Go, Java, Node.js, and Python to retrieve the labels associated with a specific table in BigQuery.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples demonstrate how to fetch table metadata and extract label information for a given dataset and table ID.\u003c/p\u003e\n"],["\u003cp\u003eEach language example includes instructions on setting up authentication and accessing relevant BigQuery API documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe content highlights how to handle cases where a table has no labels defined and display messages accordingly.\u003c/p\u003e\n"]]],[],null,["# Get table labels\n\nRetrieve the labels of a table for a given table ID.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Viewing labels](/bigquery/docs/viewing-labels)\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\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/bigquery\"\n )\n\n // tableLabels demonstrates fetching metadata from a table and printing the Label metadata to an io.Writer.\n func tableLabels(w io.Writer, 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 \tmeta, err := client.Dataset(datasetID).Table(tableID).Metadata(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n \tfmt.Fprintf(w, \"Table %s labels:\\n\", datasetID)\n \tif len(meta.Labels) == 0 {\n \t\tfmt.Fprintln(w, \"Table has no labels defined.\")\n \t\treturn nil\n \t}\n \tfor k, v := range meta.Labels {\n \t\tfmt.Fprintf(w, \"\\t%s:%s\\n\", k, v)\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 get table labels\n public class GetTableLabels {\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 getTableLabels(datasetName, tableName);\n }\n\n public static void getTableLabels(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 // This example table starts with existing label { color: 'green' }\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 table\n .getLabels()\n .forEach((key, value) -\u003e System.out.println(\"Retrieved labels successfully\"));\n } catch (https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html e) {\n System.out.println(\"Label was not deleted. \\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 getTableLabels() {\n // Gets labels on a dataset.\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 current dataset metadata.\n const table = bigquery.dataset(datasetId).table(tableId);\n const [metadata] = await table.getMetadata();\n const labels = metadata.labels;\n\n console.log(`${tableId} Labels:`);\n for (const [key, value] of Object.entries(labels)) {\n console.log(`${key}: ${value}`);\n }\n }\n getTableLabels();\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\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(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.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_get_table(table_id) # API Request\n\n # View table labels\n print(f\"Table ID: {table_id}.\")\n if table.labels:\n for label, value in table.labels.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.table.Row.html#google_cloud_bigquery_table_Row_items():\n print(f\"\\t{label}: {https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.query.ConnectionProperty.html#google_cloud_bigquery_query_ConnectionProperty_value}\")\n else:\n print(\"\\tTable has no labels defined.\")\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)."]]