List organization data exchanges (v1)

Lists all data exchanges for an organization in a specified location.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Go API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

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

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

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

Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Node.js API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.

'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 };

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.