Membuat daftar template kueri (v1)

Mencantumkan semua template kueri dalam pertukaran data Analytics Hub.

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

// listQueryTemplates lists query templates in an Analytics Hub data exchange.
func listQueryTemplates(ctx context.Context, w io.Writer, projectID string, locationID string, dataExchangeID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s/dataExchanges/%s", projectID, locationID, dataExchangeID)

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

	req := &analyticshubpb.ListQueryTemplatesRequest{
		Parent: parent,
	}

	it := client.ListQueryTemplates(ctx, req)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListQueryTemplates failed: %w", err)
		}
		fmt.Fprintf(w, "Query template: %s\n", resp.GetName())
	}
	fmt.Fprintln(w, "Listed query templates.")

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

public class ListQueryTemplates {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project_id";
    String locationId = "example-location_id";
    String dataExchangeId = "example-data_exchange_id";
    listQueryTemplates(projectId, locationId, dataExchangeId);
  }

  public static void listQueryTemplates(String projectId, String locationId, String dataExchangeId)
      throws IOException {
    try (AnalyticsHubServiceClient client = AnalyticsHubServiceClient.create()) {
      String parent = DataExchangeName.of(projectId, locationId, dataExchangeId).toString();
      for (QueryTemplate queryTemplate : client.listQueryTemplates(parent).iterateAll()) {
        System.out.println("Query template name: " + queryTemplate.getName());
        System.out.println("Display name: " + queryTemplate.getDisplayName());
      }
    }
  }
}

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 query templates in an Analytics Hub data exchange.
 *
 * @param {string} projectId - Google Cloud project ID containing the data exchange.
 * @param {string} locationId - Google Cloud region or multi-region of the data exchange (for example, 'us').
 * @param {string} dataExchangeId - ID of the data exchange containing the query templates.
 */
async function listQueryTemplates(projectId, locationId, dataExchangeId) {
  try {
    const parent = `projects/${projectId}/locations/${locationId}/dataExchanges/${dataExchangeId}`;

    const [queryTemplates] = await client.listQueryTemplates({
      parent,
    });

    console.log(`Listed query templates for ${parent}:`);
    if (queryTemplates.length === 0) {
      console.log('No query templates found.');
    }
    for (const queryTemplate of queryTemplates) {
      console.log(`Query template: ${queryTemplate.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 === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument provided: ${err.message}`);
    } else {
      throw err;
    }
  }
}

module.exports = { listQueryTemplates };

Langkah berikutnya

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