Operace s RDD v PySparku

Big Data Fundamentals with PySpark

Upendra Devisetty

Science Analyst, CyVerse

Přehled operací v PySparku

  • Transformace vytvářejí nová RDD
  • Akce provádějí výpočty nad RDD
Big Data Fundamentals with PySpark

Transformace RDD

  • Transformace využívají líné vyhodnocování

  • Základní transformace RDD

    • map(), filter(), flatMap() a union()
Big Data Fundamentals with PySpark

Transformace map()

  • Transformace map() použije funkci na všechny prvky RDD

map

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

Transformace filter()

  • Transformace filter() vrátí nové RDD pouze s prvky splňujícími podmínku

filter

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

Transformace flatMap()

  • Transformace flatMap() vrátí pro každý prvek původního RDD více hodnot

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

Transformace union()

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

Akce RDD

  • Operace, které vrátí hodnotu po provedení výpočtu nad RDD

  • Základní akce RDD

    • collect()

    • take(N)

    • first()

    • count()

Big Data Fundamentals with PySpark

Akce collect() a take()

  • collect() vrátí všechny prvky datové sady jako pole

  • take(N) vrátí pole s prvními N prvky datové sady

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

Akce first() a count()

  • first() vypíše první prvek RDD
RDD_map.first()
[1]
  • count() vrátí počet prvků v RDD
RDD_flatmap.count()
5
Big Data Fundamentals with PySpark

Pojďme procvičit operace s RDD

Big Data Fundamentals with PySpark

Preparing Video For Download...