Wprowadzenie do PySpark
Benjamin Schmidt
Data Engineer
spark.read.csv("path/to/file.csv")
spark.read.json("path/to/file.json")
spark.read.parquet("path/to/file.parquet")
Spark może wywnioskować schemat z danych przy użyciu inferSchema=True
Ręczna definicja schematu zapewnia lepszą kontrolę – przydatna dla stałych struktur danych

IntegerType: Liczby całkowite1, 3478, -18904569223347758063.14159"This is an example of a string."# Import the necessary types as classes
from pyspark.sql.types import (StructType,
StructField, IntegerType,
StringType, ArrayType)
# Construct the schema
schema = StructType([
StructField("id", IntegerType(), True),
StructField("name", StringType(), True),
StructField("scores", ArrayType(IntegerType()), True)
])
# Set the schema
df = spark.createDataFrame(data, schema=schema)
.select(), aby wybrać kolumny.filter() lub .where(), aby filtrować wiersze.sort(), aby posortować według kolumn# Select and show only the name and age columns
df.select("name", "age").show()
# Filter on age > 30
df.filter(df["age"] > 30).show()
# Use Where to filter match a specific value
df.where(df["age"] == 30).show()
# Use Sort to sort on age
df.sort("age", ascending=False).show()
.sort() lub .orderBy()na.drop(), aby usunąć wiersze z wartościami null# Sort using the age column
df.sort("age", ascending=False).show()
# Drop missing values
df.na.drop().show()
spark.read_json(): Wczytuje dane z JSONspark.read.schema(): Jawnie definiuje schemat.na.drop(): Usuwa wiersze z brakującymi wartościami.select(), .filter(), .sort(), .orderBy(): Podstawowe funkcje manipulacji danymiWprowadzenie do PySpark