組織のデータ エクスチェンジを一覧表示する(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"
)

// listOrgDataExchanges lists organization data exchanges in a specified location.
func listOrgDataExchanges(ctx context.Context, w io.Writer, organizationID string, locationID string) error {
	organization := fmt.Sprintf("organizations/%s/locations/%s", organizationID, locationID)

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

	req := &analyticshubpb.ListOrgDataExchangesRequest{
		Organization: organization,
	}

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

	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 com.google.cloud.bigquery.analyticshub.v1.ListOrgDataExchangesRequest;
import java.io.IOException;

public class ListOrgDataExchanges {

  public static void main(String[] args) throws IOException {
    String organizationId = "example-organization-id";
    String locationId = "example-location-id";
    listOrgDataExchanges(organizationId, locationId);
  }

  public static void listOrgDataExchanges(String organizationId, String locationId)
      throws IOException {
    // Initialize client within try-with-resources to ensure automatic cleanup
    try (AnalyticsHubServiceClient client = AnalyticsHubServiceClient.create()) {
      String organization =
          String.format("organizations/%s/locations/%s", organizationId, locationId);

      ListOrgDataExchangesRequest request =
          ListOrgDataExchangesRequest.newBuilder()
              .setOrganization(organization)
              .build();

      for (DataExchange dataExchange : client.listOrgDataExchanges(request).iterateAll()) {
        System.out.println("Data exchange: " + dataExchange.getName());
        System.out.println("Display name: " + dataExchange.getDisplayName());
      }
    }
  }
}

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 from an organization in BigQuery Analytics Hub.
 */
async function listOrgDataExchanges() {
  try {
    const organization = 'example-organization';
    const location = 'example-location';
    const orgLocation = `organizations/${organization}/locations/${location}`;

    const request = {
      organization: orgLocation,
    };

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

    console.log(`Retrieved ${dataExchanges.length} data exchange(s)`);
    for (const dataExchange of dataExchanges) {
      console.log(`Data exchange: ${dataExchange.name}`);
    }
  } 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 !== undefined) {
      console.error(`API error (status ${err.code}):`, err.message);
    } else {
      throw err;
    }
  }
}

module.exports = { listOrgDataExchanges };

次のステップ

他の Google Cloud プロダクトのコードサンプルを検索およびフィルタするには、Google Cloud サンプル ブラウザをご覧ください。