תבניות של שאילתות על רשימות (גרסה 1)

הצגת רשימה של כל תבניות השאילתות בחילופי נתונים ב-Analytics Hub.

דוגמת קוד

Go

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Go במדריך למתחילים של BigQuery באמצעות ספריות לקוח. למידע נוסף, קראו את מאמרי העזרה של BigQuery Go API.

כדי לבצע אימות ב-BigQuery, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לספריות לקוח.


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

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Java במדריך למתחילים של BigQuery באמצעות ספריות לקוח. למידע נוסף, קראו את מאמרי העזרה של BigQuery Java API.

כדי לבצע אימות ב-BigQuery, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לספריות לקוח.

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

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Node.js במדריך למתחילים של BigQuery באמצעות ספריות לקוח. למידע נוסף, קראו את מאמרי העזרה של BigQuery Node.js API.

כדי לבצע אימות ב-BigQuery, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לספריות לקוח.

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

המאמרים הבאים

כדי לחפש ולסנן דוגמאות קוד למוצרים אחרים של Google Cloud , ראו דפדפן לדוגמה שלGoogle Cloud .