Mencantumkan pertukaran data (v1)

Mencantumkan semua pertukaran data dalam project dan lokasi yang ditentukan.

Contoh kode

Go

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Go di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Go API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.


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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Java API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Node.js API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

'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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Python API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

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

Langkah berikutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contohGoogle Cloud .