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 は を使用してデータからスキーマを推測できる inferSchema=True

IntegerType: 整数1、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()を使って特定の列を選択する.filter()または.where()を使用して、条件に基づいて行をフィルター処理します.sort() を使用して、列のコレクション {{3}} で順序付けする# 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() または .orderBy() {{1}} を使用してデータを並べ替えるna.drop()でnull値の行を削除する# Sort using the age column
df.sort("age", ascending=False).show()
# Drop missing values
df.na.drop().show()
spark.read_json(): JSON からデータを読み込むspark.read.schema(): スキーマを明示的に定義する.na.drop(): 欠損値を含む行を削除する.select(), .filter(), .sort(), .orderBy(): 基本的なデータ操作関数PySpark 入門