List data exchanges (v1)

List all data exchanges in a specified project and 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"
)

// 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

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 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

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 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

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

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

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}")

What's next

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