在 Azure Databricks Notebook 上检索群集不活动时间

Posted

技术标签:

【中文标题】在 Azure Databricks Notebook 上检索群集不活动时间【英文标题】:Retrieve Cluster Inactivity Time on Azure Databricks Notebook 【发布时间】:2021-10-12 21:25:07 【问题描述】:

我是 Azure Databricks 的新手,我正在将它用于一个项目。

正如here 在Automatic termination 的文档中提到的那样

您还可以为集群设置自动终止。 在集群创建过程中,您可以在几分钟内指定一个inactivity period,在此之后您希望集群终止。 如果集群上运行的current timelast command之间的差异超过了指定的不活动时间, Azure Databricks 会自动终止该集群。

是否有一种解决方法可以通过Cluster API 或任何其他方法在 Azure Databricks Notebooks 上获取集群的实时非活动期(当前时间与集群上运行的最后一个命令之间的差异)?

【问题讨论】:

【参考方案1】:
# Function to retrieve cluster inactivity time
from datetime import datetime
import time

def cluster_inactivity_time(log_file_path):
 
  # Open log4j-active.log and read last line
  with open(log_file_path, "r") as file:
    first_line = file.readline()
    for last_line in file:
        pass
      
  # Convert last lines' timestamp to milliseconds
  last_run_time = last_line[9:17]
  current_date = datetime.now().strftime('%Y-%m-%d')
  last_run_datetime = round(datetime.strptime(current_date + ' ' + last_run_time, "%Y-%m-%d %H:%M:%S").timestamp() * 1000)
  
  # Finding the difference between current time and last command run time
  current_time = round(time.time() * 1000)
  difference = current_time - last_run_datetime
  inactivity_time = datetime.fromtimestamp(difference / 1000.0)
  print(f'The Cluster has been Inactive for inactivity_time.hour:inactivity_time.minute:inactivity_time.second')


# Function Call
log_file_path = '/dbfs/cluster-logs/0809-101642-leap143/driver/log4j-active.log'
cluster_inactivity_time(log_file_path)

输出:

集群在 0:0:35 内处于非活动状态

【讨论】:

以上是关于在 Azure Databricks Notebook 上检索群集不活动时间的主要内容,如果未能解决你的问题,请参考以下文章

使用 azure databricks 读取 azure databricks 日志 json 文件

在 Azure 中检查 Databricks 运行时的版本

在 Azure Databricks 中编辑 Azure 存储帐户

Azure Databricks 的 Azure DevOps 管道

从 Azure Databricks 将数据写入 Azure Blob 存储

Azure Databricks - 解释 databricks 中的安装语法