简体   繁体   English

在数据集中查找结构字段的数据类型

[英]Find the data type of a structfield in dataset

I have a dataset, which has the following schema:我有一个数据集,它具有以下架构:

ds.schema()
    0 = {StructField@21690} "StructField(col_name_1,NullType,true)"
    1 = {StructField@21691} "StructField(col_name_2,StringType,true)"
    2 = {StructField@21692} "StructField(col_name_3,ArrayType(StructType(StructField(person_name,StringType,true), StructField(person_surname,StringType,true)),true),true)"

I want to access the datatype of each StructField.我想访问每个 StructField 的数据类型。 Eg if the data type of col_name_1 is NullType print null.例如if the data type of col_name_1 is NullType print null.

How can I build this if loop?如何构建这个 if 循环?

You can pull this out of the schema:您可以将其从架构中提取出来:

 for field in df.schema.fields: print(field.name +" , "+str(field.dataType))

这对我来说很好:

ds.schema().apply(ColName).dataType().toString().equals("NullType")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用复杂类型的 StructField 构造 StructType - Construct StructType with StructField of a complex type 将Case类中的Map类型转换为StructField类型 - Converting Map type in Case Class to StructField Type Spark Java:在数据集 1 中而不是在数据集 2 中查找数据 - Spark Java: find data in Dataset 1 not in Dataset 2 无法找到用于存储在数据集中的类型的编码器,以通过Kafka流式处理mongo db数据 - Unable to find encoder for type stored in a Dataset for streaming mongo db data through Kafka 无法找到存储在DataSet中的Decimal类型的编码器 - Unable to find encoder for Decimal type stored in a DataSet 在列表中找到数据类型 - find the data type in List 我如何传递 Scala UserDefinedFunction,其中 output 是一个复杂类型(使用 StructType 和 StructField),从 Pyspark 使用 - How can I pass a Scala UserDefinedFunction where output is a complex type (using StructType and StructField) to be used from Pyspark “无法找到存储在数据集中的类型的编码器”和“没有足够的方法映射参数”? - “Unable to find encoder for type stored in a Dataset” and “not enough arguments for method map”? Spark错误:无法找到存储在数据集中的类型的编码器 - Spark Error: Unable to find encoder for type stored in a Dataset 在Spark数据集中转换数据时数据类型不匹配 - Data type mismatch while transforming data in spark dataset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM