Elenca i data exchange (v1)

Elenca tutti i data exchange in un progetto e una località specifici.

Esempio di codice

Go

Prima di provare questo esempio, segui le istruzioni di configurazione di Go nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Go.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configura l'autenticazione per le librerie client.


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

Prima di provare questo esempio, segui le istruzioni di configurazione di Java nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Java.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configura l'autenticazione per le librerie client.

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

Prima di provare questo esempio, segui le istruzioni di configurazione di Node.js nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Node.js.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configura l'autenticazione per le librerie client.

'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

Prima di provare questo esempio, segui le istruzioni di configurazione di Python nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Python.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configura l'autenticazione per le librerie client.

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

Passaggi successivi

Per cercare e filtrare gli esempi di codice per altri prodotti Google Cloud , consulta il browser degli esempi diGoogle Cloud .