Obtén una plantilla (v1beta3)

Recupera los metadatos de una plantilla de Dataflow almacenada en Cloud Storage.

Muestra de código

Go

Para autenticarte en Dataflow, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo 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

Para autenticarte en Dataflow, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo 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 };

¿Qué sigue?

Si quieres buscar y filtrar muestras de código para otros productos de Google Cloud , consulta el navegador de muestras deGoogle Cloud .