練習記錄日誌 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())