반응형

1. Databricks REST API 호출 구성 요소

  • Databricks workspace 인스턴스 이름
  • REST API 작업 유형 (예: GET, POST, PATCH 또는 DELETE)
  • 지정된 클러스터에 대한 정보를 가져오는 REST API 작업 경로 (예: /api/2.0/cluster/get)
  • Databricks 개인 액세스 토큰과 같은 Databricks 인증 정보
  • REST API 작업에 의해 지원되는 임의의 요청 페이로드 또는 요청 쿼리 파라미터 (예: 클러스터의 ID)

 

2. Databricks REST API Return 값

  • REST API 호출은 일반적으로 클러스터의 요청에 대한 정보를 포함하는 응답 payload를 반환
  • 응답 payload는 일반적으로 json 형태
 
curl --request GET "https://${DATABRICKS_HOST}/api/2.0/clusters/get" \
     --header "Authorization: Bearer ${DATABRICKS_TOKEN}" \
     --data '{ "cluster_id": "1234-567890-a12bcde3" }'

 

{
  "cluster_id": "1234-567890-a12bcde3",
  "creator_user_name": "someone@example.com",
  "...": "...",
  "cluster_name": "My New Cluster",
  "...": "...",
  "autotermination_minutes": 15,
  "...": "...",
  "state": "TERMINATED",
  "state_message": "Inactive cluster terminated (inactive for 15 minutes).",
  "...": "..."
}

 

3. Rate limits (특정 시간 내에 할 수 있는 API 호출 수)

 

4. Databricks REST API 사용 이유

  • 자동화 및 오케스트레이션
    • Databricks API를 사용하면 다양한 작업과 워크플로를 자동화하여 수동 개입을 줄이고 프로세스를 간소화
  • 기존 도구와 통합
    • 데이터 생태계의 다른 도구 및 서비스와 원활하게 통합 가능
  • 모니터링 및 관리
    • 모니터링 및 관리 기능에 대한 액세스를 제공하여 클러스터, 작업 및 노트북의 성능을 추적 가능
  • 보안 및 액세스 제어
    • API를 사용하면 보안 및 액세스 권한을 세밀하게 제어 가능
  • 효율성 및 생산성
    • 일상적인 작업과 프로세스를 자동화하여 효율성과 생산성 향상

 

5. Databricks REST API를 사용하기 위한 공통 설정

  • Databricks 호스트 이름
  • Databricks 사용자 액세스 토큰
    • 인증하려면 Databricks 사용자 액세스 토큰을 생성 필요
    • Databricks 사용자 액세스 토큰을 생성 방법
      • Databricks Workspace에 로그인
      • 오른쪽 상단에 있는 사용자 이름 클릭
      • 사용자 설정을 클릭
      • 설정 > 개발자 설정 > 액세스 토큰으로 이동
      • 새 토큰 생성을 클릭
      • 코멘트 및 토큰의 수명(일) 작성
      • 토큰 복사

 

6. Databricks REST API 사용 방법

  • Databricks API Documents : https://docs.databricks.com/api/workspace/introduction
  • Curl 요청
    • 클라이언트 URL의 약자
    • 네트워크 요청을 위한 다목적 명령 줄 도구이자 라이브러리
    • HTTP, FTP, SMTP 등 다양한 프로토콜을 지원
    • 웹 서비스 및 API와 상호 작용을 위해 사용
    • Databricks API 실행 방법
      • 환경 변수 설정
        set DATABRICKS_HOST=example.databricks.com/
        set DATABRICKS_TOKEN=dapixxxxx

      • GET request: List all cluster
        curl --request GET "https://example.databricks.com/api/2.0/clusters/list" --header "Authorization: Bearer dapi#######################"
      • POST request: Start terminated cluster
        curl --request POST "https://example.databricks.com/api/2.0/clusters/start" --header "Authorization: Bearer dapi#######################" --data "{\"cluster_id\": \"1015-073040-t4hw9fg1\"}"
      • PATCH request: Update cluster permissions
        curl --request PATCH "https://example.databricks.com/api/2.0/permissions/clusters/1015-073040-t4hw9fg1" --header "Authorization: Bearer dapi#######################" --data "{\"access_control_list\": [{\"user_name\": \"abc@xyz.com\", \"permission_level\": \"CAN_MANAGE\"}, {\"user_name\": \"def@xyz.com\", \"permission_level\": \"CAN_MANAGE\"}]}"
  • Python 요청

    • Databricks API 실행 방법
      • 환경 변수 설정
        import requests
         
        DOMAIN = 'example.databricks.com'
        TOKEN = 'dapixxxxx'
          
      • GET request: List all scopes
        # List scopes
        response = requests.get(
          'https://%s/api/2.0/secrets/scopes/list' % (DOMAIN),
          headers={'Authorization': 'Bearer %s' % TOKEN},
          json={}
        )
         
      • POST request: Create a new secret scope
        # Create a secret scope
        response = requests.post(
          'https://%s/api/2.0/secrets/scopes/create' % (DOMAIN),
          headers={'Authorization': 'Bearer %s' % TOKEN},
          json={'scope': 'abc'}
        )
      • Python에서 API 요청을 실행 후 response확인 가능
        print(response.status_code)  # status code 확인
        response.json()  # json형태로 response 확인
  • Postman 사용

    • Databricks API 실행 방법
      • 환경 변수 설정
      • GET request: List all clusters
      • POST request: Start terminated cluster

      • PATCH request: Update cluster permissions
  • Databricks-api Python package 사용

    • Databricks API 실행 방법
      • 패키지 설치
        pip install databricks-api
      • 환경 변수 설정
        from databricks_api import DatabricksAPI
         
        db = DatabricksAPI(
            host="example.databricks.com",
            token="dapixxx"
        )

      • GET request: List all clusters
        # List all clusters
        response = db.cluster.list_clusters(headers=None)

      • POST request: Start terminated cluster
        db.cluster.start_cluster(
            "1015-073040-t4hw9fg1",
            headers=None,
        )

      • Package에서 API 요청을 실행 후 response확인 가능
        print(response.status_code)  # status code 확인
        response.json()  # json형태로 response 확인

 

 

 

반응형

+ Recent posts