เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การเขียน Schema โดยตรง

ที่ผ่านมาเราโหลด Schema มาหลายวิธีแล้ว คราวนี้ลองกำหนด Schema โดยตรงกันบ้าง โดยใช้ข้อมูลจาก Data Dictionary ด้านล่างนี้:

ตัวแปร คำอธิบาย
age อายุของบุคคล
education_num ระดับการศึกษา
marital_status สถานภาพสมรส
occupation อาชีพ
income ระดับรายได้ (categorical)

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

PySpark เบื้องต้น

ดูคอร์ส

คำแนะนำการฝึกหัด

  • กำหนด schema ของข้อมูล โดยระบุชื่อคอลัมน์ (age, education_num, marital_status, occupation และ income) พร้อมชนิดข้อมูลของแต่ละคอลัมน์ และตั้งค่า argument sep= เป็น comma
  • อ่านข้อมูลจากไฟล์ที่คั่นด้วย comma ชื่อ adult_reduced_100.csv
  • แสดง schema ของ DataFrame ที่ได้

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

from pyspark.sql.types import StructType, StructField, IntegerType, StringType

# Fill in the schema with the columns you need from the exercise instructions
schema = StructType([____("____",____()),
                     ____("____",____()),
                     ____("marital_status",StringType()),
                     StructField("____",____()),
                     ____("____",____()),
                    ])

# Read in the CSV, using the schema you defined above
census_adult = spark.read.csv("adult_reduced_100.csv", sep='____', header=False, schema=schema)

# Print out the schema
census_adult.____
แก้ไขและรันโค้ด