Spark 2.3.1错误:将数据框的时间戳列转换为整数类型时出错

时间:2018-10-29 07:33:19

标签: python apache-spark pyspark apache-spark-sql

我使用以下代码进行了汇总:

获取每月销售总额:

summary = data.select("OrderMonthYear", "SaleAmount").groupBy("OrderMonthYear").sum().orderBy("OrderMonthYear").toDF("OrderMonthYear","SaleAmount")

将OrderMonthYear转换为整数类型:

results = summary.rdd.map(lambda r: (int(r.OrderMonthYear.replace('-','')), r.SaleAmount)).toDF(["OrderMonthYear","SaleAmount"])

但是在尝试将时间戳列转换为整数类型时出现以下错误。

> org.apache.spark.SparkException: Job aborted due to stage failure:
> Task 0 in stage 35.0 failed 1 times, most recent failure: Lost task
> 0.0 in stage 35.0 (TID 2620, localhost, executor driver): org.apache.spark.api.python.PythonException: Traceback (most recent
> call last):
> 
> --------------------------------------------------------------------------- Py4JJavaError                             Traceback (most recent call
> last) <command-1045820357166760> in <module>()
>       3 
>       4 # Convert OrderMonthYear to integer type
> ----> 5 results = summary.rdd.map(lambda r: (int(r.OrderMonthYear.replace('-','')),
> r.SaleAmount)).toDF(["OrderMonthYear","SaleAmount"])
> 
> /databricks/spark/python/pyspark/sql/session.py in toDF(self, schema,
> sampleRatio)
>      58         [Row(name=u'Alice', age=1)]
>      59         """
> ---> 60         return sparkSession.createDataFrame(self, schema, sampleRatio)
>      61 
>      62     RDD.toDF = toDF
> 
> /databricks/spark/python/pyspark/sql/session.py in
> createDataFrame(self, data, schema, samplingRatio, verifySchema)
>     725         else:
>     726             if isinstance(data, RDD):
> --> 727                 rdd, schema = self._createFromRDD(data.map(prepare), schema, samplingRatio)
>     728             else:
>     729                 rdd, schema = self._createFromLocal(map(prepare, data), schema)
> 
> /databricks/spark/python/pyspark/sql/session.py in
> _createFromRDD(self, rdd, schema, samplingRatio)
>     384         """
>     385         if schema is None or isinstance(schema, (list, tuple)):
> --> 386             struct = self._inferSchema(rdd, samplingRatio, names=schema)
>     387             converter = _create_converter(struct)
>     388             rdd = rdd.map(converter)
> 
> /databricks/spark/python/pyspark/sql/session.py in _inferSchema(self,
> rdd, samplingRatio, names)
>     355         :return: :class:`pyspark.sql.types.StructType`
>     356         """
> --> 357         first = rdd.first()
>     358         if not first:
>     359             raise ValueError("The first row in RDD is empty, "
> 
> /databricks/spark/python/pyspark/rdd.py in first(self)    1397        
> ValueError: RDD is empty    1398         """
> -> 1399         rs = self.take(1)    1400         if rs:    1401             return rs[0]
> 
> /databricks/spark/python/pyspark/rdd.py in take(self, num)    1379    
> 1380             p = range(partsScanned, min(partsScanned +
> numPartsToTry, totalParts))
> -> 1381             res = self.context.runJob(self, takeUpToNumLeft, p)    1382     1383             items += res
> 
> /databricks/spark/python/pyspark/context.py in runJob(self, rdd,
> partitionFunc, partitions, allowLocal)    1040         #
> SparkContext#runJob.    1041         mappedRDD =
> rdd.mapPartitions(partitionFunc)
> -> 1042         sock_info = self._jvm.PythonRDD.runJob(self._jsc.sc(), mappedRDD._jrdd, partitions)    1043         return
> list(_load_from_socket(sock_info, mappedRDD._jrdd_deserializer))   
> 1044 
> 
> /databricks/spark/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py
> in __call__(self, *args)    1255         answer =
> self.gateway_client.send_command(command)    1256         return_value
> = get_return_value(
> -> 1257             answer, self.gateway_client, self.target_id, self.name)    1258     1259         for temp_arg in temp_args:
> 
> /databricks/spark/python/pyspark/sql/utils.py in deco(*a, **kw)
>      61     def deco(*a, **kw):
>      62         try:
> ---> 63             return f(*a, **kw)
>      64         except py4j.protocol.Py4JJavaError as e:
>      65             s = e.java_exception.toString()
> 
> /databricks/spark/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py in
> get_return_value(answer, gateway_client, target_id, name)
>     326                 raise Py4JJavaError(
>     327                     "An error occurred while calling {0}{1}{2}.\n".
> --> 328                     format(target_id, ".", name), value)
>     329             else:
>     330                 raise Py4JError(
> 
> Py4JJavaError: An error occurred while calling
> z:org.apache.spark.api.python.PythonRDD.runJob. :
> org.apache.spark.SparkException: Job aborted due to stage failure:
> Task 0 in stage 35.0 failed 1 times, most recent failure: Lost task
> 0.0 in stage 35.0 (TID 2620, localhost, executor driver): org.apache.spark.api.python.PythonException: Traceback (most recent
> call last):   File "/databricks/spark/python/pyspark/worker.py", line
> 262, in main
>     process()   File "/databricks/spark/python/pyspark/worker.py", line 257, in process
>     serializer.dump_stream(func(split_index, iterator), outfile)   File "/databricks/spark/python/pyspark/serializers.py", line 373, in
> dump_stream
>     vs = list(itertools.islice(iterator, batch))   File "/databricks/spark/python/pyspark/rdd.py", line 1375, in
> takeUpToNumLeft
>     yield next(iterator)   File "/databricks/spark/python/pyspark/util.py", line 55, in wrapper
>     return f(*args, **kwargs)   File "<command-1045820357166760>", line 5, in <lambda> TypeError: an integer is required
> 
>   at
> org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:317)
>   at
> org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:457)
>   at
> org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:440)
>   at
> org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:271)
>   at
> org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
>   at scala.collection.Iterator$class.foreach(Iterator.scala:893)  at
> org.apache.spark.InterruptibleIterator.foreach(InterruptibleIterator.scala:28)
>   at
> scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
>   at
> scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
>   at
> scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
>   at
> scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
>   at
> org.apache.spark.InterruptibleIterator.to(InterruptibleIterator.scala:28)
>   at
> scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
>   at
> org.apache.spark.InterruptibleIterator.toBuffer(InterruptibleIterator.scala:28)
>   at
> scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
>   at
> org.apache.spark.InterruptibleIterator.toArray(InterruptibleIterator.scala:28)
>   at
> org.apache.spark.api.python.PythonRDD$$anonfun$3.apply(PythonRDD.scala:182)
>   at
> org.apache.spark.api.python.PythonRDD$$anonfun$3.apply(PythonRDD.scala:182)
>   at
> org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2181)
>   at
> org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2181)
>   at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
>   at org.apache.spark.scheduler.Task.run(Task.scala:112)  at
> org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:384)
>   at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
>   at java.lang.Thread.run(Thread.java:748)
> 
> Driver stacktrace:    at
> org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1747)
>   at
> org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1735)
>   at
> org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1734)
>   at
> scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
>   at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
>   at
> org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1734)
>   at
> org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:962)
>   at
> org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:962)
>   at scala.Option.foreach(Option.scala:257)   at
> org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:962)
>   at
> org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:1970)
>   at
> org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1918)
>   at
> org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:1906)
>   at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:49)
>   at
> org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:759)
>   at org.apache.spark.SparkContext.runJob(SparkContext.scala:2141)    at
> org.apache.spark.SparkContext.runJob(SparkContext.scala:2162)     at
> org.apache.spark.SparkContext.runJob(SparkContext.scala:2181)     at
> org.apache.spark.api.python.PythonRDD$.runJob(PythonRDD.scala:182)    at
> org.apache.spark.api.python.PythonRDD.runJob(PythonRDD.scala)     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:380)    at
> py4j.Gateway.invoke(Gateway.java:295)     at
> py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
>   at py4j.commands.CallCommand.execute(CallCommand.java:79)   at
> py4j.GatewayConnection.run(GatewayConnection.java:251)    at
> java.lang.Thread.run(Thread.java:748) Caused by:
> org.apache.spark.api.python.PythonException: Traceback (most recent
> call last):   File "/databricks/spark/python/pyspark/worker.py", line
> 262, in main
>     process()   File "/databricks/spark/python/pyspark/worker.py", line 257, in process
>     serializer.dump_stream(func(split_index, iterator), outfile)   File "/databricks/spark/python/pyspark/serializers.py", line 373, in
> dump_stream
>     vs = list(itertools.islice(iterator, batch))   File "/databricks/spark/python/pyspark/rdd.py", line 1375, in
> takeUpToNumLeft
>     yield next(iterator)   File "/databricks/spark/python/pyspark/util.py", line 55, in wrapper
>     return f(*args, **kwargs)   File "<command-1045820357166760>", line 5, in <lambda> TypeError: an integer is required
> 
>   at
> org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:317)
>   at
> org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:457)
>   at
> org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:440)
>   at
> org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:271)
>   at
> org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
>   at scala.collection.Iterator$class.foreach(Iterator.scala:893)  at
> org.apache.spark.InterruptibleIterator.foreach(InterruptibleIterator.scala:28)
>   at
> scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
>   at
> scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
>   at
> scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
>   at
> scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
>   at
> org.apache.spark.InterruptibleIterator.to(InterruptibleIterator.scala:28)
>   at
> scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
>   at
> org.apache.spark.InterruptibleIterator.toBuffer(InterruptibleIterator.scala:28)
>   at
> scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
>   at
> org.apache.spark.InterruptibleIterator.toArray(InterruptibleIterator.scala:28)
>   at
> org.apache.spark.api.python.PythonRDD$$anonfun$3.apply(PythonRDD.scala:182)
>   at
> org.apache.spark.api.python.PythonRDD$$anonfun$3.apply(PythonRDD.scala:182)
>   at
> org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2181)
>   at
> org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2181)
>   at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
>   at org.apache.spark.scheduler.Task.run(Task.scala:112)  at
> org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:384)
>   at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
>   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
>   ... 1 more

感谢您的支持。谢谢!

3 个答案:

答案 0 :(得分:0)

我认为您的数据如下所示;

>>> summary.select("OrderMonthYear","SaleAmount").show()
+--------------------+----------+
|      OrderMonthYear|SaleAmount|
+--------------------+----------+
|2009-01-01T00:00:...|        10|
|2009-02-01T00:00:...|        50|
+--------------------+----------+

因此您可以使用sql函数将日期字段转换为整数

>>> import pyspark.sql.functions as F    
>>> summary.withColumn('OrderMonthYear',F.date_format(F.to_date(F.substring('OrderMonthYear',1,10),'yyyy-MM-dd'),'yyyyMMdd').cast('integer')).show()
+--------------+----------+
|OrderMonthYear|SaleAmount|
+--------------+----------+
|      20090101|        10|
|      20090201|        50|
+--------------+----------+

由于Spark版本的缘故,它可能无法正常工作。如果出现错误,请尝试以下

>>> summary.withColumn('OrderMonthYear',F.date_format(F.to_date( \
...             F.unix_timestamp(F.substring('OrderMonthYear',1,10),'yyyy-MM-dd'). \
...             cast('timestamp')),'yyyyMMdd').cast('integer')).show()
+--------------+----------+
|OrderMonthYear|SaleAmount|
+--------------+----------+
|      20090101|        10|
|      20090201|        50|
+--------------+----------+

答案 1 :(得分:0)

您正在尝试对可识别时区的日期对象执行整数和替换操作。 .replace('-','')方法引起错误,因为您的时区为+0000。相反,请尝试以下操作:

results = summary.rdd.map(lambda r: (int(str(r.OrderMonthYear)[:4]), r.SaleAmount)).toDF(["OrderMonthYear","SaleAmount"])

希望这行得通。

答案 2 :(得分:0)

results = summary.rdd.map(lambda r: (int(str(r.OrderMonthYear)[:10].replace('-','')), r.SaleAmount)).toDF(["OrderMonthYear","SaleAmount"])