列出訂閱項目 (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 範例瀏覽工具。