Çıkart Dönüştür Seç

Python ile Spark SQL'e Giriş

Mark Plutowski

Data Scientist

ETS

Python ile Spark SQL'e Giriş

Çıkart Dönüştür Seç

Python ile Spark SQL'e Giriş

Çıkart, Dönüştür ve Seç

  • Çıkartma
  • Dönüştürme
  • Seçim
Python ile Spark SQL'e Giriş

Yerleşik fonksiyonlar

from pyspark.sql.functions import split, explode
Python ile Spark SQL'e Giriş

length fonksiyonu

from pyspark.sql.functions import length
df.where(length('sentence') == 0)
Python ile Spark SQL'e Giriş

Özel bir fonksiyon oluşturma

  • Kullanıcı Tanımlı Fonksiyon
  • UDF
Python ile Spark SQL'e Giriş

udf fonksiyonunu içe aktarma

from pyspark.sql.functions import udf
Python ile Spark SQL'e Giriş

Boole UDF oluşturma

print(df)
DataFrame[textdata: string]
from pyspark.sql.functions import udf
from pyspark.sql.types import BooleanType
Python ile Spark SQL'e Giriş

Boole UDF oluşturma

short_udf = udf(lambda x: 
                          True if not x or len(x) < 10 else False, 
                          BooleanType())
df.select(short_udf('textdata')\
  .alias("is short"))\
  .show(3)
+--------+
|is short|
+--------+
|   false|
|    true|
|   false|
+--------+
Python ile Spark SQL'e Giriş

Önemli UDF dönüş tipleri

from pyspark.sql.types import StringType, IntegerType, FloatType, ArrayType
Python ile Spark SQL'e Giriş

Dizi UDF'i oluşturma

df3.select('word array', in_udf('word array').alias('without endword'))\
   .show(5, truncate=30)
+-----------------------------+----------------------+
|                   word array|       without endword|
+-----------------------------+----------------------+
|[then, how, many, are, there]|[then, how, many, are]|
|                  [how, many]|                 [how]|
|             [i, donot, know]|            [i, donot]|
|                  [quite, so]|               [quite]|
|   [you, have, not, observed]|      [you, have, not]|
+-----------------------------+----------------------+
Python ile Spark SQL'e Giriş

Dizi UDF'i oluşturma

from pyspark.sql.types import StringType, ArrayType
# Removes last item in array
in_udf = udf(lambda x: 
    x[0:len(x)-1] if x and len(x) > 1 
    else [], 
    ArrayType(StringType()))
Python ile Spark SQL'e Giriş

Seyrek vektör biçimi

  1. İndisler
  2. Değerler

Örnek:

  • Dizi: [1.0, 0.0, 0.0, 3.0]
  • Seyrek vektör: (4, [0, 3], [1.0, 3.0])
Python ile Spark SQL'e Giriş

Vektör verilerle çalışma

  • hasattr(x, "toArray")
  • x.numNonzeros())
Python ile Spark SQL'e Giriş

Hadi pratik yapalım!

Python ile Spark SQL'e Giriş

Preparing Video For Download...