Schemas and Creating DataFrames A schema in Spark defines the column names and…
- User
Schemas and Creating DataFrames A schema in Spark defines the column names and associated data types for a Data‐ Frame. Most often, schemas come into play when you are reading structured data from an external data source (more on this in the next chapter). Defining a schema up front as opposed to taking a schema-on-read approach offers three benefits: 50 | Chapter 3: Apache Spark’s Structured APIs • You relieve Spark from the onus of inferring data types. • You prevent Spark from creating a separate job just to read a large portion of your file to ascertain the schema, which for a large data file can be expensive and time-consuming. • You can detect errors early if data doesn’t match the schema. So, we encourage you to always define your schema up front whenever you want to read a large file from a data source. For a short illustration, let’s define a schema fo
Bahii Spark 3ana el concept mt3 “Schema” okii : -> y3ni we can defines the column names and associated data types okii for a DataFrame okii
Bahii fe akther el 7alet bsh nts3mlo el concept of schemas where we reading structured data from an external data source → bahii 7aje tw3nik and lzem t5oh fel 7sben enou : enik Define the shema up front as oppesed to taking a schema-on-read approach y5lik 3andik these 3 benefits okii
Bach trte7 spark men “yinferring” el data types which reduce the lowd Cost effficient 5ater bsh t5alii spark m8er my3ml “as separate” job for just enou y9ra large data file which can be expensive okii and time-consuming okii Also y5lik detect errors early if data doesn’t match the scheam okii Two ways to dene a schema Spark allows you to define a schema in two ways. One is to define it programmati‐ cally, and the other is to employ a Data Definition Language (DDL) string, which is much simpler and easier to read. To define a schema programmatically for a DataFrame with three named columns, author, title, and pages, you can use the Spark DataFrame API. For example: // In Scala import org.apache.spark.sql.types._ val schema = StructType(Array(StructField("author", StringType, false), StructField("title", StringType, false), StructField("pages", IntegerType, false))) # In Python from pyspark.sql.types import * schema = StructType([StructField("author", StringType(), False), StructField("title", StringType(), False), StructField("pages", IntegerType(), False)]) Defining the same schema using DDL is much simpler: // In Scala val schema = "author STRING, title STRING, pages INT" # In Python schema = "author STRING, title STRING, pages INT"
Q- bahii honii n7ebikk ta5oo : pause Q- t3tenii 3 simple exmple of imanginary data set Q- and tel me the code to implement the schema in the two ways here oki
- Assistant
أكيد، خلينا نبدأ بخطوتين:
- مثال بسيط لمجموعة بيانات وهمية
- الكود لتعريف الـ Schema بطريقتين مختلفتين
1. مثال لمجموعة بيانات وهمية
افترض عندنا بيانات عن الكتب في مكتبة، وكل كتاب يحتوي على:
- المؤلف (Author): اسم المؤلف