pyspark.sql.types

示例:

python 复制代码
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()
bash 复制代码
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_date, to_timestamp
    • pyspark.sql.types 导入 StructType, StructField, StringType, IntegerType, FloatType, ArrayType, BooleanType, DateType, TimestampType, DecimalType, MapType
    • decimal 模块导入 Decimal 类。
    • datetime 模块导入 datetime, date 类。
  2. 初始化 SparkSession 对象

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

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

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

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

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

    • 使用 df.show(truncate=False) 显示 DataFrame 的内容。
相关推荐
Lansonli6 小时前
大数据Spark(六十一):Spark基于Standalone提交任务流程
大数据·分布式·spark
簌簌曌9 小时前
CentOS7 + JDK8 虚拟机安装与 Hadoop + Spark 集群搭建实践
大数据·hadoop·spark
Theodore_102211 小时前
大数据(1) 大数据概述
大数据·hadoop·数据分析·spark·hbase
Aurora_NeAr11 小时前
Apache Spark详解
大数据·后端·spark
qq_408413392 天前
spark 执行 hive sql数据丢失
hive·sql·spark
后端码匠2 天前
Spark 单机模式部署与启动
大数据·分布式·spark
qq_463944862 天前
【Spark征服之路-2.3-Spark运行架构】
大数据·架构·spark
yt948322 天前
如何在IDE中通过Spark操作Hive
ide·hive·spark
不吃饭的猪2 天前
记一次spark在docker本地启动报错
大数据·docker·spark
Leo.yuan3 天前
实时数据仓库是什么?数据仓库设计怎么做?
大数据·数据库·数据仓库·数据分析·spark