首页 > 解决方案 > 在 printschema 输出上创建数据框

问题描述

我在 parquet 文件上创建了一个数据框,现在可以看到数据框模式。现在我想在 printschema 输出之上创建数据框

df = spark.read.parquet("s3/location")
df.printschema()

输出看起来像 [(cola , string) , (colb,string)] 现在我想在 printschema 的输出上创建数据框。最好的方法是什么

添加更多关于迄今为止取得的成就的信息 -

df1 = sqlContext.read.parquet("s3://t1")
df1.printSchema()

我们得到了以下结果 -

root
|-- Atp: string (nullable = true)
|-- Ccetp: string (nullable = true)
|-- Ccref: string (nullable = true)
|-- Ccbbn: string (nullable = true)
|-- Ccsdt: string (nullable = true)
|-- Ccedt: string (nullable = true)
|-- Ccfdt: string (nullable = true)
|-- Ccddt: string (nullable = true)
|-- Ccamt: string (nullable = true)

我们要创建具有两列的数据框 - 1)colname,2)数据类型

但是如果我们运行下面的代码 -

schemaRDD = spark.sparkContext.parallelize([df1.schema.json()])
schema_df = spark.read.json(schemaRDD)

schema_df.show()

我们得到低于输出,我们在一行中获取整个列名和数据类型 -

+--------------------+------+
|              fields|  type|
+--------------------+------+
|[[Atp,true,str...|struct|
+--------------------+------+

寻找类似的输出

Atp| string 
Ccetp| string
Ccref| string
Ccbbn| string
Ccsdt| string
Ccedt| string
Ccfdt| string
Ccddt| string
Ccamt| string

标签: apache-spark

解决方案


不确定您使用的是什么语言,但在 pyspark 上我会这样做:

schemaRDD = spark.sparkContext.parallelize([df.schema.json()])
schema_df = spark.read.json(schemaRDD)

推荐阅读