列出数据交换 (v1)

列出指定项目和位置中的所有数据交换。

代码示例

Go

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

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


import (
	"context"
	"fmt"
	"io"

	analyticshub "cloud.google.com/go/bigquery/analyticshub/apiv1"
	analyticshubpb "cloud.google.com/go/bigquery/analyticshub/apiv1/analyticshubpb"
	"google.golang.org/api/iterator"
)

// listDataExchanges retrieves all data exchanges in a specified location.
func listDataExchanges(ctx context.Context, w io.Writer, projectID string, locationID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)

	client, err := analyticshub.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("analyticshub.NewClient: %w", err)
	}
	defer client.Close()

	req := &analyticshubpb.ListDataExchangesRequest{
		Parent: parent,
	}

	it := client.ListDataExchanges(ctx, req)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListDataExchanges: %w", err)
		}
		fmt.Fprintf(w, "Data Exchange: %s (%s)\n", resp.GetName(), resp.GetDisplayName())
	}

	return nil
}

Java

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

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

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

public class ListDataExchanges {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project-id";
    String locationId = "example-location-id";
    listDataExchanges(projectId, locationId);
  }

  public static void listDataExchanges(String projectId, String locationId) throws IOException {
    try (AnalyticsHubServiceClient client = AnalyticsHubServiceClient.create()) {
      String parent = String.format("projects/%s/locations/%s", projectId, locationId);

      for (DataExchange dataExchange : client.listDataExchanges(parent).iterateAll()) {
        System.out.printf("Data exchange name: %s%n", dataExchange.getName());
        System.out.printf("Display name: %s%n", dataExchange.getDisplayName());
        System.out.printf("Description: %s%n", dataExchange.getDescription());
      }
    }
  }
}

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 data exchanges in a given project and location.
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The geographic location where the data exchanges reside (for example, 'us' or 'us-central1').
 */
async function listDataExchanges(projectId, locationId) {
  try {
    const parent = `projects/${projectId}/locations/${locationId}`;
    const request = {
      parent,
    };

    const [dataExchanges] = await client.listDataExchanges(request);

    for (const exchange of dataExchanges) {
      console.log(`Data Exchange: ${exchange.name}`);
      console.log(`Display Name: ${exchange.displayName}`);
      console.log(`Description: ${exchange.description}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Parent 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 call failed: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { listDataExchanges };

Python

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

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

import google.api_core.exceptions
from google.cloud import bigquery_analyticshub_v1


def list_data_exchanges(project_id: str, location_id: str) -> None:
    """Lists data exchanges in a specified project and location.

    Args:
        project_id: The Google Cloud project ID in which to list data exchanges.
        location_id: The Google Cloud region or multi-region where data exchanges
          are located.
    """
    client = bigquery_analyticshub_v1.AnalyticsHubServiceClient()
    parent = f"projects/{project_id}/locations/{location_id}"

    request = bigquery_analyticshub_v1.ListDataExchangesRequest(parent=parent)

    try:
        response = client.list_data_exchanges(request=request)
        for exchange in response:
            print(f"Name: {exchange.name}")
            print(f"Display name: {exchange.display_name}")
            print(f"Description: {exchange.description}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to list data exchanges: {error}")

后续步骤

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