พื้นฐาน Notebook

Introduction to Databricks Lakehouse

Gang Wang

Senior Data Scientist

Databricks Notebook คืออะไร?

$$

recraft: half: A scientist at a clean modern laboratory workbench with multiple colorful instruments and tools arranged neatly, representing a multi-tool development environment

$$

  • เอกสารแบบโต้ตอบ ที่รันโค้ดได้
  • เชื่อมต่อกับ cluster เพื่อประมวลผล
  • รวมโค้ด ผลลัพธ์ และเอกสารไว้ในที่เดียว
  • รองรับ Python, SQL, Scala และ R
Introduction to Databricks Lakehouse

Magic commands

# Default language: Python
df = spark.table("silver_taxi_trips")
display(df)
%sql
SELECT COUNT(*) AS total_trips
FROM silver_taxi_trips
%md
## Analysis notes
Revenue is **highest** in the Northeast region.
Introduction to Databricks Lakehouse

Magic commands ที่ใช้ได้

$$

Command Purpose
%python รันโค้ด Python
%sql รันคิวรี SQL
%scala รันโค้ด Scala
%r รันโค้ด R
%md แสดงผล Markdown
%sh รันคำสั่ง shell

$$

radial: Cluster, python, sql, md, scala, r, sh

Introduction to Databricks Lakehouse

รัน Notebook อื่นด้วย %run

$$

  • %run รัน notebook อื่นในบริบทเดียวกัน
  • ฟังก์ชันและตัวแปรพร้อมใช้งานทันที
  • เหมาะสำหรับยูทิลิตีที่ใช้ซ้ำและ config ที่แชร์ร่วมกัน

$$

# Load shared helper functions
%run /Shared/utils/data_helpers
# Now use a function defined
# in data_helpers
clean_df = clean_nulls(raw_df)
Introduction to Databricks Lakehouse

การแปลผลลัพธ์

$$

  • เซลล์โค้ดแสดงผลลัพธ์ด้านล่างทันที
  • คิวรี SQL แสดงผลเป็นตารางแบบโต้ตอบ
  • DataFrame มีตัวเลือกการแสดงภาพในตัว
  • ข้อผิดพลาดแสดงstack trace พร้อมหมายเลขบรรทัด

$$

recraft: half: A laptop screen displaying a colorful data dashboard with charts, tables, and a code cell, representing notebook output and visualization

Introduction to Databricks Lakehouse

สรุป

$$

  • Notebook คือเอกสารแบบโต้ตอบที่เชื่อมต่อกับ cluster
  • Magic commands ช่วยใช้ Python, SQL, R, Scala และ Markdown ร่วมกันได้
  • %run โหลดฟังก์ชันจาก notebook อื่นเข้า session ปัจจุบัน
  • ผลลัพธ์แสดงแบบ inline พร้อมตารางและกราฟในตัว
Introduction to Databricks Lakehouse

มาฝึกกันเถอะ!

Introduction to Databricks Lakehouse

Preparing Video For Download...