Introduktion till 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 kan härleda scheman från data med inferSchema=True
Definiera schema manuellt för bättre kontroll – användbart för fasta datastrukturer

IntegerType: Heltal1, 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() för att välja specifika kolumner.filter() eller .where() för att filtrera rader baserat på villkor.sort() för att sortera efter en eller flera kolumner# 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() eller .orderBy()na.drop() för att ta bort rader med null-värden# Sort using the age column
df.sort("age", ascending=False).show()
# Drop missing values
df.na.drop().show()
spark.read_json(): Läs in data från JSONspark.read.schema(): Definiera scheman explicit.na.drop(): Ta bort rader med saknade värden.select(), .filter(), .sort(), .orderBy(): Grundläggande funktioner för datamanipuleringIntroduktion till PySpark