在pyspark中创建配置单元模式

bnl4lu3b  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(309)

在pyspark中创建模式的语法。

data.csv
id,name
1,sam
2,smith
val schema = new StructType().add("id", IntType).add("name", StringType)
val ds = spark.read.schema(schema).option("header", "true").csv("data.csv")
ds.show
fjnneemd

fjnneemd1#

使用structfield(name,datatype,nullable=true)定义structtype
从pyspark.sql.types可以导入数据类型

from pyspark.sql.types import StructType, StructField, IntegerType, StringType,FloatType,BooleanType
schema = StructType([
    StructField("col_a", StringType(), True),
    StructField("col_b", IntegerType(), True),
    StructField("col_c", FloatType(), True),
    StructField("col_d", BooleanType(), True)
])

相关问题