การดำเนินการ RDD ใน PySpark

Big Data Fundamentals with PySpark

Upendra Devisetty

Science Analyst, CyVerse

ภาพรวมของการดำเนินการ PySpark

  • Transformations สร้าง RDD ใหม่
  • Actions ประมวลผลบน RDD
Big Data Fundamentals with PySpark

RDD Transformations

  • Transformations ทำงานแบบ Lazy evaluation

  • RDD Transformations พื้นฐาน

    • map(), filter(), flatMap() และ union()
Big Data Fundamentals with PySpark

map() Transformation

  • transformation map() ใช้ฟังก์ชันกับทุก element ใน RDD

map

RDD = sc.parallelize([1,2,3,4])
RDD_map = RDD.map(lambda x: x * x)
Big Data Fundamentals with PySpark

filter() Transformation

  • transformation filter() คืน RDD ใหม่ที่มีเฉพาะ element ที่ผ่านเงื่อนไข

filter

RDD = sc.parallelize([1,2,3,4])
RDD_filter = RDD.filter(lambda x: x > 2)
Big Data Fundamentals with PySpark

flatMap() Transformation

  • transformation flatMap() คืนค่าหลายค่าสำหรับแต่ละ element ใน RDD ต้นทาง

RDD = sc.parallelize(["hello world", "how are you"])
RDD_flatmap = RDD.flatMap(lambda x: x.split(" "))
Big Data Fundamentals with PySpark

union() Transformation

inputRDD = sc.textFile("logs.txt")
errorRDD = inputRDD.filter(lambda x: "error" in x.split())
warningsRDD = inputRDD.filter(lambda x: "warnings" in x.split())
combinedRDD = errorRDD.union(warningsRDD)
Big Data Fundamentals with PySpark

RDD Actions

  • Actions คือการดำเนินการที่คืนค่าหลังประมวลผลบน RDD

  • RDD Actions พื้นฐาน

    • collect()

    • take(N)

    • first()

    • count()

Big Data Fundamentals with PySpark

collect() และ take() Actions

  • collect() คืน element ทั้งหมดของชุดข้อมูลในรูปแบบ array

  • take(N) คืน array ที่มี N element แรกของชุดข้อมูล

RDD_map.collect()
[1, 4, 9, 16]
RDD_map.take(2)
[1, 4]
Big Data Fundamentals with PySpark

first() และ count() Actions

  • first() แสดง element แรกของ RDD
RDD_map.first()
[1]
  • count() คืนจำนวน element ทั้งหมดใน RDD
RDD_flatmap.count()
5
Big Data Fundamentals with PySpark

มาฝึกการดำเนินการ RDD กันเถอะ!

Big Data Fundamentals with PySpark

Preparing Video For Download...