세션 만들기(v1)

새 Dataproc 세션을 만듭니다.

코드 샘플

Node.js

이 샘플을 사용해 보기 전에 Managed Service for Apache Spark 빠른 시작: 클라이언트 라이브러리 사용의 Node.js 설정 안내를 따르세요. 자세한 내용은 Managed Service for Apache Spark Node.js API 참고 문서를 확인하세요.

Managed Service for Apache Spark에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

'use strict';

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

/**
 * Creates a Dataproc session.
 *
 * @param {string} sessionId - The caller-chosen ID to use for the session, which becomes the final component of the session's resource name. Must be 4-63 characters containing lowercase letters, numbers, and hyphens.
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Dataproc location (region) where the session is created (for example, 'us-central1').
 */
async function createSession(sessionId, projectId, locationId) {
  // Dataproc is a regional service requiring the regional API endpoint.
  const client = new SessionControllerClient({
    apiEndpoint: `${locationId}-dataproc.googleapis.com`,
  });

  try {
    const parent = `projects/${projectId}/locations/${locationId}`;
    const session = {
      jupyterSession: {
        kernel: 'PYTHON',
      },
    };

    const request = {
      parent,
      session,
      sessionId,
    };

    // Dataproc session creation is a long-running operation.
    const [operation] = await client.createSession(request);
    const [response] = await operation.promise();

    console.log(`Created session: ${response.name}`);
    return response;
  } catch (err) {
    if (err.code === status.ALREADY_EXISTS) {
      console.error(`Session already exists: ${err.message}`);
    } else if (err.code) {
      console.error(`Google API error (${err.code}): ${err.message}`);
    } else {
      throw err;
    }
  }
}

module.exports = { createSession };

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저 참조하기