Package: databricksapi
Databricks API DocumentationThis package is a Python Implementation of theDatabricks APIfor 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_TOKENin OSX / Linux. Or in Windows by searching for System Environment Variables in the Start Menu and adding it in the editor. For details see thisguide.UPDATETokens can now be passed when a particular class is instantiated. E.g.:fromdatabricksapiimportWorkspaceurl='https://my-databricks-instance.com'token='dapiXXXXXXXXXXXXX'ws=Workspace(url,token)InstallationYou can either usepip install databricksapito install it globally, or you can clone the repository. Please note that only compatability with Python 3.7+ is guaranteed.APIs IncludedTokenSecretsClustersSCIM (Experimental)JobsDBFSGroupsInstance ProfilesLibrariesWorkspaceImportsThe modules above can be imported as followsfromdatabricksapiimportToken,Jobs,DBFSurl='https://url.for.databricks.net'token_instance=Token(url)jobs_instance=Jobs(url)Token APIThe 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.MethodscreateToken(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() methoddb_api.revokeToken('5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4')Secrets APIThe Secrets API allows you to manage secrets, secret scopes, and access permissions.MethodscreateSecretScope(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 workspaceurl='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.Thevalue_typeparameter can either be set tostringorbytesdepending on the type fo value the user passes in.url='https://url.for.databricks.net'db_api=Secrets(url)#set parametersvalue='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 themurl='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 APIThe 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.MethodscreateCluster(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.Theworker_typecan be eitherworkersorautoscale. If aautoscaleis set, then themin_workersandmax_workersmust be specified.url='https://url.for.databricks.net'db_api=Clusters(url)worker=25worker_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.Theworker_typecan be eitherworkersorautoscale. If aautoscaleis set, then themin_workersandmax_workersmust be specified.url='https://url.for.databricks.net'db_api=Clusters(url)worker=35worker_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 parameterworker_typecan be one ofworkersorautoscale.url='https://url.for.databricks.net'db_api=Clusters(url)cluster_id='1202-211320-brick1'workers=30db_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 APIThe 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.MethodscreateJob(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)Thecluster_typeparameter can be one ofexistingornew.
Thetask_typeparameter must be one ofnotebook,jar,submit, orpython.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 thedeleteJobmethod.runJob(job_id,job_type,params)Thejob_typeparameter must be one ofnotebook,jar,submitorpython.runsSubmit(run_name,cluster,task,cluster_type,task_type,libraries=None,timeout_seconds=None)Thecluster_typeparameter can be one ofexistingornew.runsList(run,run_type,job_id,offset,limit)Therun_typeparameter must be one ofcompletedoractive.DBFS APIThe 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.MethodsaddBlock(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 APIThe Groups API allows you to manage groups of users via the API. You must be a Databricks administrator to invoke this API.MethodsaddMember(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 defaultreturn_typecan be one ofjsonorlist, by default the paramter is set tojson. This is provide to simplify pulling usernames from the default return time which can be cumbersome.listParents(name,name_type)Thename_typeparameter must be one ofuserorgroup.removeMember(name,parent_name,name_type)Thename_typeparameter must be one ofuserorgroup.Instance Profiles APIThe 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.MethodsaddProfile(profile_arn,skip_validation=None)listProfiles()removeProfile()Libraries APIThe Libraries API allows you to install and uninstall libraries and get the status of libraries on a cluster via the API.MethodsallClusterStatuses(status)clusterStatus(cluster_id)installLibrary(cluster_id,libraries)uninstallLibrary(cluster_id,libraries)Worspace APIdeleteWorkspace(path,recursive)exportWorkspace(path,export_format,direct_download)getWorkspaceStatus(path)importWorkspace(path,export_format,language,content,overwrite)listWorkspace(path)mkdirsWorkspace(path)