Python Databricks API wrapper using requests module
Project description
Databricks API Documentation
This package is a Python Implementation of the Databricks API for structured and programmatic use. This Python implementation requires that your Databricks API Token be saved as an environment variable in your system: export DATABRICKS_TOKEN=MY_DATABRICKS_TOKEN
in OSX / Linux. Or in Windows by searching for System Environment Variables in the Start Menu and adding it in the editor. For details see this guide.
UPDATE
Tokens can now be passed when a particular class is instantiated. E.g.:
from databricksapi import Workspace
url = 'https://my-databricks-instance.com'
token = 'dapiXXXXXXXXXXXXX'
ws = Workspace(url, token)
Installation
You can either use pip install databricksapi
to install it globally, or you can clone the repository. Please note that only compatability with Python 3.7+ is guaranteed.
APIs Included
- Token
- Secrets
- Clusters
- SCIM (Experimental)
- Jobs
- DBFS
- Groups
- Instance Profiles
- Libraries
- Workspace
Imports
The modules above can be imported as follows
from databricksapi import Token, Jobs, DBFS
url = 'https://url.for.databricks.net'
token_instance = Token(url)
jobs_instance = Jobs(url)
Token API
The Token API allows any user to create, list, and revoke tokens that can be used to authenticate and access Databricks REST APIs. Initial authentication to this API is the same as for all of the Databricks API endpoints.
Methods
- createToken(lifetime_seconds, comment)
- listTokens()
- revokeToken(token_id)
createToken(lifetime_seconds, comment)
Create and return a token.
url = 'https://url.for.databricks.net'
db_api = Token(url)
db_api.createToken(600, 'Test token')
listTokens()
List all Token IDs in your Databricks Environment.
url = 'https://url.for.databricks.net'
db_api = Token(url)
db_api.listTokens()
revokeToken(token_id)
Revoke an active Databricks token.
url = 'https://url.for.databricks.net'
db_api = Token(url)
#token_id can be obtained from using the listTokens() method
db_api.revokeToken('5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4')
Secrets API
The Secrets API allows you to manage secrets, secret scopes, and access permissions.
Methods
- createSecretScope(scope, initial_manage_principal)
- deleteSecretScope(scope)
- listSecretScopes()
- putSeceret(value, value_type, scope, key)
- deleteSecret(scope, key)
- listSecrets(scope)
- putSecretACL(scope, principal, permission)
- deleteSecretACL(scope, principal)
- getSecretACL(*scope, principal)
- listSecretACL(scope, principal)
createSecretScope(scope, initial_manage_princial)
Creates a new secret scope.
The scope name must consist of alphanumeric characters, dashes, underscores, and periods, and may not exceed 128 characters. The maximum number of scopes in a workspace is 100.
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
scope = 'SomeSecretScope'
initial_manage_princial = 'users'
db_api.createSecretScope(scope, initial_manage_princial)
deleteSecretScope(scope)
Delete a secret scope.
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
scope = 'SomeSecretScope'
db_api.deleteSecretScope(scope)
listSecretScopes()
List all secret scopes in the workspace
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
db_api.listSecretScopes()
putSecret(value, value_type, scope, key)
Inserts a secret under the provided scope with the given name. If a secret already exists with the same name, this command overwrites the existing secret’s value. The server encrypts the secret using the secret scope’s encryption settings before storing it. You must have WRITE or MANAGE permission on the secret scope.
The value_type
parameter can either be set to string
or bytes
depending on the type fo value the user passes in.
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
#set parameters
value = 'BeepBoop'
value_type = 'string'
scope = 'SomeSecretScope'
key = 'uniqueScopekey'
db_api.putSecret(value, value_type, scope, key)
deleteSecret(scope, key)
Deletes the secret stored in this secret scope. You must have WRITE or MANAGE permission on the secret scope.
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
scope = 'SomeSecretScope'
key = 'uniqueScopekey'
db_api.deleteSecret(scope, key)
listSecrets(scope)
Lists the secret keys that are stored at this scope. This is a metadata-only operation; secret data cannot be retrieved using this API. Users need READ permission to make this call.
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
scope = 'SomeSecretScope'
db_api.listSecrets(scope)
putSecretACL(scope, principal, permission)
Creates or overwrites the ACL associated with the given principal (user or group) on the specified scope point. In general, a user or group will use the most powerful permission available to them
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
scope = 'SomeSecretScope'
prinicpal = 'users'
permission = 'READ'
db_api.putSecretACL(scope, principal, permission)
deleteSecretACL(scope, principal)
Deletes the given ACL on the given scope.
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
scope = 'SomeSecretScope'
prinicpal = 'users'
db_api.deleteSecretACL(scope, principal)
getSecretACL(*scope, principal)
Describes the details about the given ACL, such as the group and permission.
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
scope = 'SomeSecretScope'
prinicpal = 'users'
db_api.getSecretACL(scope, principal)
listSecretACL(scope, principal)
Lists the ACLs set on the given scope.
url = 'https://url.for.databricks.net'
db_api = Secrets(url)
scope = 'SomeSecretScope'
prinicpal = 'users'
db_api.listSecretACL(scope, principal)
Clusters API
The Clusters API allows you to create, start, edit, list, terminate, and delete clusters via the API. The maximum allowed size of a request to the Clusters API is 10MB.
Methods
- createCluster(worker, worker_type, cluster_name, spark_version, cluster_log_conf, node_type_id, driver_node_type_id=None, spark_conf=None, aws_attributes=None, ssh_public_keys=None, custom_tags=None, init_scripts=None, spark_env_vars=None, autotermination_minutes=None, enable_elastic_disk=None)
- editCluster(worker, worker_type, cluster_name, spark_version, cluster_log_conf, node_type_id, driver_node_type_id=None, spark_conf=None, aws_attributes=None, ssh_public_keys=None, custom_tags=None, init_scripts=None, spark_env_vars=None, autotermination_minutes=None, enable_elastic_disk=None)
- startCluster(cluster_id)
- restartCluster(cluster_id)
- resizeCluster(cluster_id, worker, worker_type)
- terminateCluster(cluster_id)
- deleteCluster(cluster_id)
- getCluster(cluster_id)
- pinCluster(cluster_id)
- unpinCluster(cluster_id)
- listClusters()
- listNodeTypes()
- listZones()
- getSparkVersions()
- getClusterEvents(cluster_id, order='DESC', start_time=None, end_time=None, event_types=None, offset=None, limit=None)
createCluster(worker, worker_type, cluster_name, spark_version, cluster_log_conf, node_type_id, driver_node_type_id=None, spark_conf=None, aws_attributes=None, ssh_public_keys=None, custom_tags=None, init_scripts=None, spark_env_vars=None, autotermination_minutes=None, enable_elastic_disk=None)
Creates a new Spark cluster. This method acquires new instances from the cloud provider if necessary. This method is asynchronous; the returned cluster_id can be used to poll the cluster state. When this method returns, the cluster is in a PENDING state. The cluster is usable once it enters a RUNNING state.
The worker_type
can be either workers
or autoscale
. If a autoscale
is set, then the min_workers
and max_workers
must be specified.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
worker = 25
worker_type = 'workers'
cluster_name = 'TestCluster'
spark_version = '4.0.x-scala2.11'
cluster_log_conf = '/dbfs/log/path'
node_type_id = 'i3.xlarge'
db_api.createCluster(worker=worker, worker_type=worker_type, cluster_name=cluster_name, spark_version=spark_version, cluster_log_conf=cluster_log_conf, node_type_id=node_type_id)
editCluster(worker, worker_type, cluster_name, spark_version, cluster_log_conf, node_type_id, driver_node_type_id=None, spark_conf=None, aws_attributes=None, ssh_public_keys=None, custom_tags=None, init_scripts=None, spark_env_vars=None, autotermination_minutes=None, enable_elastic_disk=None)
Edit an existings clusters configuration.
The worker_type
can be either workers
or autoscale
. If a autoscale
is set, then the min_workers
and max_workers
must be specified.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
worker = 35
worker_type = 'workers'
cluster_name = 'TestCluster'
spark_version = '4.0.x-scala2.11'
cluster_log_conf = '/dbfs/new/log/path'
node_type_id = 'i5.xlarge'
db_api.editCluster(worker=worker, worker_type=worker_type, cluster_name=cluster_name, spark_version=spark_version, cluster_log_conf=cluster_log_conf, node_type_id=node_type_id)
startCluster(cluster_id)
Starts a terminated Spark cluster given its ID.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
db_api.startCluster(cluster_id)
restartCluster(cluster_id)
Restarts a Spark cluster given its id. If the cluster is not in a RUNNING state, nothing will happen.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
db_api.restartCluster(cluster_id)
resizeCluster(cluster_id, worker, worker_type)
Resizes a cluster to have a desired number of workers. This will fail unless the cluster is in a RUNNING state.
The parameter worker_type
can be one of workers
or autoscale
.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
workers = 30
db_api.resizeCluster(cluster_id, workers, worker_type='workers')
terminateCluster(cluster_id)
Terminates a Spark cluster given its id. The cluster is removed asynchronously. Once the termination has completed, the cluster will be in a TERMINATED state. If the cluster is already in a TERMINATING or TERMINATED state, nothing will happen.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
db_api.terminateCluster(cluster_id)
deleteCluster(cluster_id)
Permanently deletes a Spark cluster. If the cluster is running, it is terminated and its resources are asynchronously removed. If the cluster is terminated, then it is immediately removed.
You cannot perform any action on a permanently deleted cluster and a permanently deleted cluster is no longer returned in the cluster list.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
db_api.deleteCluster(cluster_id)
getCluster(cluster_id)
Returns information about all pinned clusters, currently active clusters, up to 70 of the most recently terminated interactive clusters in the past 30 days, and up to 30 of the most recently terminated job clusters in the past 30 days.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
db_api.getCluster(cluster_id)
pinCluster(cluster_id)
Pinning a cluster ensures that the cluster is always returned by the List API. Pinning a cluster that is already pinned has no effect.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
db_api.pinCluster(cluster_id)
unpinCluster(cluster_id)
Unpinning a cluster will allow the cluster to eventually be removed from the list returned by the List API. Unpinning a cluster that is not pinned has no effect.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
db_api.unpinCluster(cluster_id)
listClusters()
Retrieves the information for a cluster given its identifier. Clusters can be described while they are running, or up to 30 days after they are terminated.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
db_api.listClusters()
listNodeTypes()
Returns a list of supported Spark node types. These node types can be used to launch a cluster.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
db_api.listNodeTypes()
listZones()
Returns a list of availability zones where clusters can be created in (ex: us-west-2a). These zones can be used to launch a cluster.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
db_api.listZones()
getSparkVersions()
Returns the list of available Spark versions. These versions can be used to launch a cluster.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
db_api.getSparkVersions()
getClusterEvents(cluster_id, order='DESC', start_time=None, end_time=None, event_types=None, offset=None, limit=None)
Retrieves a list of events about the activity of a cluster. This API is paginated. If there are more events to read, the response includes all the parameters necessary to request the next page of events.
url = 'https://url.for.databricks.net'
db_api = Clusters(url)
cluster_id = '1202-211320-brick1'
db_api.getClusterEvents(cluster_id)
Jobs API
The Jobs API allows you to create, edit, and delete jobs via the API. The maximum allowed size of a request to the Jobs API is 10MB.
Methods
- createJob(cluster, cluster_type, task, task_type, name, libraries=None, email_notications=None, timeout_seconds=None, max_retries=None, min_retry_interval_millis=None, retry_on_timeout=None,schedule=None, max_concurrent_runs=None)
- listJobs()
- deleteJob(job_id)
- batchDelete(*args)
- getJob(job_id)
- resetJob(job_id, new_settings)
- runJob(job_id, job_type, params)
- runsSubmit(run_name, cluster, task, cluster_type, task_type, libraries=None, timeout_seconds=None)
- runsList(run, run_type, job_id, offset, limit)
- runsGet(run_id)
- runsExport(run_id, views_to_export)
- runsCancel(run_id)
- runsGetOutput(run_id)
- runsDelete(run_id)
createJob(cluster, cluster_type, task, task_type, name, libraries=None, email_notications=None, timeout_seconds=None, max_retries=None, min_retry_interval_millis=None, retry_on_timeout=None,schedule=None, max_concurrent_runs=None)
The cluster_type
parameter can be one of existing
or new
.
The task_type
parameter must be one of notebook
, jar
, submit
, or python
.
All other parameters are documented in the Databricks Rest API.
batchDelete(*args)
Takes in a comma separated list of Job IDs to be deleted. This method is a wrapper around the deleteJob
method.
runJob(job_id, job_type, params)
The job_type
parameter must be one of notebook
, jar
, submit
or python
.
runsSubmit(run_name, cluster, task, cluster_type, task_type, libraries=None, timeout_seconds=None)
The cluster_type
parameter can be one of existing
or new
.
runsList(run, run_type, job_id, offset, limit)
The run_type
parameter must be one of completed
or active
.
DBFS API
The DBFS API is a Databricks API that makes it simple to interact with various data sources without having to include your credentials every time you read a file.
Methods
- addBlock(data, handle)
- closeStream(handle)
- createFile(path, overwrite)
- deleteFile(path, recursive)
- getStatus(path)
- listFiles(path)
- makeDirs(path)
- moveFiles(source_path, target_path)
- putFiles(path, overwrite, files, contents=None)
- readFiles(path, offset, length)
Groups API
The Groups API allows you to manage groups of users via the API. You must be a Databricks administrator to invoke this API.
Methods
- addMember(parent_name, name, name_type)
- createGroup(group_name)
- listGroupMembers(group_name, return_type='json')
- listGroups()
- listParents(name, name_type)
- removeMember(name, parent_name, name_type)
- deleteGroup(group_name)
listGroupMembers(group_name, return_type='json')
The default return_type
can be one of json
or list
, by default the paramter is set to json
. This is provide to simplify pulling usernames from the default return time which can be cumbersome.
listParents(name, name_type)
The name_type
parameter must be one of user
or group
.
removeMember(name, parent_name, name_type)
The name_type
parameter must be one of user
or group
.
Instance Profiles API
The Instance Profiles API allows admins to add, list, and remove instance profiles that users can launch clusters with. Regular users can list the instance profiles available to them.
Methods
- addProfile(profile_arn, skip_validation=None)
- listProfiles()
- removeProfile()
Libraries API
The Libraries API allows you to install and uninstall libraries and get the status of libraries on a cluster via the API.
Methods
- allClusterStatuses(status)
- clusterStatus(cluster_id)
- installLibrary(cluster_id, libraries)
- uninstallLibrary(cluster_id, libraries)
Worspace API
- deleteWorkspace(path, recursive)
- exportWorkspace(path, export_format, direct_download)
- getWorkspaceStatus(path)
- importWorkspace(path, export_format, language, content, overwrite)
- listWorkspace(path)
- mkdirsWorkspace(path)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file databricksapi-1.1.8.tar.gz
.
File metadata
- Download URL: databricksapi-1.1.8.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5a4d732f8087a738203a4207b72548f61315918c2739fd95a93fbcfb157c1f1 |
|
MD5 | e5e47d48c6ef1a601260d2e040420754 |
|
BLAKE2b-256 | c9ead3c9533c516bf076d2923931e10d7b417a75c92528a873ebe840538eeacc |