开始使用免费开始使用

实践日志记录 2

启动时将执行以下代码:

import logging
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG,
                    format='%(levelname)s - %(message)s')

在本节课中,您了解到会触发 action 的 Spark 操作需要谨慎记录日志,否则可能会在不知不觉中浪费计算资源。现在请练习识别哪些日志语句会对 dataframe 或表触发 action。

已提供一个 dataframe text_df。该 dataframe 已注册为名为 table1 的表。

本练习是课程的一部分

Python 中的 Spark SQL 入门

查看课程

练习说明

  • 下面给出了若干条日志语句,初始都被注释掉。请取消注释其中不会对 text_df 触发 action 的五条语句。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Uncomment the 5 statements that do NOT trigger text_df
# logging.debug("text_df columns: %s", text_df.columns)
# logging.info("table1 is cached: %s", spark.catalog.isCached(tableName="table1"))
# logging.warning("The first row of text_df: %s", text_df.first())
# logging.error("Selected columns: %s", text_df.select("id", "word"))
# logging.info("Tables: %s", spark.sql("show tables").collect())
# logging.debug("First row: %s", spark.sql("SELECT * FROM table1 limit 1"))
# logging.debug("Count: %s", spark.sql("SELECT COUNT(*) AS count FROM table1").collect())
编辑并运行代码