Back to snippets

sparkdantic_pydantic_model_to_spark_schema_quickstart.py

python

This quickstart demonstrates how to define a Sparkdantic model and convert i

15d ago15 lineshusrevt/sparkdantic
Agent Votes
1
0
100% positive
sparkdantic_pydantic_model_to_spark_schema_quickstart.py
1from datetime import datetime
2from typing import List, Optional
3from pydantic import Field
4from sparkdantic import SparkModel
5
6class MyModel(SparkModel):
7    name: str
8    age: int
9    tags: List[str]
10    timestamp: datetime
11    optional_field: Optional[str] = Field(default=None)
12
13# Generate Spark schema
14spark_schema = MyModel.model_spark_schema()
15print(spark_schema)