取得商店資訊 (v1)

從資料交換作業取得現有產品資訊。

程式碼範例

Java

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Java 設定操作說明進行操作。詳情請參閱 BigQuery Java API 參考文件。

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

import com.google.cloud.bigquery.analyticshub.v1.AnalyticsHubServiceClient;
import com.google.cloud.bigquery.analyticshub.v1.Listing;
import com.google.cloud.bigquery.analyticshub.v1.ListingName;
import java.io.IOException;

public class GetListing {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project-id";
    String locationId = "example-location-id";
    String dataExchangeId = "example-data-exchange-id";
    String listingId = "example-listing-id";
    getListing(projectId, locationId, dataExchangeId, listingId);
  }

  public static void getListing(
      String projectId, String locationId, String dataExchangeId, String listingId)
      throws IOException {
    // Initialize the client with Application Default Credentials.
    try (AnalyticsHubServiceClient client = AnalyticsHubServiceClient.create()) {
      String name = ListingName.of(projectId, locationId, dataExchangeId, listingId).toString();

      Listing listing = client.getListing(name);

      System.out.println("Listing retrieved successfully: " + listing.getName());
      System.out.println("Display name: " + listing.getDisplayName());
      System.out.println("State: " + listing.getState());
    }
  }
}

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();

/**
 * Retrieves a listing from an Analytics Hub data exchange.
 *
 * @param {string} projectId - The Google Cloud project ID containing the data exchange and listing.
 * @param {string} locationId - The Google Cloud location or region of the data exchange (for example, 'us' or 'us-central1').
 * @param {string} dataExchangeId - The caller-chosen identifier of the data exchange containing the listing.
 * @param {string} listingId - The caller-chosen identifier of the listing to retrieve.
 */
async function getListing(projectId, locationId, dataExchangeId, listingId) {
  try {
    const name = `projects/${projectId}/locations/${locationId}/dataExchanges/${dataExchangeId}/listings/${listingId}`;

    const request = {
      name,
    };

    const [listing] = await client.getListing(request);

    console.log(`Listing name: ${listing.name}`);
    console.log(`Display name: ${listing.displayName}`);
    console.log(`State: ${listing.state}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Listing not found: ${err.message}`);
    } else {
      console.error(`API error: ${err.message}`);
    }
  }
}

module.exports = { getListing };

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽工具。