Skip to main content
 首页 » 编程设计

python之Apache Spark : Can't save Grouped Data as CSV

2025年05月04日20zhenyulu

我想做一件简单的事情。我想将所有事件计数放入 2 分钟的时间戳中。

它工作正常。

df = df.groupBy(window(df["time_value"], "2 minutes")).count() 
 
df.show() 
 
+--------------------+-----+ 
| window|count| 
+--------------------+-----+ 
|[2018-04-10 15:00...| 770| 
|[2018-04-10 00:42...| 100| 
|[2018-04-10 04:14...| 54| 
|[2018-04-06 15:54...| 36| 
|[2018-04-10 04:46...| 304| 
|[2018-04-10 20:36...| 347| 
|[2018-04-10 03:26...| 41| 
|[2018-04-10 21:10...| 85| 
|[2018-04-10 11:44...| 426| 
|[2018-04-10 12:32...| 754| 
|[2018-04-10 00:28...| 61| 
|[2018-04-10 05:36...| 478| 
|[2018-04-06 07:04...| 18| 
|[2018-04-10 22:14...| 195| 
|[2018-04-10 23:40...| 175| 
|[2018-04-10 00:20...| 229| 
|[2018-04-10 03:10...| 209| 
|[2018-04-10 01:28...| 67| 
|[2018-04-09 18:52...| 9| 
|[2018-04-10 19:06...| 3548| 
+--------------------+-----+ 
only showing top 20 rows 

但是既然我写了,

df.write.csv("good_loc.csv") 
 
An error occurred while calling o161.csv. 
: java.lang.UnsupportedOperationException: CSV data source does not support struct<start:timestamp,end:timestamp> data type. 
at org.apache.spark.sql.execution.datasources.csv.CSVUtils$.org$apache$spark$sql$execution$datasources$csv$CSVUtils$$verifyType$1(CSVUtils.scala:127) 
at org.apache.spark.sql.execution.datasources.csv.CSVUtils$$anonfun$verifySchema$1.apply(CSVUtils.scala:131) 
at org.apache.spark.sql.execution.datasources.csv.CSVUtils$$anonfun$verifySchema$1.apply(CSVUtils.scala:131) 
at scala.collection.Iterator$class.foreach(Iterator.scala:893) 
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336) 
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72) 
at org.apache.spark.sql.types.StructType.foreach(StructType.scala:98) 
at org.apache.spark.sql.execution.datasources.csv.CSVUtils$.verifySchema(CSVUtils.scala:131) 
at org.apache.spark.sql.execution.datasources.csv.CSVFileFormat.prepareWrite(CSVFileFormat.scala:65) 
at org.apache.spark.sql.execution.datasources.FileFormatWriter$.write(FileFormatWriter.scala:135) 
at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand.run(InsertIntoHadoopFsRelationCommand.scala:145) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:58) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:56) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.doExecute(commands.scala:74) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138) 
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151) 
at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135) 
at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116) 
at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:92) 
at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:92) 
at org.apache.spark.sql.execution.datasources.DataSource.writeInFileFormat(DataSource.scala:437) 
at org.apache.spark.sql.execution.datasources.DataSource.write(DataSource.scala:473) 
at org.apache.spark.sql.execution.datasources.SaveIntoDataSourceCommand.run(SaveIntoDataSourceCommand.scala:50) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:58) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:56) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.doExecute(commands.scala:74) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138) 
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151) 
at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135) 
at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116) 
at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:92) 
at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:92) 
at org.apache.spark.sql.DataFrameWriter.runCommand(DataFrameWriter.scala:609) 
at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:233) 
at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:217) 
at org.apache.spark.sql.DataFrameWriter.csv(DataFrameWriter.scala:597) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244) 
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357) 
at py4j.Gateway.invoke(Gateway.java:280) 
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) 
at py4j.commands.CallCommand.execute(CallCommand.java:79) 
at py4j.GatewayConnection.run(GatewayConnection.java:214) 
at java.lang.Thread.run(Thread.java:748) 
 
Traceback (most recent call last): 
File "/usr/hdp/current/spark2-client/python/pyspark/sql/readwriter.py", line 766, in csv 
self._jwrite.csv(path) 
File "/usr/hdp/current/spark2-client/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__ 
answer, self.gateway_client, self.target_id, self.name) 
File "/usr/hdp/current/spark2-client/python/pyspark/sql/utils.py", line 63, in deco 
return f(*a, **kw) 
File "/usr/hdp/current/spark2-client/python/lib/py4j-0.10.4-src.zip/py4j/protocol.py", line 319, in get_return_value 
format(target_id, ".", name), value) 
Py4JJavaError: An error occurred while calling o161.csv. 
: java.lang.UnsupportedOperationException: CSV data source does not support struct<start:timestamp,end:timestamp> data type. 
at org.apache.spark.sql.execution.datasources.csv.CSVUtils$.org$apache$spark$sql$execution$datasources$csv$CSVUtils$$verifyType$1(CSVUtils.scala:127) 
at org.apache.spark.sql.execution.datasources.csv.CSVUtils$$anonfun$verifySchema$1.apply(CSVUtils.scala:131) 
at org.apache.spark.sql.execution.datasources.csv.CSVUtils$$anonfun$verifySchema$1.apply(CSVUtils.scala:131) 
at scala.collection.Iterator$class.foreach(Iterator.scala:893) 
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336) 
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72) 
at org.apache.spark.sql.types.StructType.foreach(StructType.scala:98) 
at org.apache.spark.sql.execution.datasources.csv.CSVUtils$.verifySchema(CSVUtils.scala:131) 
at org.apache.spark.sql.execution.datasources.csv.CSVFileFormat.prepareWrite(CSVFileFormat.scala:65) 
at org.apache.spark.sql.execution.datasources.FileFormatWriter$.write(FileFormatWriter.scala:135) 
at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand.run(InsertIntoHadoopFsRelationCommand.scala:145) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:58) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:56) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.doExecute(commands.scala:74) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138) 
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151) 
at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135) 
at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116) 
at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:92) 
at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:92) 
at org.apache.spark.sql.execution.datasources.DataSource.writeInFileFormat(DataSource.scala:437) 
at org.apache.spark.sql.execution.datasources.DataSource.write(DataSource.scala:473) 
at org.apache.spark.sql.execution.datasources.SaveIntoDataSourceCommand.run(SaveIntoDataSourceCommand.scala:50) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:58) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:56) 
at org.apache.spark.sql.execution.command.ExecutedCommandExec.doExecute(commands.scala:74) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117) 
at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138) 
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151) 
at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135) 
at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116) 
at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:92) 
at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:92) 
at org.apache.spark.sql.DataFrameWriter.runCommand(DataFrameWriter.scala:609) 
at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:233) 
at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:217) 
at org.apache.spark.sql.DataFrameWriter.csv(DataFrameWriter.scala:597) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244) 
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357) 
at py4j.Gateway.invoke(Gateway.java:280) 
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132) 
at py4j.commands.CallCommand.execute(CallCommand.java:79) 
at py4j.GatewayConnection.run(GatewayConnection.java:214) 
at java.lang.Thread.run(Thread.java:748) 

我不明白为什么会出现错误。我已经创建了数据框,我刚刚展示了它并且显示良好。现在,我只是想将它保存在 csv 中,不知道发生了什么。

请您参考如下方法:

您的列 window 是一个 StructType。因此,您不能将其直接写成 CSV。你需要把它弄平一点。在 start 和 end 两列中将其转换为字符串,或分解其结构。 CSV 只能有“简单”类型,如字符串、整数或时间戳。

试试这个:

df.select( 
    'window.start', 
    'window.end', 
    'count', 
).write.csv("good_loc.csv")