列出订阅 (v1)

列出项目和位置中的所有 Analytics Hub 订阅。

代码示例

Node.js

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 BigQuery Node.js API 参考文档。

如需向 BigQuery 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证。

'use strict';

const {AnalyticsHubServiceClient} = require('@google-cloud/bigquery-analyticshub');
const {status} = require('@grpc/grpc-js');

const client = new AnalyticsHubServiceClient();

/**
 * Lists subscriptions in a Google Cloud project and location.
 */
async function listSubscriptions() {
  try {
    const projectId = await client.getProjectId();
    const location = 'us';
    const parent = client.locationPath(projectId, location);

    const request = {
      parent,
    };

    const [subscriptions] = await client.listSubscriptions(request);
    console.log(`Retrieved ${subscriptions.length} subscription(s).`);
    for (const subscription of subscriptions) {
      console.log(`Subscription: ${subscription.name}`);
      console.log(`State: ${subscription.state}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument: ${err.message}`);
    } else {
      console.error(`API error: ${err.message}`);
    }
  }
}

module.exports = { listSubscriptions };

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器。