Obtenir un modèle (v1beta3)

Récupérez les métadonnées d'un modèle Dataflow stocké dans Cloud Storage.

Exemple de code

Go

Pour vous authentifier auprès de Dataflow, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.


import (
	"context"
	"fmt"
	"io"

	dataflow "cloud.google.com/go/dataflow/apiv1beta3"
	dataflowpb "cloud.google.com/go/dataflow/apiv1beta3/dataflowpb"
)

// getTemplate retrieves metadata for a Cloud Dataflow template.
func getTemplate(ctx context.Context, w io.Writer, projectID string, locationID string, gcsPath string) error {
	client, err := dataflow.NewTemplatesClient(ctx)
	if err != nil {
		return fmt.Errorf("NewTemplatesClient failed: %w", err)
	}
	defer client.Close()

	req := &dataflowpb.GetTemplateRequest{
		ProjectId: projectID,
		Location:  locationID,
		Template: &dataflowpb.GetTemplateRequest_GcsPath{
			GcsPath: gcsPath,
		},
		View: dataflowpb.GetTemplateRequest_METADATA_ONLY,
	}

	resp, err := client.GetTemplate(ctx, req)
	if err != nil {
		return fmt.Errorf("client.GetTemplate: %w", err)
	}

	fmt.Fprintf(w, "Template name: %s\n", resp.GetMetadata().GetName())
	fmt.Fprintf(w, "Description: %s\n", resp.GetMetadata().GetDescription())

	return nil
}

Node.js

Pour vous authentifier auprès de Dataflow, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

'use strict';

const {TemplatesServiceClient} = require('@google-cloud/dataflow');
const {status} = require('@grpc/grpc-js');

const client = new TemplatesServiceClient();

/**
 * Use the getTemplate method
 *
 * @param {string} projectId - The ID of the Google Cloud project that the job belongs to.
 * @param {string} locationId - The regional endpoint to which to direct the request (e.g., 'us-central1').
 * @param {string} gcsPath - A Cloud Storage path to the template to retrieve, beginning with 'gs://'.
 */
async function getTemplate(projectId, locationId, gcsPath) {
  try {
    const request = {
      projectId,
      location: locationId,
      gcsPath,
      view: 'METADATA_ONLY',
    };

    const [response] = await client.getTemplate(request);

    console.log(`Template type: ${response.templateType}`);
    if (response.metadata) {
      console.log(`Template name: ${response.metadata.name}`);
      console.log(`Template description: ${response.metadata.description}`);
    }

    return response;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Template not found at ${gcsPath}: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied accessing template: ${err.message}`);
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error(`Invalid argument provided: ${err.message}`);
    } else {
      console.error(`API error retrieving template: ${err.message}`);
    }
    throw err;
  }
}

module.exports = { getTemplate };

Étapes suivantes

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud , consultez l'explorateur d'exemplesGoogle Cloud .