pyspark.sql.types

示例:

from datetime import datetime, date
from decimal import Decimal
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, IntegerType, FloatType, ArrayType, BooleanType, \
    DateType, TimestampType, DecimalType, MapType

# 初始化 SparkSession 对象
spark = SparkSession.builder \
    .appName("Example PySpark Script with Advanced Data Types") \
    .getOrCreate()

# 定义数据结构
schema = StructType([
    StructField("name", StringType(), True),
    StructField("age", IntegerType(), True),
    StructField("weight", FloatType(), True),
    StructField("interests", ArrayType(StringType()), True),
    StructField("has_license", BooleanType(), True),
    StructField("birthday", DateType(), True),
    StructField("last_checkup", TimestampType(), True),
    StructField("balance", DecimalType(precision=10, scale=2), True),
    StructField("preferences", MapType(StringType(), StringType()), True)
])

# 创建数据
data = [
    (
        "Alice",
        34,
        65.5,
        ["reading", "swimming"],
        True,
        date(1990, 1, 1),
        datetime(2023, 1, 1, 10, 0, 0),
        Decimal('12345.67'),
        {"theme": "dark", "language": "en"}
    ),
    (
        "Bob",
        45,
        80.2,
        ["gaming", "traveling"],
        False,
        date(1979, 5, 15),
        datetime(2023, 5, 15, 12, 0, 0),
        Decimal('54321.01'),
        {"theme": "light", "language": "fr"}
    ),
    (
        "Cathy",
        29,
        55.0,
        ["cooking", "painting"],
        True,
        date(1995, 8, 20),
        datetime(2023, 8, 20, 14, 0, 0),
        Decimal('7890.12'),
        {"theme": "dark", "language": "zh"}
    )
]

# 创建 DataFrame
df = spark.createDataFrame(data=data, schema=schema)

# 查看 DataFrame 结构
df.printSchema()

# 显示 DataFrame 内容
df.show(truncate=False)

# 关闭 SparkSession
spark.stop()
root
 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
 |-- weight: float (nullable = true)
 |-- interests: array (nullable = true)
 |    |-- element: string (containsNull = true)
 |-- has_license: boolean (nullable = true)
 |-- birthday: date (nullable = true)
 |-- last_checkup: timestamp (nullable = true)
 |-- balance: decimal(10,2) (nullable = true)
 |-- preferences: map (nullable = true)
 |    |-- key: string
 |    |-- value: string (valueContainsNull = true)

+-----+---+------+-------------------+-----------+----------+-------------------+--------+--------------------------------+
|name |age|weight|interests          |has_license|birthday  |last_checkup       |balance |preferences                     |
+-----+---+------+-------------------+-----------+----------+-------------------+--------+--------------------------------+
|Alice|34 |65.5  |[reading, swimming]|true       |1990-01-01|2023-01-01 10:00:00|12345.67|{language -> en, theme -> dark} |
|Bob  |45 |80.2  |[gaming, traveling]|false      |1979-05-15|2023-05-15 12:00:00|54321.01|{language -> fr, theme -> light}|
|Cathy|29 |55.0  |[cooking, painting]|true       |1995-08-20|2023-08-20 14:00:00|7890.12 |{language -> zh, theme -> dark} |
+-----+---+------+-------------------+-----------+----------+-------------------+--------+--------------------------------+
  1. 导入必要的模块

    • 从 pyspark.sql 导入 SparkSession
    • 从 pyspark.sql.functions 导入 to_dateto_timestamp
    • 从 pyspark.sql.types 导入 StructTypeStructFieldStringTypeIntegerTypeFloatTypeArrayTypeBooleanTypeDateTypeTimestampTypeDecimalTypeMapType
    • 从 decimal 模块导入 Decimal 类。
    • 从 datetime 模块导入 datetimedate 类。
  2. 初始化 SparkSession 对象

    • 创建一个名为 "Example PySpark Script with Advanced Data Types" 的 SparkSession。
  3. 定义数据结构

    • 使用 StructType 定义整个 DataFrame 的结构。
    • 包括姓名(字符串)、年龄(整数)、体重(浮点数)、兴趣爱好(数组)、是否有驾照(布尔值)、生日(日期)、最近一次体检时间(时间戳)、银行账户余额(十进制数)和偏好设置(映射)。
  4. 创建数据

    • 创建一个包含示例数据的列表 data,并将日期和时间戳类型的字符串转换为 date 和 datetime 对象。
  5. 创建 DataFrame

    • 使用 spark.createDataFrame 方法创建 DataFrame,并指定其结构。
  6. 查看 DataFrame 结构

    • 使用 df.printSchema() 查看 DataFrame 的结构。
  7. 显示 DataFrame 内容

    • 使用 df.show(truncate=False) 显示 DataFrame 的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值