設定服務端點

這個範例示範如何在使用 BigQuery 用戶端程式庫時設定替代端點。

程式碼範例

Go

在試行這個範例之前,請先按照 BigQuery 快速入門導覽課程:使用用戶端程式庫中的 Go 設定說明進行操作。詳情請參閱 BigQuery Go API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

import (
	"context"
	"fmt"

	"cloud.google.com/go/bigquery"
	"google.golang.org/api/option"
)

// setClientEndpoint creates a bigquery.Client pointing
// to a specific endpoint.
func setClientEndpoint(endpoint, projectID string) (*bigquery.Client, error) {
	// projectID := "my-project-id"
	// endpoint := "https://bigquery.googleapis.com/bigquery/v2/
	ctx := context.Background()
	client, err := bigquery.NewClient(ctx, projectID, option.WithEndpoint(endpoint))
	if err != nil {
		return nil, fmt.Errorf("bigquery.NewClient: %w", err)
	}
	defer client.Close()

	return client, nil
}

Node.js

在試行這個範例之前,請先按照 BigQuery 快速入門導覽課程:使用用戶端程式庫中的 Node.js 設定說明進行操作。詳情請參閱 BigQuery Node.js API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。 詳情請參閱「設定用戶端程式庫的驗證機制」。

// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');

function setClientEndpoint() {
  // Create a bigquery client pointing to a specific endpoint

  /**
   * TODO(developer): Uncomment the following line of code and fill in your region before running the sample.
   */
  // const region = 'my-region';

  const bigquery = new BigQuery({
    apiEndpoint: `${region}-bigquery.googleapis.com`,
  });

  console.log('API Endpoint:');
  console.log(bigquery.apiEndpoint);
}

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器