在gcpdataproc中使用自定义模式读取pySpark中的JSON

在gcpdataproc中使用自定义模式读取pySpark中的JSON,json,google-cloud-platform,apache-spark-sql,pyspark-dataframes,google-cloud-dataproc,Json,Google Cloud Platform,Apache Spark Sql,Pyspark Dataframes,Google Cloud Dataproc,在gcpdataproc(使用pySpark)中,我正在执行一项任务,即按照自定义模式读取JSON文件并将其加载到数据帧中 我有以下测试JSON的示例: {"Transactions": [{"schema": "a", "id": "1", "app": "testing", "description": "JSON sche

在gcpdataproc(使用pySpark)中,我正在执行一项任务,即按照自定义模式读取JSON文件并将其加载到数据帧中

我有以下测试JSON的示例:

{"Transactions": [{"schema": "a",
"id": "1",
"app": "testing",
"description": "JSON schema for testing purpose"}]}
我创建了以下模式:

custom_schema = StructType([
                      StructField("Transactions",
                         StructType([
                             StructField("schema", StringType()),
                             StructField("id", StringType()),
                             StructField("app", StringType()),
                             StructField("description", StringType())
                                   ])
                            )])
将JSON解读为:
df_2=spark.read.json(json_路径,schema=custom_schema)

得到以下结果,

现在,我需要检查数据帧中的数据,当我尝试执行
df_2.show()
时,它占用了太多的时间,并且显示为内核繁忙


我需要帮助,这就是我在代码中缺少的内容,以及如何以dataframe(表格格式)查看数据。

我认为问题在于您的自定义模式定义和JSON文件。以下代码和JSON文件适用于我:

代码 JSON文件
gs://my bucket/transactions.json
的内容是:

{"schema": "a", "id": "1", "app": "foo", "description": "test"}
{"schema": "b", "id": "2", "app": "bar", "description": "test2"}
输出
尝试跟随,但不起作用custom_schema=StructType([StructField(“Transactions”),ArrayType(StructType([StructField(“schema”,StringType())),StructField(“id”,StringType()),StructField(“app”,StringType()),StructField(“description”,StringType())]]``你能试试df_2.show(5)看看有没有结果吗?是的,我尝试了…没有工作…似乎Dataproc群集配置也有问题?您是否尝试将其作为PySpark作业提交,而不是在Jupyter笔记本中运行。我无法重现您的问题,
df.show()
对我有效。您可以尝试使用更简单的模式。
{"schema": "a", "id": "1", "app": "foo", "description": "test"}
{"schema": "b", "id": "2", "app": "bar", "description": "test2"}
+------+---+---+-----------+
|schema| id|app|description|
+------+---+---+-----------+
|     a|  1|foo|       test|
|     b|  2|bar|      test2|
+------+---+---+-----------+